MADARA  3.4.1
VariableReference.h
Go to the documentation of this file.
1 #ifndef _MADARA_VARIABLE_REFERENCE_H_
2 #define _MADARA_VARIABLE_REFERENCE_H_
3 
12 #include <string>
13 #include <cstring>
14 #include "madara/MadaraExport.h"
19 
20 namespace madara
21 {
22 namespace knowledge
23 {
24 class ThreadSafeContext;
25 class KnolwedgeBaseImpl;
26 class Variables;
27 
32 class MADARA_EXPORT VariableReference
33 {
34 public:
35  friend class ThreadSafeContext;
36  friend class KnowledgeBaseImpl;
37  friend class Variables;
38 
39  using pair_ptr = KnowledgeMap::value_type*;
40 
44  VariableReference() = default;
45 
50 
55  bool is_valid(void) const;
56 
61  bool operator==(const VariableReference& rhs) const;
62 
67  bool operator!=(const VariableReference& rhs) const;
68 
75  const char* get_name(void) const;
76 
84  KnowledgeRecord* get_record_unsafe(void) const;
85 
89  void assign(pair_ptr entry);
90 
91 private:
92  pair_ptr entry_ = nullptr;
93 };
94 
96 typedef std::vector<VariableReference> VariableReferences;
97 
99 typedef std::map<const char*, VariableReference, utility::ComparisonLessThan>
101 
121 {
122 public:
134  bool history = false, uint64_t min_toi = 0)
135  : map_(map), history_(history), min_toi_(min_toi)
136  {
137  }
138 
143  {
144  public:
145  using value_type = std::pair<const char*, const KnowledgeRecord*>;
146  using difference_type = size_t;
148  using pointer = value_type*;
149  using iterator_category = std::input_iterator_tag;
150 
156  {
157  if (biter_)
158  {
159  return {miter_->first, &**biter_};
160  }
161  return {miter_->first, miter_->second.get_record_unsafe()};
162  }
163 
168  {
169  if (biter_)
170  {
171  ++*biter_;
172  auto rec = miter_->second.get_record_unsafe();
173  auto cbuf = rec->share_circular_buffer();
174  if (cbuf && *biter_ != cbuf->cend())
175  {
176  return *this;
177  }
178  }
179 
180  ++miter_;
181  biter_ = begin_biter();
182  return *this;
183  }
184 
190  {
191  const_iterator ret(*this);
192  ++*this;
193  return ret;
194  }
195 
196  bool operator==(const const_iterator& o) const
197  {
198  return reader_ == o.reader_ && miter_ == o.miter_ &&
199  ((biter_ == nullptr && o.biter_ == nullptr) ||
200  (biter_ && o.biter_ && *biter_ == *o.biter_));
201  }
202 
203  bool operator!=(const const_iterator& o) const
204  {
205  return !(*this == o);
206  }
207 
208  private:
209  using miter_type = VariableReferenceMap::const_iterator;
211  using biter_uptr = std::unique_ptr<biter_type>;
212 
214  {
215  for (;;)
216  {
217  if (!reader_->history_ || miter_ == reader_->map_.end())
218  {
219  return nullptr;
220  }
221 
222  const KnowledgeRecord* rec = miter_->second.get_record_unsafe();
223 
224  if (!rec->has_history())
225  {
226  return nullptr;
227  }
228 
229  auto cbuf = rec->share_circular_buffer();
230  auto ret = cbuf->cbegin();
231 
232  if (reader_->min_toi_ > 0)
233  {
234  ret = std::upper_bound(ret, cbuf->cend(), reader_->min_toi_,
235  [](const uint64_t& lhs, const KnowledgeRecord& rhs) {
236  return lhs < rhs.toi();
237  });
238  }
239 
240  if (ret == cbuf->cend())
241  {
242  ++miter_;
243  continue;
244  }
245 
246  return into_unique(std::move(ret));
247  }
248  }
249 
252  {
253  }
254 
256  : reader_(&f), miter_(miter), biter_(begin_biter())
257  {
258  }
259 
260  public:
262  : reader_(o.reader_),
263  miter_(o.miter_),
264  biter_(o.biter_ ? into_unique(*o.biter_) : nullptr)
265  {
266  }
267 
269  {
270  biter_ = o.biter_ ? into_unique(*o.biter_) : nullptr;
271  reader_ = o.reader_;
272  miter_ = o.miter_;
273  return *this;
274  }
275 
278 
279  private:
282 
285 
287  biter_uptr biter_{nullptr};
288 
290  };
291 
293  {
294  return {*this};
295  }
296 
298  {
299  return {*this, map_.end()};
300  }
301 
302 private:
305 
307  bool history_ = false;
308 
310  uint64_t min_toi_ = 0;
311 };
312 }
313 }
314 
315 #include "VariableReference.inl"
316 
317 #endif // _MADARA_VARIABLE_REFERENCE_H_
This class provides a distributed knowledge base implementation.
This class encapsulates an entry in a KnowledgeBase.
bool has_history() const
Return true if this record has a circular buffer history.
std::shared_ptr< CircBuf > share_circular_buffer() const
Get a shared_ptr to the history buffer inside this record.
This class stores variables and their values for use by any entity needing state information in a thr...
Constant Input Iterator over values in the map.
value_type operator*() const
Gives a pair value (note, not a reference).
const VariableReferenceMapReader * reader_
Reader the iterator belongs to.
const_iterator & operator=(const_iterator &&o)=default
std::pair< const char *, const KnowledgeRecord * > value_type
const_iterator & operator++()
Increments iterator, to move onto the next record in the map.
const_iterator operator++(int)
Post-Increments iterator, to move onto the next record in the map.
biter_uptr biter_
Iterator over history of current map member, if any exists.
const_iterator(const VariableReferenceMapReader &f, miter_type miter)
Helper class for iterating over VariableReferenceMaps, optionally including history of changed record...
bool history_
True if iteration should include history.
VariableReferenceMapReader(const VariableReferenceMap &map, bool history=false, uint64_t min_toi=0)
Primary constructor.
const VariableReferenceMap & map_
Map to iterate over.
uint64_t min_toi_
Only iterate updates with TOI > this value.
Optimized reference to a variable within the knowledge base.
VariableReference()=default
Default constructor.
KnowledgeMap::value_type * pair_ptr
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
Const input iterator for elements of a CircularBuffer. Usual semantics.
Provides functions and classes for the distributed knowledge base.
auto operator==(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l)==r)
std::map< const char *, VariableReference, utility::ComparisonLessThan > VariableReferenceMap
a map of variable references
auto operator!=(const KnowledgeRecord &l, const T &r) -> decltype(knowledge_cast< T >(l) !=r)
std::vector< VariableReference > VariableReferences
a vector of variable references
std::unique_ptr< decay_< T > > into_unique(T &&val)
Converts a typed value into a unique_ptr of a decayed type.
Definition: StlHelper.h:93
Copyright(c) 2020 Galois.