entry.h 1.0 KB
Newer Older
S
superjom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef VISUALDL_STORAGE_ENTRY_H
#define VISUALDL_STORAGE_ENTRY_H

#include "visualdl/logic/im.h"
#include "visualdl/storage/storage.pb.h"
#include "visualdl/utils/guard.h"

namespace visualdl {

struct Storage;

/*
 * Utility helper for storage::Entry.
 */
template <typename T>
struct Entry {
S
superjom 已提交
17
  DECL_GUARD(Entry<T>)
S
superjom 已提交
18 19 20 21
  // use pointer to avoid copy
  storage::Entry* entry{nullptr};

  Entry() {}
S
superjom 已提交
22
  explicit Entry(storage::Entry* entry, Storage* parent)
S
superjom 已提交
23
      : entry(entry), x_(parent) {}
S
superjom 已提交
24
  void operator()(storage::Entry* entry, Storage* parent) {
S
superjom 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    this->entry = entry;
    x_ = parent;
  }

  // Set a single value.
  void Set(T v);

  // Add a value to repeated message field.
  void Add(T v);

  Storage* parent() { return x_; }

private:
  Storage* x_;
};

template <typename T>
struct EntryReader {
  EntryReader(storage::Entry x) : data_(x) {}
  // Get a single value.
  T Get() const;
  // Get repeated field.
  std::vector<T> GetMulti() const;

private:
  storage::Entry data_;
};

}  // namespace visualdl

#endif