MADARA  3.1.8
Integer.inl
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_CONTAINERS_INTEGER_INL_
2 #define _MADARA_KNOWLEDGE_CONTAINERS_INTEGER_INL_
3 
4 
5 #include "Integer.h"
7 
8 inline void
10 {
11  ContextGuard context_guard (*context_);
12  if (context_ && name_ != "")
13  {
15  }
16 }
17 
18 inline std::string
20 {
21  std::stringstream result;
22 
23  result << "Integer: ";
24 
25  if (context_)
26  {
27  ContextGuard context_guard (*context_);
28  MADARA_GUARD_TYPE guard (mutex_);
29 
30  result << this->name_;
31  result << " = " << context_->get (variable_).to_string ();
32  }
33 
34  return result.str ();
35 }
36 
39 {
40  return new Integer (*this);
41 }
42 
43 inline void
45 {
46  if (this != &rhs)
47  {
48  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
49 
50  this->context_ = rhs.context_;
51  this->name_ = rhs.name_;
52  this->settings_ = rhs.settings_;
53  this->variable_ = rhs.variable_;
54  }
55 }
56 
57 inline void
59  const std::string & var_name,
61 {
62  KnowledgeUpdateSettings keep_local (true);
63  context_ = &(knowledge.get_context ());
64 
65  ContextGuard context_guard (*context_);
66  MADARA_GUARD_TYPE guard (mutex_);
67 
68  name_ = var_name;
69  variable_ = context_->get_ref (name_, keep_local);
70 }
71 
72 inline void
74  const std::string & var_name,
76 {
77  KnowledgeUpdateSettings keep_local (true);
78  context_ = &knowledge;
79 
80  ContextGuard context_guard (*context_);
81  MADARA_GUARD_TYPE guard (mutex_);
82 
83  name_ = var_name;
84  variable_ = context_->get_ref (name_, keep_local);
85 }
86 
87 inline void
89  const std::string & var_name,
91 {
92  KnowledgeUpdateSettings keep_local (true);
93  context_ = knowledge.get_context ();
94 
95  ContextGuard context_guard (*context_);
96  MADARA_GUARD_TYPE guard (mutex_);
97 
98  name_ = var_name;
99  variable_ = context_->get_ref (name_, keep_local);
100 }
101 
104 {
105  if (context_)
106  {
107  ContextGuard context_guard (*context_);
108  MADARA_GUARD_TYPE guard (mutex_);
109  context_->set (variable_, value, settings_);
110  }
111 
112  return value;
113 }
114 
115 inline bool
117 {
118  if (context_)
119  {
120  ContextGuard context_guard (*context_);
121  MADARA_GUARD_TYPE guard (mutex_);
122  return context_->get (variable_, settings_) == value;
123  }
124 
125  return false;
126 }
127 
128 inline bool
130 {
131  if (context_)
132  {
133  ContextGuard context_guard (*context_);
134  MADARA_GUARD_TYPE guard (mutex_);
135  return context_->get (variable_, settings_).to_integer () != value;
136  }
137 
138  return true;
139 }
140 
141 inline bool
143  const Integer & value) const
144 {
145  if (context_)
146  {
147  ContextGuard context_guard (*context_);
148  MADARA_GUARD_TYPE guard (mutex_);
149  return
151  value.context_->get (value.variable_, value.settings_);
152  }
153 
154  return false;
155 }
156 
157 inline bool
159  const Integer & value) const
160 {
161  if (context_)
162  {
163  ContextGuard context_guard (*context_);
164  MADARA_GUARD_TYPE guard (mutex_);
165  return
167  value.context_->get (value.variable_, value.settings_);
168  }
169 
170  return true;
171 }
172 
173 inline bool
175 {
176  if (context_)
177  {
178  ContextGuard context_guard (*context_);
179  MADARA_GUARD_TYPE guard (mutex_);
180  return context_->get (variable_, settings_).to_integer() < value;
181  }
182 
183  return false;
184 }
185 
186 inline bool
188 {
189  if (context_)
190  {
191  ContextGuard context_guard (*context_);
192  MADARA_GUARD_TYPE guard (mutex_);
193  return context_->get (variable_, settings_).to_integer() <= value;
194  }
195 
196  return false;
197 }
198 
199 inline bool
201 {
202  if (context_)
203  {
204  ContextGuard context_guard (*context_);
205  MADARA_GUARD_TYPE guard (mutex_);
206  return context_->get (variable_, settings_).to_integer() > value;
207  }
208 
209  return false;
210 }
211 
212 inline bool
214 {
215  if (context_)
216  {
217  ContextGuard context_guard (*context_);
218  MADARA_GUARD_TYPE guard (mutex_);
219  return context_->get (variable_, settings_).to_integer() >= value;
220  }
221 
222  return false;
223 }
224 
227 {
228  return to_integer ();
229 }
230 
231 inline bool
233 {
234  bool result (false);
235 
236  if (context_)
237  {
238  ContextGuard context_guard (*context_);
239  MADARA_GUARD_TYPE guard (mutex_);
240  result = context_->exists (variable_);
241  }
242 
243  return result;
244 }
245 
246 inline bool
248 {
249  bool result (false);
250 
252  "Integer::is_true: checking for non-zero value\n");
253 
254  if (context_)
255  {
256  ContextGuard context_guard (*context_);
257  MADARA_GUARD_TYPE guard (mutex_);
258  result = context_->get (variable_, settings_).is_true ();
259  }
260 
262  "Integer::is_true: final result is %d\n", (int)result);
263 
264  return result;
265 }
266 
267 inline bool
269 {
270  bool result (true);
271 
273  "Integer::is_false: checking for zero value\n");
274 
275  if (context_)
276  {
277  ContextGuard context_guard (*context_);
278  MADARA_GUARD_TYPE guard (mutex_);
279  result = context_->get (variable_, settings_).is_false ();
280  }
281 
283  "Integer::is_false: final result is %d\n", (int)result);
284 
285  return result;
286 }
287 
290 {
292 
293  if (context_)
294  {
295  ContextGuard context_guard (*context_);
296  MADARA_GUARD_TYPE guard (mutex_);
297  result = context_->get (variable_, settings_);
298  }
299 
300  return result;
301 }
302 
305 {
306  if (context_)
307  {
308  ContextGuard context_guard (*context_);
309  MADARA_GUARD_TYPE guard (mutex_);
310  return context_->get (variable_, settings_).to_integer ();
311  }
312  else
313  return 0;
314 }
315 
318 {
320 
321  if (context_)
322  {
323  ContextGuard context_guard (*context_);
324  MADARA_GUARD_TYPE guard (mutex_);
325 
326  result = context_->get (variable_, settings_).to_integer ();
327  result += value;
328  context_->set (variable_, result, settings_);
329  }
330 
331  return result;
332 }
333 
336 {
338 
339  if (context_)
340  {
341  ContextGuard context_guard (*context_);
342  MADARA_GUARD_TYPE guard (mutex_);
343 
344  result = context_->get (variable_, settings_).to_integer ();
345  result -= value;
346  context_->set (variable_, result, settings_);
347  }
348 
349  return result;
350 }
351 
354 {
355  if (context_)
356  {
357  ContextGuard context_guard (*context_);
358  MADARA_GUARD_TYPE guard (mutex_);
359  return context_->inc (variable_, settings_).to_integer ();
360  }
361  else
362  return 0;
363 }
364 
367 {
368  if (context_)
369  {
370  ContextGuard context_guard (*context_);
371  MADARA_GUARD_TYPE guard (mutex_);
372  return context_->dec (variable_, settings_).to_integer ();
373  }
374  else
375  return 0;
376 }
377 
378 inline double
380 {
381  if (context_)
382  {
383  ContextGuard context_guard (*context_);
384  MADARA_GUARD_TYPE guard (mutex_);
385  return context_->get (variable_, settings_).to_double ();
386  }
387  else
388  return 0.0;
389 }
390 
391 inline std::string
393 {
394  if (context_)
395  {
396  ContextGuard context_guard (*context_);
397  MADARA_GUARD_TYPE guard (mutex_);
398  return context_->get (variable_, settings_).to_string ();
399  }
400  else
401  return "";
402 }
403 
404 inline void
406  uint32_t quality,
407  const KnowledgeReferenceSettings & settings)
408 {
409  if (context_)
410  {
411  ContextGuard context_guard (*context_);
412  MADARA_GUARD_TYPE guard (mutex_);
413  context_->set_quality (name_, quality, true, settings);
414  }
415 }
416 
417 #endif // _MADARA_KNOWLEDGE_CONTAINERS_INTEGER_INL_
This class encapsulates an entry in a KnowledgeBase.
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool is_true(void) const
Checks to see if the record is true.
double to_double(void) const
Returns the value as a double.
Definition: Integer.inl:379
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Integer.inl:58
Integer(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Integer.cpp:4
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Integer.inl:213
std::string to_string(void) const
Returns the value as a string.
Definition: Integer.inl:392
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Integer.inl:19
double to_double(void) const
converts the value to a float/double
std::string name_
Prefix of variable.
void modify(void)
Mark the value as modified.
Definition: Integer.inl:9
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Integer.inl:289
type operator--(void)
Decrements the value of the variable and returns the result.
Definition: Integer.inl:366
VariableReference variable_
Variable reference.
Definition: Integer.h:358
This class stores variables and their values for use by any entity needing state information in a thr...
bool operator<(type value) const
Checks for less than relationship.
Definition: Integer.inl:174
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Integer.inl:38
MADARA_LOCK_TYPE mutex_
guard for access and changes
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
type operator*(void) const
Returns the value of the variable.
Definition: Integer.inl:226
bool operator!=(const Integer &value) const
Checks for inequality.
Definition: Integer.inl:158
type operator++(void)
Increments the value of the variable and returns the result.
Definition: Integer.inl:353
bool is_true(void) const
Determines if the value is true.
Definition: Integer.inl:247
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
void operator=(const Integer &rhs)
Assignment operator.
Definition: Integer.inl:44
bool is_false(void) const
Determines if the value is zero.
Definition: Integer.inl:268
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
Definition: Integer.inl:405
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
This class stores an integer within a variable context.
Definition: Integer.h:31
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
type operator-=(type value)
Decrements by a value.
Definition: Integer.inl:335
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: Integer.h:35
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Integer.h:353
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Integer.inl:187
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
Definition: Integer.inl:232
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
bool operator==(const Integer &value) const
Checks for equality.
Definition: Integer.inl:142
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
bool operator>(type value) const
Checks for greater than relationship.
Definition: Integer.inl:200
type operator+=(type value)
Increments by a value.
Definition: Integer.inl:317
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
Definition: Integer.inl:304