MADARA  3.4.1
Double.cpp
Go to the documentation of this file.
1 #include "Double.h"
3 
5  const KnowledgeUpdateSettings& settings)
6  : BaseContainer("", settings), context_(0)
7 {
8 }
9 
12  : BaseContainer(name, settings), context_(&(knowledge.get_context()))
13 {
14  variable_ = knowledge.get_ref(name, settings_);
15 }
16 
19  : BaseContainer(name, settings), context_(knowledge.get_context())
20 {
21  variable_ = knowledge.get_ref(name, settings_);
22 }
23 
26  const KnowledgeUpdateSettings& settings)
27  : BaseContainer(name, settings), context_(&(knowledge.get_context()))
28 {
29  variable_ = knowledge.get_ref(name);
30  context_->set(variable_, value, settings);
31 }
32 
34  Variables& knowledge, type value, const KnowledgeUpdateSettings& settings)
35  : BaseContainer(name, settings), context_(knowledge.get_context())
36 {
37  variable_ = knowledge.get_ref(name);
38  context_->set(variable_, value, settings);
39 }
40 
42  : BaseContainer(rhs), context_(rhs.context_), variable_(rhs.variable_)
43 {
44 }
45 
47 
49 {
50  ContextGuard context_guard(*context_);
51  if (context_ && name_ != "")
52  {
53  context_->mark_modified(variable_);
54  }
55 }
56 
58 {
59  std::stringstream result;
60 
61  result << "Double: ";
62 
63  if (context_)
64  {
65  ContextGuard context_guard(*context_);
66  MADARA_GUARD_TYPE guard(mutex_);
67 
68  result << this->name_;
69  result << " = " << context_->get(variable_).to_string();
70  }
71 
72  return result.str();
73 }
74 
76 {
77  modify();
78 }
79 
81 {
82  return get_debug_info();
83 }
84 
87 {
88  return new Double(*this);
89 }
90 
92 {
93  if (this != &rhs)
94  {
95  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
96 
97  this->context_ = rhs.context_;
98  this->name_ = rhs.name_;
99  this->settings_ = rhs.settings_;
100  this->variable_ = rhs.variable_;
101  }
102 }
103 
105 {
106  if (context_ && other.context_)
107  {
108  std::lock(*context_, *other.context_, mutex_, other.mutex_);
109 
110  ContextGuard context_guard(*context_, std::adopt_lock);
111  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
112  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
113  guard2(other.mutex_, std::adopt_lock);
114 
115  type temp = *other;
116  other = **this;
117  *this = temp;
118  }
119 }
120 
122  const std::string& var_name, KnowledgeBase& knowledge)
123 {
124  KnowledgeUpdateSettings keep_local(true);
125  context_ = &(knowledge.get_context());
126 
127  ContextGuard context_guard(*context_);
128  MADARA_GUARD_TYPE guard(mutex_);
129 
130  name_ = var_name;
131  variable_ = context_->get_ref(name_, keep_local);
132 }
133 
135  const std::string& var_name, Variables& knowledge)
136 {
137  KnowledgeUpdateSettings keep_local(true);
138  context_ = knowledge.get_context();
139 
140  ContextGuard context_guard(*context_);
141  MADARA_GUARD_TYPE guard(mutex_);
142 
143  name_ = var_name;
144  variable_ = context_->get_ref(name_, keep_local);
145 }
146 
148  const std::string& var_name, ThreadSafeContext& knowledge)
149 {
150  KnowledgeUpdateSettings keep_local(true);
151  context_ = &knowledge;
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 
162 {
163  if (context_)
164  {
165  ContextGuard context_guard(*context_);
166  MADARA_GUARD_TYPE guard(mutex_);
167  context_->set(variable_, value, settings_);
168  }
169 
170  return value;
171 }
172 
175 {
177 
178  if (context_)
179  {
180  ContextGuard context_guard(*context_);
181  MADARA_GUARD_TYPE guard(mutex_);
182 
183  result = context_->get(variable_, settings_).to_double();
184  result += value;
185  context_->set(variable_, result, settings_);
186  }
187 
188  return result;
189 }
190 
193 {
195 
196  if (context_)
197  {
198  ContextGuard context_guard(*context_);
199  MADARA_GUARD_TYPE guard(mutex_);
200 
201  result = context_->get(variable_, settings_).to_double();
202  result -= value;
203  context_->set(variable_, result, settings_);
204  }
205 
206  return result;
207 }
210 {
211  if (context_)
212  {
213  ContextGuard context_guard(*context_);
214  MADARA_GUARD_TYPE guard(mutex_);
215  return context_->inc(variable_, settings_).to_double();
216  }
217  else
218  return 0;
219 }
220 
223 {
224  if (context_)
225  {
226  ContextGuard context_guard(*context_);
227  MADARA_GUARD_TYPE guard(mutex_);
228  return context_->dec(variable_, settings_).to_double();
229  }
230  else
231  return 0;
232 }
233 
235 {
236  if (context_)
237  {
238  ContextGuard context_guard(*context_);
239  MADARA_GUARD_TYPE guard(mutex_);
240  return context_->get(variable_, settings_).to_double() == value;
241  }
242 
243  return false;
244 }
245 
247 {
248  if (context_)
249  {
250  ContextGuard context_guard(*context_);
251  MADARA_GUARD_TYPE guard(mutex_);
252  return context_->get(variable_, settings_).to_double() != value;
253  }
254 
255  return true;
256 }
257 
259  const Double& value) const
260 {
261  if (context_)
262  {
263  ContextGuard context_guard(*context_);
264  MADARA_GUARD_TYPE guard(mutex_);
265  return context_->get(variable_, settings_) ==
266  value.context_->get(value.variable_, value.settings_);
267  }
268 
269  return false;
270 }
271 
273  const Double& value) const
274 {
275  if (context_)
276  {
277  ContextGuard context_guard(*context_);
278  MADARA_GUARD_TYPE guard(mutex_);
279  return context_->get(variable_, settings_) !=
280  value.context_->get(value.variable_, value.settings_);
281  }
282 
283  return true;
284 }
285 
287 {
288  if (context_)
289  {
290  ContextGuard context_guard(*context_);
291  MADARA_GUARD_TYPE guard(mutex_);
292  return context_->get(variable_, settings_).to_double() < value;
293  }
294 
295  return false;
296 }
297 
299 {
300  if (context_)
301  {
302  ContextGuard context_guard(*context_);
303  MADARA_GUARD_TYPE guard(mutex_);
304  return context_->get(variable_, settings_).to_double() <= value;
305  }
306 
307  return false;
308 }
309 
311 {
312  if (context_)
313  {
314  ContextGuard context_guard(*context_);
315  MADARA_GUARD_TYPE guard(mutex_);
316  return context_->get(variable_, settings_).to_double() > value;
317  }
318 
319  return false;
320 }
321 
323 {
324  if (context_)
325  {
326  ContextGuard context_guard(*context_);
327  MADARA_GUARD_TYPE guard(mutex_);
328  return context_->get(variable_, settings_).to_double() >= value;
329  }
330 
331  return false;
332 }
333 
336 {
337  return to_double();
338 }
339 
341 {
342  bool result(false);
343 
344  if (context_)
345  {
346  ContextGuard context_guard(*context_);
347  MADARA_GUARD_TYPE guard(mutex_);
348  result = context_->exists(variable_);
349  }
350 
351  return result;
352 }
353 
356 {
358 
359  if (context_)
360  {
361  ContextGuard context_guard(*context_);
362  MADARA_GUARD_TYPE guard(mutex_);
363  result = context_->get(variable_, settings_);
364  }
365 
366  return result;
367 }
368 
370 {
371  if (context_)
372  {
373  ContextGuard context_guard(*context_);
374  MADARA_GUARD_TYPE guard(mutex_);
375  return context_->get(variable_, settings_).to_double();
376  }
377  else
378  return 0.0;
379 }
380 
383 {
384  if (context_)
385  {
386  ContextGuard context_guard(*context_);
387  MADARA_GUARD_TYPE guard(mutex_);
388  return context_->get(variable_, settings_).to_integer();
389  }
390  else
391  return 0;
392 }
393 
395 {
396  if (context_)
397  {
398  ContextGuard context_guard(*context_);
399  MADARA_GUARD_TYPE guard(mutex_);
400  return context_->get(variable_, settings_).to_string();
401  }
402  else
403  return "";
404 }
405 
407  uint32_t quality, 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 
418 {
419  bool result(false);
420 
422  "Double::is_true: checking for non-zero value\n", (int)result);
423 
424  if (context_)
425  {
426  ContextGuard context_guard(*context_);
427  MADARA_GUARD_TYPE guard(mutex_);
428  result = context_->get(variable_, settings_).is_true();
429  }
430 
432  "Double::is_true: final result is %d\n", (int)result);
433 
434  return result;
435 }
436 
438 {
439  return !is_true();
440 }
441 
443 {
444  return is_true();
445 }
446 
448 {
449  return is_false();
450 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
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.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
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 a double within a variable context.
Definition: Double.h:33
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Double.cpp:75
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
Definition: Double.cpp:406
double to_double(void) const
Returns the value as a double (alias of *)
Definition: Double.cpp:369
void modify(void)
Mark the value as modified.
Definition: Double.cpp:48
type operator-=(type value)
Decrements by a value.
Definition: Double.cpp:192
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Double.cpp:121
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Double.cpp:57
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Double.cpp:355
type operator*(void) const
Returns the value of the variable.
Definition: Double.cpp:335
type operator+=(type value)
Increments by a value.
Definition: Double.cpp:174
type operator++(void)
Increments the value of the variable and returns the result.
Definition: Double.cpp:209
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Double.cpp:322
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:442
bool is_true(void) const
Determines if the value is true.
Definition: Double.cpp:417
double type
trait that describes the value type
Definition: Double.h:36
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Double.cpp:80
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer.
Definition: Double.cpp:382
bool operator>(type value) const
Checks for greater than relationship.
Definition: Double.cpp:310
bool operator!=(type value) const
Checks for inequality.
Definition: Double.cpp:246
bool is_false(void) const
Determines if the value is zero.
Definition: Double.cpp:437
void operator=(const Double &rhs)
Assignment operator.
Definition: Double.cpp:91
VariableReference variable_
Variable reference.
Definition: Double.h:343
bool operator==(type value) const
Checks for equality.
Definition: Double.cpp:234
void exchange(containers::Double &other)
Exchanges the integer at this location with the integer at another location.
Definition: Double.cpp:104
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Double.h:338
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:447
type operator--(void)
Decrements the value of the variable and returns the result.
Definition: Double.cpp:222
std::string to_string(void) const
Returns the value as a string.
Definition: Double.cpp:394
virtual ~Double()
Destructor.
Definition: Double.cpp:46
Double(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Double.cpp:4
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
Definition: Double.cpp:340
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Double.cpp:86
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Double.cpp:298
bool operator<(type value) const
Checks for less than relationship.
Definition: Double.cpp:286
constexpr string_t string
Provides functions and classes for the distributed knowledge base.