Apama  10.1.0.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
metadata_t.hpp
Go to the documentation of this file.
1 /*
2  * Title: bits/metadata_t.hpp
3  * Description: C++ header-only wrapper for C-ABI data_t type
4  * $Copyright (c) 2015-2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$
5  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
6  * @Version: $Id: metadata_t.hpp 298712 2017-01-05 16:12:59Z mipr $
7  */
8 
15 #ifndef _DATAT_BITS_INCLUDE_TAG
16 #error Must only be included from sag_connectivity_cpp.hpp, use that instead
17 #endif
18 
38 class metadata_t: protected map_t
39 {
40  friend class data_t;
41  friend class Message;
42 public:
49  template<typename DATA, typename UNDERLYING, typename PAIR>
50  struct _iterator: public map_t::_iterator<DATA, UNDERLYING, PAIR>
51  {
54 
55  _iterator(): map_iterator() {}
56  _iterator(const UNDERLYING &tabledata, size_t capacity, int64_t offs, bool stepToItem=false)
57  : map_iterator(tabledata, capacity, offs, stepToItem)
58  {}
59  _iterator(const map_iterator &other): map_iterator(other) {}
60  iterator &operator=(const map_iterator &other)
61  {
62  map_iterator::tabledata = other.tabledata;
63  map_iterator::offs = other.offs;
64  map_iterator::step = other.step;
65  map_iterator::capacity = other.capacity;
66  return *this;
67  }
68 
69  std::string key() const
70  {
71  assert(map_iterator::tabledata[map_iterator::offs].key.tag == SAG_DATA_STRING);
72  if (map_iterator::tabledata[map_iterator::offs].key.tag != SAG_DATA_STRING) throw std::runtime_error("Metadata key not a string");
73  return map_iterator::tabledata[map_iterator::offs].key.string;
74  }
75  std::string value() const
76  {
77  try {
78  return(convert_to<std::string>(map_iterator::operator*().second));
79  } catch (...) {
80  throw std::runtime_error("Non-stringifiable data in metadata");
81  }
82  }
83  private: // can't expose these as string/string so make them inaccessible
84  typename map_iterator::element_t &operator*() const
85  {
86  return reinterpret_cast<typename map_iterator::element_t &>(map_iterator::tabledata[map_iterator::offs]);
87  }
88  typename map_iterator::element_t *operator->() const
89  {
90  return reinterpret_cast<typename map_iterator::element_t *>(map_iterator::tabledata + map_iterator::offs);
91  }
92  };
95 
97  metadata_t(): map_t() {}
99  explicit metadata_t(size_t n): map_t(n) {}
101  metadata_t(metadata_t &&other) : map_t(std::move(other)) {}
105  {
106  metadata_t tmp;
107  swap(std::move(tmp));
108  swap(std::move(other));
109  return *this;
110  }
112  metadata_t copy() const;
113 
114  using map_t::empty;
115  using map_t::size;
116  using map_t::clear;
117 
119  bool operator==(const metadata_t &other) const
120  {
121  return static_cast<const map_t&>(*this) == static_cast<const map_t&>(other);
122  }
124  bool operator!=(const metadata_t &other) const { return !operator==(other); }
125 
127  void swap(metadata_t &&other)
128  {
129  return static_cast<map_t&>(*this).swap(static_cast<map_t&&>(other));
130  }
131 
135  std::string operator[](const char *k)
136  {
137  iterator it = find(k);
138  if (it == end()) {
139  throw std::runtime_error("Element not in map");
140  } else {
141  return it.value();
142  }
143  }
146  std::string const operator[](const char *k) const
147  {
148  const_iterator it = find(k);
149  if (it == end()) {
150  throw std::runtime_error("Element not in map");
151  } else {
152  return it.value();
153  }
154  }
158  std::string operator[](const std::string &k)
159  {
160  iterator it = find(k);
161  if (it == end()) {
162  throw std::runtime_error("Element not in map");
163  } else {
164  return it.value();
165  }
166  }
169  std::string const operator[](const std::string &k) const
170  {
171  const_iterator it = find(k);
172  if (it == end()) {
173  throw std::runtime_error("Element not in map");
174  } else {
175  return it.value();
176  }
177  }
180  iterator find(const char *k)
181  {
182  return map_t::find(data_t(k));
183  }
186  const_iterator find(const char *k) const
187  {
188  return map_t::find(data_t(k));
189  }
192  iterator find(const std::string &k)
193  {
194  return map_t::find(data_t(k));
195  }
198  const_iterator find(const std::string &k) const
199  {
200  return map_t::find(data_t(k));
201  }
202 
205  iterator find(const data_t &k)
206  {
207  return map_t::find(k);
208  }
211  const_iterator find(const data_t &k) const
212  {
213  return map_t::find(k);
214  }
223  std::pair<iterator, bool> insert(const std::pair<std::string, std::string> &v)
224  {
225  return map_t::insert(std::make_pair(data_t(v.first), data_t(v.second)));
226  }
227 
236  std::pair<iterator, bool> insert(const std::pair<const char*, const char*> &v)
237  {
238  return map_t::insert(std::make_pair(data_t(v.first), data_t(v.second)));
239  }
240 
245  size_t erase(const std::string &k)
246  {
247  return map_t::erase(data_t(k));
248  }
253  size_t erase(const char *k)
254  {
255  return map_t::erase(data_t(k));
256  }
261  void erase(iterator it)
262  {
263  map_t::erase(it);
264  }
265 
266  /* Iteration:
267  * begin() and end() methods, both const and non-const. Unordered so no reverse iterator
268  */
270  iterator begin() { return iterator(table?table->table:0, table?table->capacity:0, 0, true); }
272  iterator end() { return iterator(table?table->table:0, table?table->capacity:0, table?table->capacity:0, false); }
274  const_iterator begin() const { return const_iterator(table?table->table:0, table?table->capacity:0, 0, true); }
276  const_iterator end() const { return const_iterator(table?table->table:0, table?table->capacity:0, table?table->capacity:0, false); }
278  const_iterator cbegin() const { return const_iterator(table?table->table:0, table?table->capacity:0, 0, true); }
280  const_iterator cend() const { return const_iterator(table?table->table:0, table?table->capacity:0, table?table->capacity:0, false); }
281 
282 private:
284  metadata_t &operator=(const metadata_t &other) { abort(); }
286  metadata_t(const metadata_t &other) { abort(); }
287 };
288 
289 
metadata_t(metadata_t &&other)
Move constructor.
Definition: metadata_t.hpp:101
iterator find(const data_t &k)
Searches the map for an item with the given key and returns an iterator to that position in the map...
std::pair< iterator, bool > insert(const std::pair< data_t &&, data_t && > &v)
Insert a new key/value pair into the map.
const_iterator find(const std::string &k) const
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:198
bool operator!=(const metadata_t &other) const
Returns false if this map deep-equals other.
Definition: metadata_t.hpp:124
metadata_t(size_t n)
Construct a map with (at least) capacity for n items.
Definition: metadata_t.hpp:99
A container for an payload and associated metadata.
Definition: message.hpp:27
A map class which implements many of the functions on std::map.
Definition: map_t.hpp:36
const_iterator cbegin() const
Forward const_iterator begin.
Definition: metadata_t.hpp:278
std::pair< iterator, bool > insert(const std::pair< std::string, std::string > &v)
Insert a new key/value pair into the map.
Definition: metadata_t.hpp:223
std::string const operator[](const char *k) const
Returns the item with the given key.
Definition: metadata_t.hpp:146
const_iterator begin() const
Forward const_iterator begin.
Definition: metadata_t.hpp:274
STL namespace.
size_t erase(const char *k)
Remove the item with the specified key.
Definition: metadata_t.hpp:253
iterator find(const char *k)
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:180
size_t erase(const std::string &k)
Remove the item with the specified key.
Definition: metadata_t.hpp:245
const_iterator end() const
Forward const_iterator end.
Definition: metadata_t.hpp:276
const_iterator find(const data_t &k) const
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:211
iterator find(const data_t &k)
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:205
size_t size() const
Returns the number of key/value pairs in the map.
Definition: map_t.hpp:285
void swap(map_t &&other)
Swap the contents of this map and other.
Definition: map_t.hpp:193
const_iterator cend() const
Forward const_iterator end.
Definition: metadata_t.hpp:280
metadata_t & operator=(metadata_t &&other)
Move assignment.
Definition: metadata_t.hpp:104
Forward/reverse and const/non-const iterators are implemented using this class.
Definition: map_t.hpp:62
const_iterator find(const char *k) const
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:186
iterator end()
Forward iterator end.
Definition: metadata_t.hpp:272
void erase(iterator it)
Erase the item pointed to by this iterator.
Definition: metadata_t.hpp:261
metadata_t()
Construct an empty map.
Definition: metadata_t.hpp:97
void clear()
Empty the map, clean the name and free the underlying data.
bool empty() const
Returns true if the map is empty (size() == 0)
Definition: map_t.hpp:288
bool operator==(const metadata_t &other) const
Returns true if this map deep-equals other.
Definition: metadata_t.hpp:119
std::pair< iterator, bool > insert(const std::pair< const char *, const char * > &v)
Insert a new key/value pair into the map.
Definition: metadata_t.hpp:236
size_t erase(const data_t &k)
Remove the item with the specified key.
utf8-encoded const char*
Definition: sag_connectivity_c.h:46
A variant type which can be one of the following:
Definition: data_t.hpp:42
iterator begin()
Forward iterator begin.
Definition: metadata_t.hpp:270
Forward/reverse and const/non-const iterators are implemented using this class.
Definition: metadata_t.hpp:50
void swap(metadata_t &&other)
Swap the contents of this map and other.
Definition: metadata_t.hpp:127
std::string const operator[](const std::string &k) const
Returns the item with the given key.
Definition: metadata_t.hpp:169
iterator find(const std::string &k)
Searches the map for an item with the given key and returns an iterator to that position in the map...
Definition: metadata_t.hpp:192
metadata_t copy() const
Return a deep copy of this map.
A map class which implements many of the functions on std::map.
Definition: metadata_t.hpp:38
std::string operator[](const std::string &k)
Returns the item with the given key.
Definition: metadata_t.hpp:158
std::string operator[](const char *k)
Returns the item with the given key.
Definition: metadata_t.hpp:135