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