MADARA  3.4.1
IntegerStaged.inl
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_CONTAINERS_INTEGERSTAGED_INL_
2 #define _MADARA_KNOWLEDGE_CONTAINERS_INTEGERSTAGED_INL_
3 
4 #include "IntegerStaged.h"
6 
8 {
9  ContextGuard context_guard(*context_);
10  if (context_ && name_ != "")
11  {
13  }
14 }
15 
17  void)
18 {
19  std::stringstream result;
20 
21  result << "IntegerStaged: ";
22 
23  if (context_)
24  {
25  ContextGuard context_guard(*context_);
26  MADARA_GUARD_TYPE guard(mutex_);
27 
28  result << this->name_;
29  result << " = " << context_->get(variable_).to_string();
30  result << ", staged_value = " << value_;
31  }
32 
33  return result.str();
34 }
35 
38 {
39  return new IntegerStaged(*this);
40 }
41 
43  const IntegerStaged& rhs)
44 {
45  if (this != &rhs)
46  {
47  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
48 
49  this->has_changed_ = rhs.has_changed_;
50  this->value_ = rhs.value_;
51  this->context_ = rhs.context_;
52  this->name_ = rhs.name_;
53  this->settings_ = rhs.settings_;
54  this->variable_ = rhs.variable_;
55  }
56 }
57 
59  const std::string& var_name, KnowledgeBase& knowledge, bool sync)
60 {
61  KnowledgeUpdateSettings keep_local(true);
62  context_ = &(knowledge.get_context());
63 
64  ContextGuard context_guard(*context_);
65  MADARA_GUARD_TYPE guard(mutex_);
66 
67  name_ = var_name;
68  variable_ = context_->get_ref(name_, keep_local);
69 
70  if (sync)
71  {
72  value_ = context_->get(variable_).to_integer();
73  has_changed_ = false;
74  }
75 }
76 
78  const std::string& var_name, ThreadSafeContext& knowledge, bool sync)
79 {
80  KnowledgeUpdateSettings keep_local(true);
82 
83  ContextGuard context_guard(*context_);
84  MADARA_GUARD_TYPE guard(mutex_);
85 
86  name_ = var_name;
87  variable_ = context_->get_ref(name_, keep_local);
88 
89  if (sync)
90  {
91  value_ = context_->get(variable_).to_integer();
92  has_changed_ = false;
93  }
94 }
95 
97  const std::string& var_name, Variables& knowledge, bool sync)
98 {
99  KnowledgeUpdateSettings keep_local(true);
100  context_ = knowledge.get_context();
101 
102  ContextGuard context_guard(*context_);
103  MADARA_GUARD_TYPE guard(mutex_);
104 
105  name_ = var_name;
106  variable_ = context_->get_ref(name_, keep_local);
107 
108  if (sync)
109  {
110  value_ = context_->get(variable_).to_integer();
111  has_changed_ = false;
112  }
113 }
114 
117 {
118  has_changed_ = true;
119  return value_ = value;
120 }
121 
123  type value) const
124 {
125  return value_ == value;
126 }
127 
129  type value) const
130 {
131  return value_ != value;
132 }
133 
135  const IntegerStaged& value) const
136 {
137  return value_ == value.value_;
138 }
139 
141  const IntegerStaged& value) const
142 {
143  return value_ != value.value_;
144 }
145 
147  type value) const
148 {
149  return value_ < value;
150 }
151 
153  type value) const
154 {
155  return value_ <= value;
156 }
157 
159  type value) const
160 {
161  return value_ > value;
162 }
163 
165  type value) const
166 {
167  return value_ >= value;
168 }
169 
172 {
173  return to_integer();
174 }
175 
177 {
178  bool result(false);
179 
180  if (context_)
181  {
182  ContextGuard context_guard(*context_);
183  MADARA_GUARD_TYPE guard(mutex_);
184  result = context_->exists(variable_);
185  }
186 
187  return result;
188 }
189 
191 {
192  return value_ != 0;
193 }
194 
196 {
197  return value_ == 0;
198 }
199 
202 {
203  return KnowledgeRecord(value_);
204 }
205 
208 {
209  return value_;
210 }
211 
214 {
215  has_changed_ = true;
216  value_ += value;
217 
218  return value_;
219 }
220 
223 {
224  has_changed_ = true;
225  value_ += value;
226 
227  return value_;
228 }
229 
232 {
233  has_changed_ = true;
234  ++value_;
235 
236  return value_;
237 }
238 
241 {
242  has_changed_ = true;
243  --value_;
244 
245  return value_;
246 }
247 
249  void) const
250 {
251  return (double)value_;
252 }
253 
255  void) const
256 {
257  KnowledgeRecord value(value_);
258  return value.to_string();
259 }
260 
262  uint32_t quality, const KnowledgeReferenceSettings& settings)
263 {
264  if (context_)
265  {
266  ContextGuard context_guard(*context_);
267  MADARA_GUARD_TYPE guard(mutex_);
268  context_->set_quality(name_, quality, true, settings);
269  }
270 }
271 
273 {
274  has_changed_ = false;
275  value_ = context_->get(variable_).to_integer();
276 }
277 
279 {
280  if (has_changed_)
281  context_->set(variable_, value_);
282 }
283 
284 #endif // _MADARA_KNOWLEDGE_CONTAINERS_INTEGERSTAGED_INL_
const ThreadSafeContext * context_
madara::knowledge::KnowledgeRecord KnowledgeRecord
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.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
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...
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...
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
Stages an integer value to and from the knowledge base.
Definition: IntegerStaged.h:39
bool is_false(void) const
Determines if the value is zero.
ThreadSafeContext * context_
Variable context that we are modifying.
type operator*(void) const
Returns the value of the variable.
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
bool has_changed_
Tracks if value_ has changed since last read.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: IntegerStaged.h:42
void modify(void)
Mark the value as modified.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
bool operator!=(const IntegerStaged &value) const
Checks for inequality.
bool operator<(type value) const
Checks for less than relationship.
std::string to_string(void) const
Returns the value as a string.
virtual BaseContainer * clone(void) const
Clones this container.
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
bool operator>=(type value) const
Checks for greater than or equal to relationship.
double to_double(void) const
Returns the value as a double.
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
type operator-=(type value)
Decrements by a value.
VariableReference variable_
Variable reference.
type operator--(void)
Decrements the value of the variable and returns the result.
void write(void)
Writes the value to the knowledge base.
type operator++(void)
Increments the value of the variable and returns the result.
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
bool operator==(const IntegerStaged &value) const
Checks for equality.
bool operator>(type value) const
Checks for greater than relationship.
type operator+=(type value)
Increments by a value.
void operator=(const IntegerStaged &rhs)
Assignment operator.
void read(void)
Reads the value from the knowledge base.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
bool operator<=(type value) const
Checks for less than or equal to relationship.
bool is_true(void) const
Determines if the value is true.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.