MADARA  3.1.8
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  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_string ();
211  result += value;
212  context_->set (variable_, result, settings_);
213  }
214 
215  return result;
216 }
217 
218 bool
220 {
221  if (context_)
222  {
223  ContextGuard context_guard (*context_);
224  MADARA_GUARD_TYPE guard (mutex_);
225  return context_->get (variable_, settings_) == value;
226  }
227 
228  return false;
229 }
230 
231 bool
233 {
234  if (context_)
235  {
236  ContextGuard context_guard (*context_);
237  MADARA_GUARD_TYPE guard (mutex_);
238  return context_->get (variable_, settings_).to_string () != value;
239  }
240 
241  return true;
242 }
243 
244 bool
246  const String & value) const
247 {
248  if (context_)
249  {
250  ContextGuard context_guard (*context_);
251  MADARA_GUARD_TYPE guard (mutex_);
252  return
254  value.context_->get (value.variable_, value.settings_);
255  }
256 
257  return false;
258 }
259 
260 bool
262  const String & value) const
263 {
264  if (context_)
265  {
266  ContextGuard context_guard (*context_);
267  MADARA_GUARD_TYPE guard (mutex_);
268  return
270  value.context_->get (value.variable_, value.settings_);
271  }
272 
273  return true;
274 }
275 
276 bool
278 {
279  if (context_)
280  {
281  ContextGuard context_guard (*context_);
282  MADARA_GUARD_TYPE guard (mutex_);
283  return context_->get (variable_, settings_).to_string () < value;
284  }
285 
286  return false;
287 }
288 
289 bool
291 {
292  if (context_)
293  {
294  ContextGuard context_guard (*context_);
295  MADARA_GUARD_TYPE guard (mutex_);
296  return context_->get (variable_, settings_).to_string () <= value;
297  }
298 
299  return false;
300 }
301 
302 bool
304 {
305  if (context_)
306  {
307  ContextGuard context_guard (*context_);
308  MADARA_GUARD_TYPE guard (mutex_);
309  return context_->get (variable_, settings_).to_string () > value;
310  }
311 
312  return false;
313 }
314 
315 bool
317 {
318  if (context_)
319  {
320  ContextGuard context_guard (*context_);
321  MADARA_GUARD_TYPE guard (mutex_);
322  return context_->get (variable_, settings_).to_string () >= value;
323  }
324 
325  return false;
326 }
327 
330 {
331  return to_string ();
332 }
333 
334 bool
336 {
337  bool result (false);
338 
339  if (context_)
340  {
341  ContextGuard context_guard (*context_);
342  MADARA_GUARD_TYPE guard (mutex_);
343  result = context_->exists (variable_);
344  }
345 
346  return result;
347 }
348 
351 {
353 
354  if (context_)
355  {
356  ContextGuard context_guard (*context_);
357  MADARA_GUARD_TYPE guard (mutex_);
358  result = context_->get (variable_, settings_);
359  }
360 
361  return result;
362 }
363 
366 {
367  if (context_)
368  {
369  ContextGuard context_guard (*context_);
370  MADARA_GUARD_TYPE guard (mutex_);
371  return context_->get (variable_, settings_).to_string ();
372  }
373  else
374  return "";
375 }
376 
377 double
379 {
380  if (context_)
381  {
382  ContextGuard context_guard (*context_);
383  MADARA_GUARD_TYPE guard (mutex_);
384  return context_->get (variable_, settings_).to_double ();
385  }
386  else
387  return 0.0;
388 }
389 
392 {
393  if (context_)
394  {
395  ContextGuard context_guard (*context_);
396  MADARA_GUARD_TYPE guard (mutex_);
397  return context_->get (variable_, settings_).to_integer ();
398  }
399  else
400  return 0;
401 }
402 
403 void
405  uint32_t quality,
406  const KnowledgeReferenceSettings & settings)
407 {
408  if (context_)
409  {
410  ContextGuard context_guard (*context_);
411  MADARA_GUARD_TYPE guard (mutex_);
412  context_->set_quality (name_, quality, true, settings);
413  }
414 }
415 
416 bool
418 {
419  bool result (false);
420 
422  "String::is_true: checking for non-zero value\n");
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  "String::is_true: final result is %d\n", (int)result);
433 
434  return result;
435 }
436 
437 bool
439 {
440  return !is_true ();
441 }
442 
443 
444 bool
446 {
447  return is_true ();
448 }
449 
450 bool
452 {
453  return is_false ();
454 }
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:451
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:350
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.
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:329
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: String.cpp:290
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:404
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:219
bool operator>(type value) const
Checks for greater than relationship.
Definition: String.cpp:303
bool is_false(void) const
Determines if the value is zero.
Definition: String.cpp:438
type operator+=(type value)
Increments by a value.
Definition: String.cpp:201
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
Definition: String.cpp:335
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
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:445
String(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: String.cpp:4
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
bool operator!=(type value) const
Checks for inequality.
Definition: String.cpp:232
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
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:365
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
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
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:316
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:417
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:378
madara::knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer.
Definition: String.cpp:391
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:143
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:277