proio
event.h
1 #ifndef PROIO_EVENT_H
2 #define PROIO_EVENT_H
3 
4 #include <string>
5 
6 #include "google/protobuf/descriptor.h"
7 #include "google/protobuf/dynamic_message.h"
8 
9 #include "proio/proto/proio.pb.h"
10 
11 namespace proio {
14 class Event {
15  public:
18  Event();
21  Event(const std::string &proto);
22  virtual ~Event();
23 
28  uint64_t AddEntry(google::protobuf::Message *entry, std::string tag = "");
37  google::protobuf::Message *GetEntry(uint64_t id);
41  void TagEntry(uint64_t id, std::string tag);
44  void UntagEntry(uint64_t id, std::string tag);
47  void RemoveEntry(uint64_t id);
50  std::vector<std::string> Tags();
54  std::vector<uint64_t> TaggedEntries(std::string tag);
57  std::vector<uint64_t> AllEntries();
61  std::vector<std::string> EntryTags(uint64_t id);
64  void DeleteTag(std::string tag);
69  google::protobuf::Message *Free(const google::protobuf::Descriptor *desc);
74  std::map<std::string, std::shared_ptr<const std::string>> &Metadata() { return metadata; }
75 
78  std::string String();
82  bool SerializeToString(std::string *output) {
83  FlushCache();
84  return getProto()->SerializeToString(output);
85  }
90  void FlushCache();
93  void Clear();
99  void SetDescriptorPool(const google::protobuf::DescriptorPool *pool = NULL);
108  void UseGeneratedPool(bool useGenPool = true);
109 
110  Event &operator=(const Event &event);
111 
112  private:
113  uint64_t getTypeID(google::protobuf::Message *entry);
114  const google::protobuf::Descriptor *getDescriptor(uint64_t typeID);
115  void tagCleanup();
116  void clearDescriptors();
117 
118  friend class Writer;
119  friend class Reader;
120 
121  proto::Event *getProto();
122 
123  proto::Event *eventProto;
124  std::map<std::string, uint64_t> revTypeLookup;
125  std::map<uint64_t, google::protobuf::Message *> entryCache;
126  std::map<uint64_t, const google::protobuf::Descriptor *> descriptorCache;
127  std::map<std::string, std::shared_ptr<const std::string>> metadata;
128  const google::protobuf::DescriptorPool *descriptorPool;
129  std::unique_ptr<google::protobuf::DynamicMessageFactory> messageFactory;
130  bool useGenPool;
131  bool dirtyTags;
132 
133  std::map<const google::protobuf::Descriptor *, std::vector<google::protobuf::Message *>> store;
134 };
135 
136 const class UnknownMessageTypeError : public std::exception {
137  virtual const char *what() const throw() { return "Unknown message type"; }
138 } unknownMessageTypeError;
139 } // namespace proio
140 
141 #endif // PROIO_EVENT_H
google::protobuf::Message * Free(const google::protobuf::Descriptor *desc)
Definition: event.cc:142
void DeleteTag(std::string tag)
Definition: event.cc:140
std::vector< std::string > Tags()
Definition: event.cc:100
std::vector< std::string > EntryTags(uint64_t id)
Definition: event.cc:127
Definition: event.h:11
uint64_t AddEntry(google::protobuf::Message *entry, std::string tag="")
Definition: event.cc:27
google::protobuf::Message * GetEntry(uint64_t id)
Definition: event.cc:43
void TagEntry(uint64_t id, std::string tag)
Definition: event.cc:75
void FlushCache()
Definition: event.cc:172
std::map< std::string, std::shared_ptr< const std::string > > & Metadata()
Definition: event.h:74
void SetDescriptorPool(const google::protobuf::DescriptorPool *pool=NULL)
Definition: event.cc:205
void Clear()
Definition: event.cc:191
void RemoveEntry(uint64_t id)
Definition: event.cc:89
bool SerializeToString(std::string *output)
Definition: event.h:82
std::string String()
Definition: event.cc:152
void UntagEntry(uint64_t id, std::string tag)
Definition: event.cc:77
void UseGeneratedPool(bool useGenPool=true)
Definition: event.cc:212
std::vector< uint64_t > AllEntries()
Definition: event.cc:120
std::vector< uint64_t > TaggedEntries(std::string tag)
Definition: event.cc:109