MADARA  3.2.3
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  std::lock(*context_, *other.context_, mutex_, other.mutex_);
133 
134  ContextGuard context_guard (*context_, std::adopt_lock);
135  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
136  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
137  guard2 (other.mutex_, std::adopt_lock);
138 
139  type temp = *other;
140  other = **this;
141  *this = temp;
142  }
143 }
144 
145 void
147  const std::string & var_name,
149 {
150  KnowledgeUpdateSettings keep_local (true);
151  context_ = &(knowledge.get_context ());
152 
153  ContextGuard context_guard (*context_);
154  MADARA_GUARD_TYPE guard (mutex_);
155 
156  name_ = var_name;
157  variable_ = context_->get_ref (name_, keep_local);
158 }
159 
160 void
162  const std::string & var_name,
164 {
165  KnowledgeUpdateSettings keep_local (true);
166  context_ = knowledge.get_context ();
167 
168  ContextGuard context_guard (*context_);
169  MADARA_GUARD_TYPE guard (mutex_);
170 
171  name_ = var_name;
172  variable_ = context_->get_ref (name_, keep_local);
173 }
174 
175 void
177  const std::string & var_name,
179 {
180  KnowledgeUpdateSettings keep_local (true);
181  context_ = &knowledge;
182 
183  ContextGuard context_guard (*context_);
184  MADARA_GUARD_TYPE guard (mutex_);
185 
186  name_ = var_name;
187  variable_ = context_->get_ref (name_, keep_local);
188 }
189 
192 {
193  if (context_)
194  {
195  ContextGuard context_guard (*context_);
196  MADARA_GUARD_TYPE guard (mutex_);
197  context_->set (variable_, value, settings_);
198  }
199 
200  return value;
201 }
202 
205 {
207 
208  if (context_)
209  {
210  ContextGuard context_guard (*context_);
211  MADARA_GUARD_TYPE guard (mutex_);
212 
213  result = context_->get (variable_, settings_).to_double ();
214  result += value;
215  context_->set (variable_, result, settings_);
216  }
217 
218  return result;
219 }
220 
223 {
225 
226  if (context_)
227  {
228  ContextGuard context_guard (*context_);
229  MADARA_GUARD_TYPE guard (mutex_);
230 
231  result = context_->get (variable_, settings_).to_double ();
232  result -= value;
233  context_->set (variable_, result, settings_);
234  }
235 
236  return result;
237 }
240 {
241  if (context_)
242  {
243  ContextGuard context_guard (*context_);
244  MADARA_GUARD_TYPE guard (mutex_);
245  return context_->inc (variable_, settings_).to_double ();
246  }
247  else
248  return 0;
249 }
250 
253 {
254  if (context_)
255  {
256  ContextGuard context_guard (*context_);
257  MADARA_GUARD_TYPE guard (mutex_);
258  return context_->dec (variable_, settings_).to_double ();
259  }
260  else
261  return 0;
262 }
263 
264 bool
266 {
267  if (context_)
268  {
269  ContextGuard context_guard (*context_);
270  MADARA_GUARD_TYPE guard (mutex_);
271  return context_->get (variable_, settings_) == value;
272  }
273 
274  return false;
275 }
276 
277 bool
279 {
280  if (context_)
281  {
282  ContextGuard context_guard (*context_);
283  MADARA_GUARD_TYPE guard (mutex_);
284  return context_->get (variable_, settings_).to_double () != value;
285  }
286 
287  return true;
288 }
289 
290 bool
292  const Double & value) const
293 {
294  if (context_)
295  {
296  ContextGuard context_guard (*context_);
297  MADARA_GUARD_TYPE guard (mutex_);
298  return
300  value.context_->get (value.variable_, value.settings_);
301  }
302 
303  return false;
304 }
305 
306 bool
308  const Double & value) const
309 {
310  if (context_)
311  {
312  ContextGuard context_guard (*context_);
313  MADARA_GUARD_TYPE guard (mutex_);
314  return
316  value.context_->get (value.variable_, value.settings_);
317  }
318 
319  return true;
320 }
321 
322 bool
324 {
325  if (context_)
326  {
327  ContextGuard context_guard (*context_);
328  MADARA_GUARD_TYPE guard (mutex_);
329  return context_->get (variable_, settings_).to_double () < value;
330  }
331 
332  return false;
333 }
334 
335 bool
337 {
338  if (context_)
339  {
340  ContextGuard context_guard (*context_);
341  MADARA_GUARD_TYPE guard (mutex_);
342  return context_->get (variable_, settings_).to_double () <= value;
343  }
344 
345  return false;
346 }
347 
348 bool
350 {
351  if (context_)
352  {
353  ContextGuard context_guard (*context_);
354  MADARA_GUARD_TYPE guard (mutex_);
355  return context_->get (variable_, settings_).to_double () > value;
356  }
357 
358  return false;
359 }
360 
361 bool
363 {
364  if (context_)
365  {
366  ContextGuard context_guard (*context_);
367  MADARA_GUARD_TYPE guard (mutex_);
368  return context_->get (variable_, settings_).to_double () >= value;
369  }
370 
371  return false;
372 }
373 
376 {
377  return to_double ();
378 }
379 
380 bool
382 {
383  bool result (false);
384 
385  if (context_)
386  {
387  ContextGuard context_guard (*context_);
388  MADARA_GUARD_TYPE guard (mutex_);
389  result = context_->exists (variable_);
390  }
391 
392  return result;
393 }
394 
397 {
399 
400  if (context_)
401  {
402  ContextGuard context_guard (*context_);
403  MADARA_GUARD_TYPE guard (mutex_);
404  result = context_->get (variable_, settings_);
405  }
406 
407  return result;
408 }
409 
410 double
412 {
413  if (context_)
414  {
415  ContextGuard context_guard (*context_);
416  MADARA_GUARD_TYPE guard (mutex_);
417  return context_->get (variable_, settings_).to_double ();
418  }
419  else
420  return 0.0;
421 }
422 
425 {
426  if (context_)
427  {
428  ContextGuard context_guard (*context_);
429  MADARA_GUARD_TYPE guard (mutex_);
430  return context_->get (variable_, settings_).to_integer ();
431  }
432  else
433  return 0;
434 }
435 
438 {
439  if (context_)
440  {
441  ContextGuard context_guard (*context_);
442  MADARA_GUARD_TYPE guard (mutex_);
443  return context_->get (variable_, settings_).to_string ();
444  }
445  else
446  return "";
447 }
448 
449 void
451  uint32_t quality,
452  const KnowledgeReferenceSettings & settings)
453 {
454  if (context_)
455  {
456  ContextGuard context_guard (*context_);
457  MADARA_GUARD_TYPE guard (mutex_);
458  context_->set_quality (name_, quality, true, settings);
459  }
460 }
461 
462 bool
464 {
465  bool result (false);
466 
468  "Double::is_true: checking for non-zero value\n", (int)result);
469 
470  if (context_)
471  {
472  ContextGuard context_guard (*context_);
473  MADARA_GUARD_TYPE guard (mutex_);
474  result = context_->get (variable_, settings_).is_true ();
475  }
476 
478  "Double::is_true: final result is %d\n", (int)result);
479 
480  return result;
481 }
482 
483 bool
485 {
486  return !is_true ();
487 }
488 
489 
490 bool
492 {
493  return is_true ();
494 }
495 
496 bool
498 {
499  return is_false ();
500 }
This class encapsulates an entry in a KnowledgeBase.
This class stores a double within a variable context.
Definition: Double.h:32
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:424
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:381
bool operator==(type value) const
Checks for equality.
Definition: Double.cpp:265
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
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:323
type operator++(void)
Increments the value of the variable and returns the result.
Definition: Double.cpp:239
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:497
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Double.cpp:396
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:349
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:484
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
static struct madara::knowledge::tags::string_t string
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
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:204
type operator-=(type value)
Decrements by a value.
Definition: Double.cpp:222
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
bool operator!=(type value) const
Checks for inequality.
Definition: Double.cpp:278
type operator*(void) const
Returns the value of the variable.
Definition: Double.cpp:375
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:437
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
Definition: Double.cpp:450
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Double.cpp:336
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Double.cpp:362
Integer to_integer(void) const
converts the value to an integer.
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.
Settings for applying knowledge updates.
bool is_true(void) const
Determines if the value is true.
Definition: Double.cpp:463
type operator--(void)
Decrements the value of the variable and returns the result.
Definition: Double.cpp:252
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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:491
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:411
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Double.cpp:146
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.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.