MADARA  3.1.8
Double.cpp
Go to the documentation of this file.
1 #include "Double.h"
3 
4 
6  const KnowledgeUpdateSettings & settings)
7 : BaseContainer ("", settings), context_ (0)
8 {
9 }
10 
12  const std::string & name,
14  const KnowledgeUpdateSettings & settings)
15 : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
16 {
17  variable_ = knowledge.get_ref (name, settings_);
18 }
19 
21  const std::string & name,
23  const KnowledgeUpdateSettings & settings)
24 : BaseContainer (name, settings), context_ (knowledge.get_context ())
25 {
26  variable_ = knowledge.get_ref (name, settings_);
27 }
28 
30  const std::string & name,
32  type value,
33  const KnowledgeUpdateSettings & settings)
34 : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
35 {
36  variable_ = knowledge.get_ref (name);
37  context_->set (variable_, value, settings);
38 }
39 
41  const std::string & name,
43  type value,
44  const KnowledgeUpdateSettings & settings)
45 : BaseContainer (name, settings), context_ (knowledge.get_context ())
46 {
47  variable_ = knowledge.get_ref (name);
48  context_->set (variable_, value, settings);
49 }
50 
51 
53  : BaseContainer (rhs), context_ (rhs.context_),
54  variable_ (rhs.variable_)
55 {
56 
57 }
58 
59 
61 {
62 
63 }
64 
65 void
67 {
68  ContextGuard context_guard (*context_);
69  if (context_ && name_ != "")
70  {
72  }
73 }
74 
77 {
78  std::stringstream result;
79 
80  result << "Double: ";
81 
82  if (context_)
83  {
84  ContextGuard context_guard (*context_);
85  MADARA_GUARD_TYPE guard (mutex_);
86 
87  result << this->name_;
88  result << " = " << context_->get (variable_).to_string ();
89  }
90 
91  return result.str ();
92 }
93 
94 void
96 {
97  modify ();
98 }
99 
102 {
103  return get_debug_info ();
104 }
105 
108 {
109  return new Double (*this);
110 }
111 
112 void
114 {
115  if (this != &rhs)
116  {
117  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
118 
119  this->context_ = rhs.context_;
120  this->name_ = rhs.name_;
121  this->settings_ = rhs.settings_;
122  this->variable_ = rhs.variable_;
123  }
124 }
125 
126 void
128  containers::Double & other)
129 {
130  if (context_ && other.context_)
131  {
132  ContextGuard context_guard (*context_);
133  ContextGuard other_context_guard (*other.context_);
134  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
135 
136  type temp = *other;
137  other = **this;
138  *this = temp;
139  }
140 }
141 
142 void
144  const std::string & var_name,
146 {
147  KnowledgeUpdateSettings keep_local (true);
148  context_ = &(knowledge.get_context ());
149 
150  ContextGuard context_guard (*context_);
151  MADARA_GUARD_TYPE guard (mutex_);
152 
153  name_ = var_name;
154  variable_ = context_->get_ref (name_, keep_local);
155 }
156 
157 void
159  const std::string & var_name,
161 {
162  KnowledgeUpdateSettings keep_local (true);
163  context_ = knowledge.get_context ();
164 
165  ContextGuard context_guard (*context_);
166  MADARA_GUARD_TYPE guard (mutex_);
167 
168  name_ = var_name;
169  variable_ = context_->get_ref (name_, keep_local);
170 }
171 
172 void
174  const std::string & var_name,
176 {
177  KnowledgeUpdateSettings keep_local (true);
178  context_ = &knowledge;
179 
180  ContextGuard context_guard (*context_);
181  MADARA_GUARD_TYPE guard (mutex_);
182 
183  name_ = var_name;
184  variable_ = context_->get_ref (name_, keep_local);
185 }
186 
189 {
190  if (context_)
191  {
192  ContextGuard context_guard (*context_);
193  MADARA_GUARD_TYPE guard (mutex_);
194  context_->set (variable_, value, settings_);
195  }
196 
197  return value;
198 }
199 
202 {
204 
205  if (context_)
206  {
207  ContextGuard context_guard (*context_);
208  MADARA_GUARD_TYPE guard (mutex_);
209 
210  result = context_->get (variable_, settings_).to_double ();
211  result += value;
212  context_->set (variable_, result, settings_);
213  }
214 
215  return result;
216 }
217 
220 {
222 
223  if (context_)
224  {
225  ContextGuard context_guard (*context_);
226  MADARA_GUARD_TYPE guard (mutex_);
227 
228  result = context_->get (variable_, settings_).to_double ();
229  result -= value;
230  context_->set (variable_, result, settings_);
231  }
232 
233  return result;
234 }
237 {
238  if (context_)
239  {
240  ContextGuard context_guard (*context_);
241  MADARA_GUARD_TYPE guard (mutex_);
242  return context_->inc (variable_, settings_).to_double ();
243  }
244  else
245  return 0;
246 }
247 
250 {
251  if (context_)
252  {
253  ContextGuard context_guard (*context_);
254  MADARA_GUARD_TYPE guard (mutex_);
255  return context_->dec (variable_, settings_).to_double ();
256  }
257  else
258  return 0;
259 }
260 
261 bool
263 {
264  if (context_)
265  {
266  ContextGuard context_guard (*context_);
267  MADARA_GUARD_TYPE guard (mutex_);
268  return context_->get (variable_, settings_) == value;
269  }
270 
271  return false;
272 }
273 
274 bool
276 {
277  if (context_)
278  {
279  ContextGuard context_guard (*context_);
280  MADARA_GUARD_TYPE guard (mutex_);
281  return context_->get (variable_, settings_).to_double () != value;
282  }
283 
284  return true;
285 }
286 
287 bool
289  const Double & value) const
290 {
291  if (context_)
292  {
293  ContextGuard context_guard (*context_);
294  MADARA_GUARD_TYPE guard (mutex_);
295  return
297  value.context_->get (value.variable_, value.settings_);
298  }
299 
300  return false;
301 }
302 
303 bool
305  const Double & value) const
306 {
307  if (context_)
308  {
309  ContextGuard context_guard (*context_);
310  MADARA_GUARD_TYPE guard (mutex_);
311  return
313  value.context_->get (value.variable_, value.settings_);
314  }
315 
316  return true;
317 }
318 
319 bool
321 {
322  if (context_)
323  {
324  ContextGuard context_guard (*context_);
325  MADARA_GUARD_TYPE guard (mutex_);
326  return context_->get (variable_, settings_).to_double () < value;
327  }
328 
329  return false;
330 }
331 
332 bool
334 {
335  if (context_)
336  {
337  ContextGuard context_guard (*context_);
338  MADARA_GUARD_TYPE guard (mutex_);
339  return context_->get (variable_, settings_).to_double () <= value;
340  }
341 
342  return false;
343 }
344 
345 bool
347 {
348  if (context_)
349  {
350  ContextGuard context_guard (*context_);
351  MADARA_GUARD_TYPE guard (mutex_);
352  return context_->get (variable_, settings_).to_double () > value;
353  }
354 
355  return false;
356 }
357 
358 bool
360 {
361  if (context_)
362  {
363  ContextGuard context_guard (*context_);
364  MADARA_GUARD_TYPE guard (mutex_);
365  return context_->get (variable_, settings_).to_double () >= value;
366  }
367 
368  return false;
369 }
370 
373 {
374  return to_double ();
375 }
376 
377 bool
379 {
380  bool result (false);
381 
382  if (context_)
383  {
384  ContextGuard context_guard (*context_);
385  MADARA_GUARD_TYPE guard (mutex_);
386  result = context_->exists (variable_);
387  }
388 
389  return result;
390 }
391 
394 {
396 
397  if (context_)
398  {
399  ContextGuard context_guard (*context_);
400  MADARA_GUARD_TYPE guard (mutex_);
401  result = context_->get (variable_, settings_);
402  }
403 
404  return result;
405 }
406 
407 double
409 {
410  if (context_)
411  {
412  ContextGuard context_guard (*context_);
413  MADARA_GUARD_TYPE guard (mutex_);
414  return context_->get (variable_, settings_).to_double ();
415  }
416  else
417  return 0.0;
418 }
419 
422 {
423  if (context_)
424  {
425  ContextGuard context_guard (*context_);
426  MADARA_GUARD_TYPE guard (mutex_);
427  return context_->get (variable_, settings_).to_integer ();
428  }
429  else
430  return 0;
431 }
432 
435 {
436  if (context_)
437  {
438  ContextGuard context_guard (*context_);
439  MADARA_GUARD_TYPE guard (mutex_);
440  return context_->get (variable_, settings_).to_string ();
441  }
442  else
443  return "";
444 }
445 
446 void
448  uint32_t quality,
449  const KnowledgeReferenceSettings & settings)
450 {
451  if (context_)
452  {
453  ContextGuard context_guard (*context_);
454  MADARA_GUARD_TYPE guard (mutex_);
455  context_->set_quality (name_, quality, true, settings);
456  }
457 }
458 
459 bool
461 {
462  bool result (false);
463 
465  "Double::is_true: checking for non-zero value\n", (int)result);
466 
467  if (context_)
468  {
469  ContextGuard context_guard (*context_);
470  MADARA_GUARD_TYPE guard (mutex_);
471  result = context_->get (variable_, settings_).is_true ();
472  }
473 
475  "Double::is_true: final result is %d\n", (int)result);
476 
477  return result;
478 }
479 
480 bool
482 {
483  return !is_true ();
484 }
485 
486 
487 bool
489 {
490  return is_true ();
491 }
492 
493 bool
495 {
496  return is_false ();
497 }
This class encapsulates an entry in a KnowledgeBase.
This class stores a double within a variable context.
Definition: Double.h:32
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.
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer.
Definition: Double.cpp:421
bool is_true(void) const
Checks to see if the record is true.
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
Definition: Double.cpp:378
bool operator==(type value) const
Checks for equality.
Definition: Double.cpp:262
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Double.cpp:95
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
double to_double(void) const
converts the value to a float/double
std::string name_
Prefix of variable.
bool operator<(type value) const
Checks for less than relationship.
Definition: Double.cpp:320
type operator++(void)
Increments the value of the variable and returns the result.
Definition: Double.cpp:236
This class stores variables and their values for use by any entity needing state information in a thr...
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
Definition: Double.cpp:494
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Double.cpp:393
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.
void exchange(containers::Double &other)
Exchanges the integer at this location with the integer at another location.
Definition: Double.cpp:127
bool operator>(type value) const
Checks for greater than relationship.
Definition: Double.cpp:346
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Double.h:354
virtual ~Double()
Destructor.
Definition: Double.cpp:60
bool is_false(void) const
Determines if the value is zero.
Definition: Double.cpp:481
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
double type
trait that describes the value type
Definition: Double.h:36
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Double.cpp:76
type operator+=(type value)
Increments by a value.
Definition: Double.cpp:201
type operator-=(type value)
Decrements by a value.
Definition: Double.cpp:219
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
bool operator!=(type value) const
Checks for inequality.
Definition: Double.cpp:275
type operator*(void) const
Returns the value of the variable.
Definition: Double.cpp:372
VariableReference variable_
Variable reference.
Definition: Double.h:359
Double(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Double.cpp:5
std::string to_string(void) const
Returns the value as a string.
Definition: Double.cpp:434
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
Definition: Double.cpp:447
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Double.cpp:333
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Double.cpp:359
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Double.cpp:101
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
bool is_true(void) const
Determines if the value is true.
Definition: Double.cpp:460
type operator--(void)
Decrements the value of the variable and returns the result.
Definition: Double.cpp:249
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.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
Definition: Double.cpp:488
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
Provides an interface for external functions into the MADARA KaRL variable settings.
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Double.cpp:107
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
void operator=(const Double &rhs)
Assignment operator.
Definition: Double.cpp:113
void modify(void)
Mark the value as modified.
Definition: Double.cpp:66
double to_double(void) const
Returns the value as a double (alias of *)
Definition: Double.cpp:408
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Double.cpp:143
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the 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...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.