MADARA  3.2.3
NativeIntegerVectorStaged.inl
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
3 #define _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
4 
7 
8 inline void
10 {
11  ContextGuard context_guard (*context_);
12  if (context_ && name_ != "")
13  {
15  }
16 }
17 
18 inline std::string
20  void)
21 {
22  std::stringstream result;
23 
24  result << "Native Integer Vector Staged: ";
25 
26  if (context_)
27  {
28  result << this->name_;
29  result << " [" << size () << "]";
30  result << " = " << value_.to_string ();
31  }
32 
33  return result.str ();
34 }
35 
36 inline std::string
38  void)
39 {
40  return get_debug_info ();
41 }
42 
43 inline void
45  const NativeIntegerVectorStaged & rhs)
46 {
47  if (this != &rhs)
48  {
49  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
50 
51  this->context_ = rhs.context_;
52  this->name_ = rhs.name_;
53  this->settings_ = rhs.settings_;
54  this->vector_ = rhs.vector_;
55  this->value_ = rhs.value_;
56  this->has_changed_ = true;
57  }
58 }
59 
60 inline void
62  const std::vector <type> & rhs)
63 {
64  if (!has_changed_)
65  has_changed_ = true;
66 
68 }
69 
70 inline void
72  type value)
73 {
74  if (!has_changed_)
75  has_changed_ = true;
76 
77  size_t i = size ();
78  resize ((int)i + 1);
79  set (i, value);
80 }
81 
82 inline void
84  size_t size)
85 {
86  if (!has_changed_)
87  has_changed_ = true;
88 
89  value_.resize (size);
90 }
91 
92 inline size_t
94 {
95  return value_.size ();
96 }
97 
98 inline void
100  const std::string & var_name,
102  bool sync)
103 {
104  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
105  {
106  context_ = &(knowledge.get_context ());
107 
108  ContextGuard context_guard (*context_);
109  MADARA_GUARD_TYPE guard (mutex_);
110 
111  name_ = var_name;
112 
113  vector_ = knowledge.get_ref (var_name, settings_);
114 
115  if (size > 0)
116  {
117  resize (size_t (size));
118  }
119  else if (sync)
120  {
121  std::vector <type> values = knowledge.get (vector_).to_integers ();
122  value_.set_value (values);
123  }
124  }
125 }
126 
127 inline void
129  const std::string & var_name,
130  Variables & knowledge, int size,
131  bool sync)
132 {
133  if (context_ != knowledge.get_context () || name_ != var_name)
134  {
135  context_ = knowledge.get_context ();
136 
137  ContextGuard context_guard (*context_);
138  MADARA_GUARD_TYPE guard (mutex_);
139 
140  name_ = var_name;
141 
142  vector_ = knowledge.get_ref (var_name, settings_);
143 
144  if (size > 0)
145  {
146  resize (size_t (size));
147  }
148  else if (sync)
149  {
150  std::vector <type> values = knowledge.get (vector_).to_integers ();
151  value_.set_value (values);
152  }
153  }
154 }
155 
156 inline void
158  const std::string & var_name,
160  bool sync)
161 {
162  if (context_ != &knowledge || name_ != var_name)
163  {
164  context_ = &knowledge;
165 
166  ContextGuard context_guard (*context_);
167  MADARA_GUARD_TYPE guard (mutex_);
168 
169  name_ = var_name;
170 
171  vector_ = knowledge.get_ref (var_name, settings_);
172 
173  if (size > 0)
174  {
175  resize (size_t (size));
176  }
177  else if (sync)
178  {
179  std::vector <type> values = knowledge.get (vector_).to_integers ();
180  value_.set_value (values);
181  }
182  }
183 }
184 
185 inline void
188 {
189  knowledge::KnowledgeRecord temp (other.value_);
190 
191  other.value_ = value_;
192  value_ = temp;
193 
194  if (!has_changed_)
195  has_changed_ = true;
196 
197  if (!other.has_changed_)
198  other.has_changed_ = true;
199 }
200 
201 inline void
204 {
205  size_t other_size = other.size ();
206  size_t this_size = this->size ();
207 
208  if (this_size > 0)
209  {
210  size_t size = other_size + this_size;
211  other.resize ((int)size);
212 
213  knowledge::KnowledgeRecord & rhs (other.value_);
215 
216  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
217 
218  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
219  {
220  rhs.set_index (j, lhs.retrieve_index (i).to_double ());
221  }
222 
223  lhs.reset_value ();
224 
225  if (!has_changed_)
226  has_changed_ = true;
227  }
228 }
229 
230 inline void
232  KnowledgeVector & target) const
233 {
234  target.resize (size ());
235 
236  for (size_t i = 0; i < target.size (); ++i)
237  {
238  target[i] = value_.retrieve_index (i);
239  }
240 }
241 
244  size_t index) const
245 {
247 
248  result = result.retrieve_index (index);
249 
250  return result.to_integer ();
251 }
252 
253 inline void
255  size_t index,
256  type value)
257 {
258  if (!has_changed_)
259  has_changed_ = true;
260 
261  value_.set_index (index, value);
262 }
263 
264 inline void
266  const std::vector <type> & value)
267 {
268  if (!has_changed_)
269  has_changed_ = true;
270 
272 }
273 
274 inline void
276  size_t /*index*/,
277  uint32_t quality,
278  const KnowledgeReferenceSettings & settings)
279 {
280  if (context_)
281  {
282  ContextGuard context_guard (*context_);
283  MADARA_GUARD_TYPE guard (mutex_);
284  context_->set_quality (name_, quality, true, settings);
285  }
286 }
287 
290  size_t index) const
291 {
293  result = value_.retrieve_index (index);
294  return result;
295 }
296 
299  void) const
300 {
301  return value_;
302 }
303 
304 inline bool
306 {
307  bool result (false);
308 
310  "NativeIntegerVectorStaged::is_true: checking for non-zero value\n");
311 
312  result = value_.is_true ();
313 
315  "NativeIntegerVectorStaged::is_true: final result is %d\n",
316  (int)result);
317 
318  return result;
319 }
320 
321 inline bool
323 {
324  return !is_true ();
325 }
326 
327 inline void
329 {
330  has_changed_ = false;
331  value_ = context_->get (vector_);
332 }
333 
334 inline void
336 {
337  if (has_changed_)
339 }
340 
341 
342 #endif // _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
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.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
bool is_true(void) const
Checks to see if the record is true.
void operator=(const NativeIntegerVectorStaged &rhs)
Assignment operator.
This class stores a vector of doubles inside of KaRL.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1, bool sync=true)
Sets the variable name that this refers to.
void reset_value(void) noexcept
resets the variable to an integer
void transfer_to(NativeIntegerVectorStaged &other)
Transfers elements from this vector to another.
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
Retrieves the entire vector as a native double array in a record.
madara::knowledge::KnowledgeRecord KnowledgeRecord
void set(size_t index, type value)
Sets a knowledge variable to a specified value.
ThreadSafeContext * context_
Variable context that we are modifying.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
double to_double(void) const
converts the value to a float/double.
std::string name_
Prefix of variable.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
This class stores variables and their values for use by any entity needing state information in a thr...
size_t size(void) const
Returns the size of the vector.
MADARA_LOCK_TYPE mutex_
guard for access and changes
KnowledgeRecord value_
The staged value of the vector.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
bool is_true(void) const
Determines if all values in the vector are true.
void read(void)
Reads the value from the knowledge base.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
::std::vector< KnowledgeRecord > KnowledgeVector
void write(void)
Writes the value to the knowledge base.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
static struct madara::knowledge::tags::string_t string
VariableReference vector_
Reference to the size field of the vector space.
bool is_false(void) const
Determines if the value of the vector is false.
bool has_changed_
Tracks if value_ has changed since last read.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
void exchange(NativeIntegerVectorStaged &other)
Exchanges the vector at this location with the vector at another location.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Integer to_integer(void) const
converts the value to an integer.
void set_quality(size_t index, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
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.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void resize(size_t new_size)
resizes an array to a new size
Provides an interface for external functions into the MADARA KaRL variable settings.
uint32_t size(void) const
returns the size of the value
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
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.