MADARA  3.1.8
IntegerStaged.cpp
Go to the documentation of this file.
1 #include "IntegerStaged.h"
3 
5  const KnowledgeUpdateSettings & settings)
6 : BaseContainer ("", settings), context_ (0), value_ (0), has_changed_ (false)
7 {
8 }
9 
11  const std::string & name,
13  const KnowledgeUpdateSettings & settings)
14 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
15  has_changed_ (false)
16 {
17  variable_ = knowledge.get_ref (name, settings_);
18  value_ = knowledge.get (variable_).to_integer ();
19 }
20 
22  const std::string & name,
24  const KnowledgeUpdateSettings & settings)
25 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
26  has_changed_ (false)
27 {
28  variable_ = knowledge.get_ref (name, settings_);
29  value_ = knowledge.get (variable_).to_integer ();
30 }
31 
33  const std::string & name,
35  type value,
36  const KnowledgeUpdateSettings & settings)
37 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
38  value_ (value),
39  has_changed_ (false)
40 {
41  variable_ = knowledge.get_ref (name);
42  context_->set (variable_, value, settings);
43 }
44 
46  const std::string & name,
48  type value,
49  const KnowledgeUpdateSettings & settings)
50 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
51  value_ (value),
52  has_changed_ (false)
53 {
54  variable_ = knowledge.get_ref (name);
55  context_->set (variable_, value, settings);
56 }
57 
58 
60 : BaseContainer (rhs), context_ (rhs.context_), variable_ (rhs.variable_),
61  value_ (rhs.value_)
62 {
63 
64 }
65 
66 
68 {
69  write ();
70 }
71 
72 void
74 {
75  modify ();
76 }
77 
80 {
81  return get_debug_info ();
82 }
83 
84 void
87 {
88  if (context_ && other.context_)
89  {
90  ContextGuard context_guard (*context_);
91  ContextGuard other_context_guard (*other.context_);
92  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
93 
94  type temp = *other;
95  other = **this;
96  *this = temp;
97  }
98 }
99 
100 
101 bool
103 {
104  return is_true ();
105 }
106 
107 bool
109 {
110  return is_false ();
111 }
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: IntegerStaged.h:42
void exchange(IntegerStaged &other)
Exchanges the integer at this location with the integer at another location.
void write(void)
Writes the value to the knowledge base.
IntegerStaged(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
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 ...
void modify(void)
Mark the value as modified.
MADARA_LOCK_TYPE mutex_
guard for access and changes
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
bool has_changed_
Tracks if value_ has changed since last read.
ThreadSafeContext * context_
Variable context that we are modifying.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
bool is_false(void) const
Determines if the value is zero.
VariableReference variable_
Variable reference.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
bool is_true(void) const
Determines if the value is true.
Stages an integer value to and from the knowledge base.
Definition: IntegerStaged.h:38
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
Settings for applying knowledge updates.
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
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
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.