Robot Raconteur Core C++ Library
StringTable.h
Go to the documentation of this file.
1 
25 #include <boost/unordered_map.hpp>
26 
27 #pragma once
28 
29 namespace RobotRaconteur
30 {
31 class Message;
32 class MessageEntry;
33 class MessageElement;
34 
35 namespace detail
36 {
37 class ROBOTRACONTEUR_CORE_API StringTableEntry
38 {
39  public:
40  StringTableEntry();
41 
42  MessageStringPtr value;
43  uint32_t code;
44  bool confirmed;
45  std::vector<uint32_t> table_flags;
46 };
47 
48 class ROBOTRACONTEUR_CORE_API StringTable : private boost::noncopyable
49 {
50  public:
51  StringTable(bool server);
52  virtual ~StringTable();
53 
54  uint32_t GetCodeForString(MessageStringRef str);
55  bool GetStringForCode(uint32_t code, MessageStringPtr& str);
56 
57  RR_SHARED_PTR<const StringTableEntry> GetEntryForString(MessageStringRef str);
58  RR_SHARED_PTR<const StringTableEntry> GetEntryForCode(uint32_t code);
59 
60  bool AddCode(uint32_t code, MessageStringRef str, const std::vector<uint32_t>& table_flags);
61  void AddCodesCSV(const std::string& csv, const std::vector<uint32_t>& table_flags);
62 
63  void MessageReplaceStringsWithCodes(const RR_INTRUSIVE_PTR<Message>& m);
64  void MessageReplaceCodesWithStrings(const RR_INTRUSIVE_PTR<Message>& m);
65 
66  std::vector<uint32_t> GetTableFlags();
67  void SetTableFlags(std::vector<uint32_t> flags);
68 
69  protected:
70  void MessageEntryReplaceStringsWithCodes(const RR_INTRUSIVE_PTR<MessageEntry>& e,
71  boost::unordered_map<MessageStringPtr, uint32_t>& local_table,
72  uint32_t& next_local_code, uint32_t& table_size);
73  void MessageElementReplaceStringsWithCodes(const RR_INTRUSIVE_PTR<MessageElement>& e,
74  boost::unordered_map<MessageStringPtr, uint32_t>& local_table,
75  uint32_t& next_local_code, uint32_t& table_size);
76  void MessageEntryReplaceCodesWithStrings(const RR_INTRUSIVE_PTR<MessageEntry>& e,
77  boost::unordered_map<uint32_t, MessageStringPtr>& local_table);
78  void MessageElementReplaceCodesWithStrings(const RR_INTRUSIVE_PTR<MessageElement>& e,
79  boost::unordered_map<uint32_t, MessageStringPtr>& local_table);
80 
81  void DoReplaceString(MessageStringPtr& str, uint32_t& code, uint8_t& flags, uint32_t flag_str, uint32_t flag_code,
82  boost::unordered_map<MessageStringPtr, uint32_t>& local_table, uint32_t& next_local_code,
83  uint32_t& table_size);
84  void DoReplaceCode(MessageStringPtr& str, uint32_t& code, uint8_t& flags, uint32_t flag_str, uint32_t flag_code,
85  boost::unordered_map<uint32_t, MessageStringPtr>& local_table);
86 
87  bool AddCode_p(uint32_t code, MessageStringRef str, const std::vector<uint32_t>& table_flags);
88  void AddCodesCSV_p(const std::string& csv, const std::vector<uint32_t>& table_flags);
89 
90  bool server;
91 
92  size_t max_entry_count;
93  size_t max_str_len;
94  uint32_t next_code;
95 
96  boost::mutex this_lock;
97 
98  RR_UNORDERED_MAP<uint32_t, RR_SHARED_PTR<StringTableEntry> > code_table;
99  RR_UNORDERED_MAP<MessageStringPtr, RR_SHARED_PTR<StringTableEntry> > string_table;
100 
101  uint32_t flags;
102 };
103 
104 } // namespace detail
105 } // namespace RobotRaconteur