MADARA  3.2.3
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 
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  settings_ = settings;
19 }
20 
22  const std::string & name,
24  const KnowledgeUpdateSettings & settings)
25 : BaseContainer (name, settings), context_ (knowledge.get_context ())
26 {
27  variable_ = knowledge.get_ref (name, settings_);
28  settings_ = settings;
29 }
30 
32  const std::string & name,
34  const std::string & value,
35  const KnowledgeUpdateSettings & settings)
36 : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
37 {
38  variable_ = knowledge.get_ref (name);
39  knowledge.set (variable_, value);
40 }
41 
43  const std::string & name,
45  const std::string & value,
46  const KnowledgeUpdateSettings & settings)
47 : BaseContainer (name, settings), context_ (knowledge.get_context ())
48 {
49  variable_ = knowledge.get_ref (name);
50  knowledge.set (variable_, value);
51 }
52 
53 
56 {
57 
58 }
59 
60 
62 {
63 
64 }
65 
66 void
68 {
69  ContextGuard context_guard (*context_);
70  if (context_ && name_ != "")
71  {
73  }
74 }
75 
78 {
79  std::stringstream result;
80 
81  result << "String: ";
82 
83  if (context_)
84  {
85  ContextGuard context_guard (*context_);
86  MADARA_GUARD_TYPE guard (mutex_);
87 
88  result << this->name_;
89  result << " = " << context_->get (variable_).to_string ();
90  }
91 
92  return result.str ();
93 }
94 
95 void
97 {
98  modify ();
99 }
100 
103 {
104  return get_debug_info ();
105 }
106 
109 {
110  return new String (*this);
111 }
112 
113 void
115 {
116  if (this != &rhs)
117  {
118  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
119 
120  this->context_ = rhs.context_;
121  this->name_ = rhs.name_;
122  this->settings_ = rhs.settings_;
123  this->variable_ = rhs.variable_;
124  }
125 }
126 
127 void
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 
140  type temp = *other;
141  other = **this;
142  *this = temp;
143  }
144 }
145 
146 void
148  const std::string & var_name,
150 {
151  KnowledgeUpdateSettings keep_local (true);
152  context_ = &(knowledge.get_context ());
153 
154  ContextGuard context_guard (*context_);
155  MADARA_GUARD_TYPE guard (mutex_);
156 
157  name_ = var_name;
158  variable_ = context_->get_ref (name_, keep_local);
159 }
160 
161 void
163  const std::string & var_name,
165 {
166  KnowledgeUpdateSettings keep_local (true);
167  context_ = knowledge.get_context ();
168 
169  ContextGuard context_guard (*context_);
170  MADARA_GUARD_TYPE guard (mutex_);
171 
172  name_ = var_name;
173  variable_ = context_->get_ref (name_, keep_local);
174 }
175 
176 void
178  const std::string & var_name,
180 {
181  KnowledgeUpdateSettings keep_local (true);
182  context_ = &knowledge;
183 
184  ContextGuard context_guard (*context_);
185  MADARA_GUARD_TYPE guard (mutex_);
186 
187  name_ = var_name;
188  variable_ = context_->get_ref (name_, keep_local);
189 }
190 
193 {
194  if (context_)
195  {
196  ContextGuard context_guard (*context_);
197  MADARA_GUARD_TYPE guard (mutex_);
198  context_->set (variable_, value, settings_);
199  }
200 
201  return value;
202 }
203 
206 {
208 
209  if (context_)
210  {
211  ContextGuard context_guard (*context_);
212  MADARA_GUARD_TYPE guard (mutex_);
213 
214  result = context_->get (variable_, settings_).to_string ();
215  result += value;
216  context_->set (variable_, result, settings_);
217  }
218 
219  return result;
220 }
221 
222 bool
224 {
225  if (context_)
226  {
227  ContextGuard context_guard (*context_);
228  MADARA_GUARD_TYPE guard (mutex_);
229  return context_->get (variable_, settings_) == value;
230  }
231 
232  return false;
233 }
234 
235 bool
237 {
238  if (context_)
239  {
240  ContextGuard context_guard (*context_);
241  MADARA_GUARD_TYPE guard (mutex_);
242  return context_->get (variable_, settings_).to_string () != value;
243  }
244 
245  return true;
246 }
247 
248 bool
250  const String & value) const
251 {
252  if (context_)
253  {
254  ContextGuard context_guard (*context_);
255  MADARA_GUARD_TYPE guard (mutex_);
256  return
258  value.context_->get (value.variable_, value.settings_);
259  }
260 
261  return false;
262 }
263 
264 bool
266  const String & value) const
267 {
268  if (context_)
269  {
270  ContextGuard context_guard (*context_);
271  MADARA_GUARD_TYPE guard (mutex_);
272  return
274  value.context_->get (value.variable_, value.settings_);
275  }
276 
277  return true;
278 }
279 
280 bool
282 {
283  if (context_)
284  {
285  ContextGuard context_guard (*context_);
286  MADARA_GUARD_TYPE guard (mutex_);
287  return context_->get (variable_, settings_).to_string () < value;
288  }
289 
290  return false;
291 }
292 
293 bool
295 {
296  if (context_)
297  {
298  ContextGuard context_guard (*context_);
299  MADARA_GUARD_TYPE guard (mutex_);
300  return context_->get (variable_, settings_).to_string () <= value;
301  }
302 
303  return false;
304 }
305 
306 bool
308 {
309  if (context_)
310  {
311  ContextGuard context_guard (*context_);
312  MADARA_GUARD_TYPE guard (mutex_);
313  return context_->get (variable_, settings_).to_string () > value;
314  }
315 
316  return false;
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_string () >= value;
327  }
328 
329  return false;
330 }
331 
334 {
335  return to_string ();
336 }
337 
338 bool
340 {
341  bool result (false);
342 
343  if (context_)
344  {
345  ContextGuard context_guard (*context_);
346  MADARA_GUARD_TYPE guard (mutex_);
347  result = context_->exists (variable_);
348  }
349 
350  return result;
351 }
352 
355 {
357 
358  if (context_)
359  {
360  ContextGuard context_guard (*context_);
361  MADARA_GUARD_TYPE guard (mutex_);
362  result = context_->get (variable_, settings_);
363  }
364 
365  return result;
366 }
367 
370 {
371  if (context_)
372  {
373  ContextGuard context_guard (*context_);
374  MADARA_GUARD_TYPE guard (mutex_);
375  return context_->get (variable_, settings_).to_string ();
376  }
377  else
378  return "";
379 }
380 
381 double
383 {
384  if (context_)
385  {
386  ContextGuard context_guard (*context_);
387  MADARA_GUARD_TYPE guard (mutex_);
388  return context_->get (variable_, settings_).to_double ();
389  }
390  else
391  return 0.0;
392 }
393 
396 {
397  if (context_)
398  {
399  ContextGuard context_guard (*context_);
400  MADARA_GUARD_TYPE guard (mutex_);
401  return context_->get (variable_, settings_).to_integer ();
402  }
403  else
404  return 0;
405 }
406 
407 void
409  uint32_t quality,
410  const KnowledgeReferenceSettings & settings)
411 {
412  if (context_)
413  {
414  ContextGuard context_guard (*context_);
415  MADARA_GUARD_TYPE guard (mutex_);
416  context_->set_quality (name_, quality, true, settings);
417  }
418 }
419 
420 bool
422 {
423  bool result (false);
424 
426  "String::is_true: checking for non-zero value\n");
427 
428  if (context_)
429  {
430  ContextGuard context_guard (*context_);
431  MADARA_GUARD_TYPE guard (mutex_);
432  result = context_->get (variable_, settings_).is_true ();
433  }
434 
436  "String::is_true: final result is %d\n", (int)result);
437 
438  return result;
439 }
440 
441 bool
443 {
444  return !is_true ();
445 }
446 
447 
448 bool
450 {
451  return is_true ();
452 }
453 
454 bool
456 {
457  return is_false ();
458 }
This class encapsulates an entry in a KnowledgeBase.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool is_true(void) const
Checks to see if the record is true.
VariableReference variable_
Variable reference.
Definition: String.h:335
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:455
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: String.cpp:354
double to_double(void) const
converts the value to a float/double.
std::string name_
Prefix of variable.
This class stores a string within a variable context.
Definition: String.h:31
This class stores variables and their values for use by any entity needing state information in a thr...
type operator*(void) const
Returns the value of the variable.
Definition: String.cpp:333
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: String.cpp:294
MADARA_LOCK_TYPE mutex_
guard for access and changes
virtual BaseContainer * clone(void) const
Clones this container.
Definition: String.cpp:108
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
Definition: String.cpp:408
void exchange(String &other)
Exchanges the string at this location with the string at another location.
Definition: String.cpp:128
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
std::string type
trait that describes the value type
Definition: String.h:35
virtual ~String()
Destructor.
Definition: String.cpp:61
bool operator==(type value) const
Checks for equality.
Definition: String.cpp:223
bool operator>(type value) const
Checks for greater than relationship.
Definition: String.cpp:307
bool is_false(void) const
Determines if the value is zero.
Definition: String.cpp:442
type operator+=(type value)
Increments by a value.
Definition: String.cpp:205
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
Definition: String.cpp:339
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
static struct madara::knowledge::tags::string_t string
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: String.h:330
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:449
String(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: String.cpp:4
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
bool operator!=(type value) const
Checks for inequality.
Definition: String.cpp:236
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Integer to_integer(void) const
converts the value to an integer.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: String.cpp:77
std::string to_string(void) const
Returns the value as a string (alias of *)
Definition: String.cpp:369
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a string.
int set(const std::string &key, madara::knowledge::KnowledgeRecord::Integer value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Sets the value of a variable.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
Settings for applying knowledge updates.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: String.cpp:102
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
void operator=(const String &rhs)
Assignment operator.
Definition: String.cpp:114
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.
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: String.cpp:320
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
bool is_true(void) const
Determines if the value is true.
Definition: String.cpp:421
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
double to_double(void) const
Returns the value as a double.
Definition: String.cpp:382
madara::knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer.
Definition: String.cpp:395
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: String.cpp:96
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...
void modify(void)
Mark the value as modified.
Definition: String.cpp:67
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: String.cpp:147
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
bool operator<(type value) const
Checks for less than relationship.
Definition: String.cpp:281