MADARA  3.2.3
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 
5 #include "IntegerStaged.h"
7 
8 inline void
10 {
11  ContextGuard context_guard (*context_);
12  if (context_ && name_ != "")
13  {
15  }
16 }
17 
18 inline std::string
20 {
21  std::stringstream result;
22 
23  result << "IntegerStaged: ";
24 
25  if (context_)
26  {
27  ContextGuard context_guard (*context_);
28  MADARA_GUARD_TYPE guard (mutex_);
29 
30  result << this->name_;
31  result << " = " << context_->get (variable_).to_string ();
32  result << ", staged_value = " << value_;
33  }
34 
35  return result.str ();
36 }
37 
40 {
41  return new IntegerStaged (*this);
42 }
43 
44 inline void
46 {
47  if (this != &rhs)
48  {
49  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
50 
51  this->has_changed_ = rhs.has_changed_;
52  this->value_ = rhs.value_;
53  this->context_ = rhs.context_;
54  this->name_ = rhs.name_;
55  this->settings_ = rhs.settings_;
56  this->variable_ = rhs.variable_;
57  }
58 }
59 
60 inline void
62  const std::string & var_name,
64  bool sync)
65 {
66  KnowledgeUpdateSettings keep_local (true);
67  context_ = &(knowledge.get_context ());
68 
69  ContextGuard context_guard (*context_);
70  MADARA_GUARD_TYPE guard (mutex_);
71 
72  name_ = var_name;
73  variable_ = context_->get_ref (name_, keep_local);
74 
75  if (sync)
76  {
78  has_changed_ = false;
79  }
80 }
81 
82 inline void
84  const std::string & var_name,
86  bool sync)
87 {
88  KnowledgeUpdateSettings keep_local (true);
89  context_ = &knowledge;
90 
91  ContextGuard context_guard (*context_);
92  MADARA_GUARD_TYPE guard (mutex_);
93 
94  name_ = var_name;
95  variable_ = context_->get_ref (name_, keep_local);
96 
97  if (sync)
98  {
100  has_changed_ = false;
101  }
102 }
103 
104 inline void
106  const std::string & var_name,
108  bool sync)
109 {
110  KnowledgeUpdateSettings keep_local (true);
111  context_ = knowledge.get_context ();
112 
113  ContextGuard context_guard (*context_);
114  MADARA_GUARD_TYPE guard (mutex_);
115 
116  name_ = var_name;
117  variable_ = context_->get_ref (name_, keep_local);
118 
119  if (sync)
120  {
122  has_changed_ = false;
123  }
124 }
125 
128 {
129  has_changed_ = true;
130  return value_ = value;
131 }
132 
133 inline bool
135 {
136  return value_ == value;
137 }
138 
139 inline bool
141 {
142  return value_ != value;
143 }
144 
145 inline bool
147 const IntegerStaged & value) const
148 {
149  return value_ == value.value_;
150 }
151 
152 inline bool
154  const IntegerStaged & value) const
155 {
156  return value_ != value.value_;
157 }
158 
159 inline bool
161 {
162  return value_ < value;
163 }
164 
165 inline bool
167 {
168  return value_ <= value;
169 }
170 
171 inline bool
173 {
174  return value_ > value;
175 }
176 
177 inline bool
179 {
180  return value_ >= value;
181 }
182 
185 {
186  return to_integer ();
187 }
188 
189 inline bool
191 {
192  bool result (false);
193 
194  if (context_)
195  {
196  ContextGuard context_guard (*context_);
197  MADARA_GUARD_TYPE guard (mutex_);
198  result = context_->exists (variable_);
199  }
200 
201  return result;
202 }
203 
204 inline bool
206 {
207  return value_ != 0;
208 }
209 
210 inline bool
212 {
213  return value_ == 0;
214 }
215 
218 {
219  return KnowledgeRecord (value_);
220 }
221 
224 {
225  return value_;
226 }
227 
230 {
231  has_changed_ = true;
232  value_ += value;
233 
234  return value_;
235 }
236 
239 {
240  has_changed_ = true;
241  value_ += value;
242 
243  return value_;
244 }
245 
248 {
249  has_changed_ = true;
250  ++value_;
251 
252  return value_;
253 }
254 
257 {
258  has_changed_ = true;
259  --value_;
260 
261  return value_;
262 }
263 
264 inline double
266 {
267  return (double)value_;
268 }
269 
270 inline std::string
272 {
273  KnowledgeRecord value (value_);
274  return value.to_string ();
275 }
276 
277 inline void
279  uint32_t quality,
280  const KnowledgeReferenceSettings & settings)
281 {
282  if (context_)
283  {
284  ContextGuard context_guard (*context_);
285  MADARA_GUARD_TYPE guard (mutex_);
286  context_->set_quality (name_, quality, true, settings);
287  }
288 }
289 
290 inline void
292 {
293  has_changed_ = false;
295 }
296 
297 inline void
299 {
300  if (has_changed_)
302 }
303 
304 #endif // _MADARA_KNOWLEDGE_CONTAINERS_INTEGERSTAGED_INL_
This class encapsulates an entry in a KnowledgeBase.
virtual BaseContainer * clone(void) const
Clones this container.
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 write(void)
Writes the value to the knowledge base.
IntegerStaged(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
madara::knowledge::KnowledgeRecord KnowledgeRecord
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
void operator=(const IntegerStaged &rhs)
Assignment operator.
std::string name_
Prefix of variable.
double to_double(void) const
Returns the value as a double.
This class stores variables and their values for use by any entity needing state information in a thr...
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
void modify(void)
Mark the value as modified.
MADARA_LOCK_TYPE mutex_
guard for access and changes
bool has_changed_
Tracks if value_ has changed since last read.
ThreadSafeContext * context_
Variable context that we are modifying.
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
type operator++(void)
Increments the value of the variable and returns the result.
bool is_false(void) const
Determines if the value is zero.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
bool operator<=(type value) const
Checks for less than or equal to relationship.
type operator--(void)
Decrements the value of the variable and returns the result.
bool operator>=(type value) const
Checks for greater than or equal to relationship.
type operator*(void) const
Returns the value of the variable.
bool operator==(const IntegerStaged &value) const
Checks for equality.
VariableReference variable_
Variable reference.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
static struct madara::knowledge::tags::string_t string
type operator-=(type value)
Decrements by a value.
std::string to_string(void) const
Returns the value as a string.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
bool is_true(void) const
Determines if the value is true.
void read(void)
Reads the value from the knowledge base.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
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.
type operator+=(type value)
Increments by a value.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Provides functions and classes for the distributed knowledge base.
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the variable.
KnowledgeUpdateSettings settings_
Settings for modifications.
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
bool operator>(type value) const
Checks for greater than relationship.
bool operator<(type value) const
Checks for less than relationship.
Provides an interface for external functions into the MADARA KaRL variable settings.
bool operator!=(const IntegerStaged &value) const
Checks for inequality.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
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...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.