MADARA  3.4.1
Counter.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <sstream>
5 
6 #include "Counter.h"
8 
10  const KnowledgeUpdateSettings& settings)
11  : BaseContainer("", settings), context_(0), id_(0), counters_(1)
12 {
13  init_noharm();
14 }
15 
18  : BaseContainer(name, settings),
19  context_(&(knowledge.get_context())),
20  id_(0),
21  counters_(1)
22 {
23  init_noharm();
24  build_var();
26 }
27 
30  : BaseContainer(name, settings),
31  context_(knowledge.get_context()),
32  id_(0),
33  counters_(1)
34 {
35  init_noharm();
36  build_var();
38 }
39 
41  KnowledgeBase& knowledge, int id, int counters, type value,
42  const KnowledgeUpdateSettings& settings)
43  : BaseContainer(name, settings),
44  context_(&(knowledge.get_context())),
45  id_(id),
46  counters_(counters)
47 {
48  init_noharm();
49  build_var();
51  context_->set(variable_, value, settings);
52 }
53 
55  Variables& knowledge, int id, int counters, type value,
56  const KnowledgeUpdateSettings& settings)
57  : BaseContainer(name, settings),
58  context_(knowledge.get_context()),
59  id_(id),
60  counters_(counters)
61 {
62  init_noharm();
63  build_var();
65  context_->set(variable_, value, settings);
66 }
67 
69  : BaseContainer(rhs),
70  context_(rhs.context_),
71  variable_(rhs.variable_),
72  id_(rhs.id_),
73  counters_(rhs.counters_),
74  aggregate_count_(rhs.aggregate_count_)
75 {
76 }
77 
79 
81 {
82  if (this != &rhs)
83  {
84  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
85 
86  this->context_ = rhs.context_;
87  this->name_ = rhs.name_;
88  this->id_ = rhs.id_;
89  this->counters_ = rhs.counters_;
90  this->settings_ = rhs.settings_;
91  this->variable_ = rhs.variable_;
92  this->aggregate_count_ = rhs.aggregate_count_;
93  }
94 }
95 
97 {
98  if (context_ && name_ != "")
99  {
100  ContextGuard context_guard(*context_);
101  MADARA_GUARD_TYPE guard(mutex_);
102 
103  std::stringstream buffer;
104  if (counters_ > 0)
105  {
106  // add the first counter variable
107  buffer << name_;
108  buffer << ".0";
109 
110  // add all other counter variables
111  for (int i = 1; i < counters_; ++i)
112  {
113  buffer << "+";
114  buffer << name_;
115  buffer << ".";
116  buffer << i;
117  }
118  }
119 
120  aggregate_count_ = context_->compile(buffer.str());
121  }
122  else if (name_ == "")
123  {
124  context_->print("ERROR: containers::Counter needs a name.\n", 0);
125  }
126  else if (!context_)
127  {
129  logger::LOG_ERROR, "ERROR: containers::Counter needs a context.\n");
130  }
131 }
132 
134 {
135  if (context_ && name_ != "")
136  {
137  ContextGuard context_guard(*context_);
138  MADARA_GUARD_TYPE guard(mutex_);
139 
140  std::stringstream buffer;
141 
142  buffer << name_;
143  buffer << ".";
144  buffer << id_;
145 
146  variable_ = context_->get_ref(buffer.str(), no_harm);
147  }
148  else if (name_ == "")
149  {
150  context_->print("ERROR: Container::Counter needs a name.\n", 0);
151  }
152  else if (!context_)
153  {
155  logger::LOG_ERROR, "ERROR: containers::Counter needs a context.\n");
156  }
157 }
158 
160 {
161  no_harm.always_overwrite = false;
162  no_harm.delay_sending_modifieds = true;
163  no_harm.expand_variables = false;
164  no_harm.signal_changes = false;
165  no_harm.track_local_changes = false;
166  no_harm.treat_globals_as_locals = true;
167 }
168 
170 {
171  ContextGuard context_guard(*context_);
172  if (context_ && name_ != "")
173  {
174  context_->mark_modified(variable_);
175  }
176 }
177 
179 {
180  std::stringstream result;
181 
182  result << "Counter: ";
183 
184  if (context_)
185  {
186  ContextGuard context_guard(*context_);
187  MADARA_GUARD_TYPE guard(mutex_);
188 
189  result << this->name_;
190  result << " = " << context_->get(variable_).to_string();
191  result << " (total: " << get_count() << ")";
192  }
193 
194  return result.str();
195 }
196 
198 {
199  modify();
200 }
201 
203 {
204  return get_debug_info();
205 }
206 
209 {
210  return new Counter(*this);
211 }
212 
214 {
215  MADARA_GUARD_TYPE guard(mutex_);
216  return id_;
217 }
218 
220 {
221  MADARA_GUARD_TYPE guard(mutex_);
222  return counters_;
223 }
224 
226  const std::string& var_name, KnowledgeBase& knowledge)
227 {
228  KnowledgeUpdateSettings keep_local(true);
229  context_ = &(knowledge.get_context());
230 
231  ContextGuard context_guard(*context_);
232  MADARA_GUARD_TYPE guard(mutex_);
233 
234  name_ = var_name;
235 
236  this->build_var();
237  this->build_aggregate_count();
238 }
239 
241  const std::string& var_name, Variables& knowledge)
242 {
243  KnowledgeUpdateSettings keep_local(true);
244  context_ = knowledge.get_context();
245 
246  ContextGuard context_guard(*context_);
247  MADARA_GUARD_TYPE guard(mutex_);
248 
249  name_ = var_name;
250 
251  this->build_var();
252  this->build_aggregate_count();
253 }
254 
256 {
257  if (context_)
258  {
259  ContextGuard context_guard(*context_);
260  MADARA_GUARD_TYPE guard(mutex_);
261 
262  id_ = id;
263  counters_ = counters;
264 
265  this->build_var();
266  this->build_aggregate_count();
267  }
268 }
269 
272 {
273  if (context_)
274  {
275  ContextGuard context_guard(*context_);
276  MADARA_GUARD_TYPE guard(mutex_);
277  context_->set(variable_, value, settings_);
278  }
279 
280  return value;
281 }
282 
284 {
285  if (context_)
286  {
287  ContextGuard context_guard(*context_);
288  MADARA_GUARD_TYPE guard(mutex_);
289  return get_count() == value;
290  }
291 
292  return false;
293 }
294 
296 {
297  if (context_)
298  {
299  ContextGuard context_guard(*context_);
300  MADARA_GUARD_TYPE guard(mutex_);
301  return get_count() != value;
302  }
303 
304  return true;
305 }
306 
308  const Counter& value) const
309 {
310  if (context_)
311  {
312  ContextGuard context_guard(*context_);
313  MADARA_GUARD_TYPE guard(mutex_);
314  return get_count() == value.get_count();
315  }
316 
317  return false;
318 }
319 
321  const Counter& value) const
322 {
323  if (context_)
324  {
325  ContextGuard context_guard(*context_);
326  MADARA_GUARD_TYPE guard(mutex_);
327  return get_count() != value.get_count();
328  }
329 
330  return true;
331 }
332 
334 {
335  if (context_)
336  {
337  ContextGuard context_guard(*context_);
338  MADARA_GUARD_TYPE guard(mutex_);
339  return get_count() < value;
340  }
341 
342  return false;
343 }
344 
346 {
347  if (context_)
348  {
349  ContextGuard context_guard(*context_);
350  MADARA_GUARD_TYPE guard(mutex_);
351  return get_count() <= value;
352  }
353 
354  return false;
355 }
356 
358 {
359  if (context_)
360  {
361  ContextGuard context_guard(*context_);
362  MADARA_GUARD_TYPE guard(mutex_);
363  return get_count() > value;
364  }
365 
366  return false;
367 }
368 
370 {
371  if (context_)
372  {
373  ContextGuard context_guard(*context_);
374  MADARA_GUARD_TYPE guard(mutex_);
375  return get_count() >= value;
376  }
377 
378  return false;
379 }
380 
383 {
384  return to_integer();
385 }
386 
389 {
391 
392  if (context_)
393  {
394  ContextGuard context_guard(*context_);
395  MADARA_GUARD_TYPE guard(mutex_);
396  result = get_count_record();
397  }
398 
399  return result;
400 }
401 
404 {
406 
407  if (context_)
408  {
409  ContextGuard context_guard(*context_);
410  MADARA_GUARD_TYPE guard(mutex_);
411  result = get_count();
412  }
413 
414  return result;
415 }
416 
418 {
419  if (context_)
420  {
421  type current(0);
422  ContextGuard context_guard(*context_);
423  MADARA_GUARD_TYPE guard(mutex_);
424 
425  current = context_->get(variable_, settings_).to_integer();
426  current += value;
427  context_->set(variable_, current, settings_);
428  }
429 }
430 
432 {
433  if (context_)
434  {
435  type current(0);
436  ContextGuard context_guard(*context_);
437  MADARA_GUARD_TYPE guard(mutex_);
438 
439  current = context_->get(variable_, settings_).to_integer();
440  current -= value;
441  context_->set(variable_, current, settings_);
442  }
443 }
444 
446 {
447  if (context_)
448  {
449  ContextGuard context_guard(*context_);
450  MADARA_GUARD_TYPE guard(mutex_);
451  context_->inc(variable_, settings_);
452  }
453 }
454 
456 {
457  if (context_)
458  {
459  ContextGuard context_guard(*context_);
460  MADARA_GUARD_TYPE guard(mutex_);
461  context_->dec(variable_, settings_);
462  }
463 }
464 
466 {
467  double result(0.0);
468 
469  if (context_)
470  {
471  ContextGuard context_guard(*context_);
472  MADARA_GUARD_TYPE guard(mutex_);
473  result = get_count_double();
474  }
475 
476  return result;
477 }
478 
480 {
481  std::string result;
482 
483  if (context_)
484  {
485  ContextGuard context_guard(*context_);
486  MADARA_GUARD_TYPE guard(mutex_);
487  result = get_count_string();
488  }
489 
490  return result;
491 }
492 
494  uint32_t quality, const KnowledgeReferenceSettings& settings)
495 {
496  if (context_)
497  {
498  ContextGuard context_guard(*context_);
499  MADARA_GUARD_TYPE guard(mutex_);
500  context_->set_quality(name_, quality, true, settings);
501  }
502 }
503 
505 {
506  bool result(false);
507 
509  "Counter::is_true: checking local counter for non-zero\n");
510 
511  if (context_)
512  {
513  ContextGuard context_guard(*context_);
514  MADARA_GUARD_TYPE guard(mutex_);
515  result = context_->get(variable_, settings_).is_true();
516  }
517 
519  "Counter::is_true: final result is %d\n", (int)result);
520 
521  return result;
522 }
523 
525 {
526  return !is_true();
527 }
528 
530 {
531  return is_true();
532 }
533 
535 {
536  return is_false();
537 }
538 
539 #endif // _MADARA_NO_KARL_
#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.
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 an integer within a variable context.
Definition: Counter.h:34
void operator+=(type value)
Increments by a value.
Definition: Counter.cpp:417
CompiledExpression aggregate_count_
Expression for aggregating count in one atomic operation.
Definition: Counter.h:413
double to_double(void) const
Returns the value as a double.
Definition: Counter.cpp:465
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the counter variables.
Definition: Counter.cpp:493
void operator--(void)
Decrements the value of the variable and returns the result.
Definition: Counter.cpp:455
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Counter.cpp:369
void build_aggregate_count(void)
Builds the aggregate counter logic.
Definition: Counter.cpp:96
int get_id(void) const
Returns the id of the counter in the counter ring.
Definition: Counter.cpp:213
bool operator<(type value) const
Checks for less than relationship.
Definition: Counter.cpp:333
void init_noharm(void)
Initialize the no harm eval settings.
Definition: Counter.cpp:159
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: Counter.cpp:529
std::string to_string(void) const
Returns the value as a string.
Definition: Counter.cpp:479
VariableReference variable_
Variable reference.
Definition: Counter.h:398
bool operator!=(const Counter &value) const
Checks for inequality.
Definition: Counter.cpp:320
void resize(int id=0, int counters=1)
Resizes the counter, usually when number of counters change.
Definition: Counter.cpp:255
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Counter.cpp:208
void operator-=(type value)
Decrements by a value.
Definition: Counter.cpp:431
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: Counter.cpp:534
void operator++(void)
Increments the value of the variable and returns the result.
Definition: Counter.cpp:445
bool is_true(void) const
Determines if the local count is not zero.
Definition: Counter.cpp:504
int get_counters(void) const
Returns the number of counters in the counter ring.
Definition: Counter.cpp:219
void modify(void)
Mark the value as modified.
Definition: Counter.cpp:169
Counter(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Counter.cpp:9
type get_count(void) const
Counts all counter variables.
Definition: Counter.h:358
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: Counter.h:37
type operator*(void) const
Returns the value of the variable.
Definition: Counter.cpp:382
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Counter.cpp:388
bool operator==(const Counter &value) const
Checks for equality.
Definition: Counter.cpp:307
int counters_
the number of counters in the counter ring
Definition: Counter.h:408
void operator=(const Counter &rhs)
Assignment operator.
Definition: Counter.cpp:80
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Counter.cpp:202
int id_
id of this counter in the counter ring
Definition: Counter.h:403
void build_var(void)
Builds the variable that is actually incremented.
Definition: Counter.cpp:133
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Counter.cpp:197
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Counter.cpp:225
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
Definition: Counter.cpp:403
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Counter.cpp:178
bool is_false(void) const
Determines if the local count is zero.
Definition: Counter.cpp:524
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Counter.h:393
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Counter.cpp:345
bool operator>(type value) const
Checks for greater than relationship.
Definition: Counter.cpp:357
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger