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