entry.cc 3.6 KB
Newer Older
S
superjom 已提交
1 2 3 4 5 6 7 8 9
#include "visualdl/storage/entry.h"

namespace visualdl {

#define IMPL_ENTRY_SET_OR_ADD(method__, ctype__, dtype__, opr__) \
  template <>                                                    \
  void Entry<ctype__>::method__(ctype__ v) {                     \
    entry->set_dtype(storage::DataType::dtype__);                \
    entry->opr__(v);                                             \
S
superjom 已提交
10
    WRITE_GUARD                                                  \
S
superjom 已提交
11 12
  }

S
superjom 已提交
13 14 15 16 17 18 19 20 21 22 23
#define IMPL_ENTRY_SETMUL(ctype__, dtype__, field__)              \
  template <>                                                     \
  void Entry<ctype__>::SetMulti(const std::vector<ctype__>& vs) { \
    entry->set_dtype(storage::DataType::dtype__);                 \
    entry->clear_##field__();                                     \
    for (auto v : vs) {                                           \
      entry->add_##field__(v);                                    \
    }                                                             \
    WRITE_GUARD                                                   \
  }

S
superjom 已提交
24
template <>
S
superjom 已提交
25
void Entry<std::vector<byte_t>>::Set(std::vector<byte_t> v) {
S
superjom 已提交
26 27 28 29 30 31
  entry->set_dtype(storage::DataType::kBytes);
  entry->set_y(std::string(v.begin(), v.end()));
  WRITE_GUARD
}

template <>
S
superjom 已提交
32
void Entry<std::vector<byte_t>>::Add(std::vector<byte_t> v) {
S
superjom 已提交
33 34 35 36 37
  entry->set_dtype(storage::DataType::kBytess);
  *entry->add_ys() = std::string(v.begin(), v.end());
  WRITE_GUARD
}

S
superjom 已提交
38
IMPL_ENTRY_SET_OR_ADD(Set, int, kInt32, set_i32);
S
superjom 已提交
39
IMPL_ENTRY_SET_OR_ADD(Set, std::string, kString, set_s);
S
superjom 已提交
40 41 42 43
IMPL_ENTRY_SET_OR_ADD(Set, int64_t, kInt64, set_i64);
IMPL_ENTRY_SET_OR_ADD(Set, bool, kBool, set_b);
IMPL_ENTRY_SET_OR_ADD(Set, float, kFloat, set_f);
IMPL_ENTRY_SET_OR_ADD(Set, double, kDouble, set_d);
S
superjom 已提交
44
IMPL_ENTRY_SET_OR_ADD(Add, int, kInt32s, add_i32s);
S
superjom 已提交
45 46 47 48 49 50
IMPL_ENTRY_SET_OR_ADD(Add, int64_t, kInt64s, add_i64s);
IMPL_ENTRY_SET_OR_ADD(Add, float, kFloats, add_fs);
IMPL_ENTRY_SET_OR_ADD(Add, double, kDoubles, add_ds);
IMPL_ENTRY_SET_OR_ADD(Add, std::string, kStrings, add_ss);
IMPL_ENTRY_SET_OR_ADD(Add, bool, kBools, add_bs);

S
superjom 已提交
51 52 53 54 55 56
IMPL_ENTRY_SETMUL(int, kInt32, i32s);
IMPL_ENTRY_SETMUL(int64_t, kInt64, i64s);
IMPL_ENTRY_SETMUL(float, kFloat, fs);
IMPL_ENTRY_SETMUL(double, kDouble, ds);
IMPL_ENTRY_SETMUL(bool, kBool, bs);

S
superjom 已提交
57 58 59
#define IMPL_ENTRY_GET(T, fieldname__) \
  template <>                          \
  T EntryReader<T>::Get() const {      \
S
superjom 已提交
60
    return data_.fieldname__();        \
S
superjom 已提交
61 62
  }

S
superjom 已提交
63
IMPL_ENTRY_GET(int, i32);
S
superjom 已提交
64 65 66 67 68 69
IMPL_ENTRY_GET(int64_t, i64);
IMPL_ENTRY_GET(float, f);
IMPL_ENTRY_GET(double, d);
IMPL_ENTRY_GET(std::string, s);
IMPL_ENTRY_GET(bool, b);

S
superjom 已提交
70
template <>
S
superjom 已提交
71
std::vector<uint8_t> EntryReader<std::vector<byte_t>>::Get() const {
S
superjom 已提交
72
  const auto& y = data_.y();
S
superjom 已提交
73
  return std::vector<byte_t>(y.begin(), y.end());
S
superjom 已提交
74 75
}

S
superjom 已提交
76 77 78 79 80 81 82
#define IMPL_ENTRY_GET_MULTI(T, fieldname__)           \
  template <>                                          \
  std::vector<T> EntryReader<T>::GetMulti() const {    \
    return std::vector<T>(data_.fieldname__().begin(), \
                          data_.fieldname__().end());  \
  }

S
superjom 已提交
83
IMPL_ENTRY_GET_MULTI(int, i32s);
S
superjom 已提交
84
IMPL_ENTRY_GET_MULTI(int64_t, i64s);
S
superjom 已提交
85 86 87 88 89
IMPL_ENTRY_GET_MULTI(float, fs);
IMPL_ENTRY_GET_MULTI(double, ds);
IMPL_ENTRY_GET_MULTI(std::string, ss);
IMPL_ENTRY_GET_MULTI(bool, bs);

S
superjom 已提交
90 91 92 93
template class Entry<int>;
template class Entry<float>;
template class Entry<double>;
template class Entry<bool>;
S
superjom 已提交
94
template class Entry<std::vector<byte_t>>;
S
superjom 已提交
95 96 97 98 99

template class EntryReader<int>;
template class EntryReader<float>;
template class EntryReader<double>;
template class EntryReader<bool>;
S
superjom 已提交
100
template class EntryReader<std::vector<byte_t>>;
S
superjom 已提交
101

S
superjom 已提交
102
}  // namespace visualdl