MADARA  3.1.8
NativeIntegerVector.cpp
Go to the documentation of this file.
1 #include "NativeIntegerVector.h"
3 
5  const KnowledgeUpdateSettings & settings)
6 : BaseContainer ("", settings), context_ (0)
7 {
8 }
9 
11  const std::string & name,
13  int size,
14  const KnowledgeUpdateSettings & settings)
15 : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
16 {
17  vector_ = knowledge.get_ref (name, KnowledgeUpdateSettings (true));
18  if (size >= 0)
19  {
20  resize (size);
21  }
22 }
23 
25  const std::string & name,
27  int size,
28  const KnowledgeUpdateSettings & settings)
29  : BaseContainer (name, settings), context_ (knowledge.get_context ())
30 {
31  vector_ = knowledge.get_ref (name, settings);
32  if (size >= 0)
33  {
34  resize (size);
35  }
36 }
37 
39  const NativeIntegerVector & rhs)
40 : BaseContainer (rhs), context_ (rhs.context_),
41  vector_ (rhs.vector_)
42 {
43 
44 }
45 
46 
48 {
49 
50 }
51 
52 void
54 {
55  ContextGuard context_guard (*context_);
56  if (context_ && name_ != "")
57  {
59  }
60 }
61 
64  void)
65 {
66  std::stringstream result;
67 
68  result << "Native Integer Vector: ";
69 
70  if (context_)
71  {
72  ContextGuard context_guard (*context_);
73  MADARA_GUARD_TYPE guard (mutex_);
74 
75  result << this->name_;
76  result << " [" << size () << "]";
77  result << " = " << context_->get (vector_).to_string ();
78  }
79 
80  return result.str ();
81 }
82 
83 void
85 {
86  modify ();
87 }
88 
91  void)
92 {
93  return get_debug_info ();
94 }
95 
98 {
99  return new NativeIntegerVector (*this);
100 }
101 
102 void
104  const NativeIntegerVector & rhs)
105 {
106  if (this != &rhs)
107  {
108  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
109 
110  this->context_ = rhs.context_;
111  this->name_ = rhs.name_;
112  this->settings_ = rhs.settings_;
113  this->vector_ = rhs.vector_;
114  }
115 }
116 
117 void
119  type value)
120 {
121  if (context_ && name_ != "")
122  {
123  ContextGuard context_guard (*context_);
124  MADARA_GUARD_TYPE guard (mutex_);
125 
126  size_t i = size ();
127  resize ((int)i + 1);
128  set (i, value);
129  }
130 }
131 
132 void
134  size_t size)
135 {
136  if (context_ && name_ != "")
137  {
138  ContextGuard context_guard (*context_);
139  MADARA_GUARD_TYPE guard (mutex_);
140 
142 
143  value.resize (size);
144 
145  context_->set (vector_, value, settings_);
146  }
147 }
148 
149 size_t
151 {
152  ContextGuard context_guard (*context_);
153  MADARA_GUARD_TYPE guard (mutex_);
154 
155  return context_->get (vector_, settings_).size ();
156 }
157 
158 void
160  const std::string & var_name,
162 {
163  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
164  {
165  context_ = &(knowledge.get_context ());
166 
167  ContextGuard context_guard (*context_);
168  MADARA_GUARD_TYPE guard (mutex_);
169 
170  name_ = var_name;
171 
172  vector_ = knowledge.get_ref (var_name, settings_);
173 
174  if (size > 0)
175  resize (size_t (size));
176  }
177 }
178 
179 void
181  const std::string & var_name,
182  Variables & knowledge, int size)
183 {
184  if (context_ != knowledge.get_context () || name_ != var_name)
185  {
186  context_ = knowledge.get_context ();
187 
188  ContextGuard context_guard (*context_);
189  MADARA_GUARD_TYPE guard (mutex_);
190 
191  name_ = var_name;
192 
193  vector_ = knowledge.get_ref (var_name, settings_);
194 
195  if (size > 0)
196  resize (size_t (size));
197  }
198 }
199 
200 void
202  const std::string & var_name,
204 {
205  if (context_ != &knowledge || name_ != var_name)
206  {
207  context_ = &knowledge;
208 
209  ContextGuard context_guard (*context_);
210  MADARA_GUARD_TYPE guard (mutex_);
211 
212  name_ = var_name;
213 
214  vector_ = knowledge.get_ref (var_name, settings_);
215 
216  if (size > 0)
217  resize (size_t (size));
218  }
219 }
220 
221 void
223  NativeIntegerVector & other)
224 {
225  if (context_ && other.context_)
226  {
227  ContextGuard context_guard (*context_);
228  ContextGuard other_context_guard (*other.context_);
229  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
230 
232 
233  context_->set (other.vector_, context_->get (vector_), other.settings_);
234  context_->set (vector_, temp, settings_);
235  }
236 }
237 
238 void
240  NativeIntegerVector & other)
241 {
242  if (context_ && other.context_)
243  {
244  ContextGuard context_guard (*context_);
245  ContextGuard other_context_guard (*other.context_);
246  MADARA_GUARD_TYPE guard (mutex_);
247  MADARA_GUARD_TYPE guard2 (other.mutex_);
248 
249  size_t other_size = other.size ();
250  size_t this_size = this->size ();
251 
252  if (this_size > 0)
253  {
254  size_t size = other_size + this_size;
255  other.resize ((int)size);
256 
257  knowledge::KnowledgeRecord rhs (other.context_->get (other.vector_));
259 
260  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
261 
262  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
263  {
264  rhs.set_index (other_size, lhs.retrieve_index (i).to_double ());
265  }
266 
267  other.context_->set (other.vector_, rhs, other.settings_);
268 
269  this->resize (0);
270  }
271  }
272 }
273 
274 void
276  KnowledgeVector & target) const
277 {
278  if (context_)
279  {
280  ContextGuard context_guard (*context_);
281  MADARA_GUARD_TYPE guard (mutex_);
282 
283  target.resize (size ());
284 
285  for (size_t i = 0; i < target.size (); ++i)
286  {
287  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
288  }
289  }
290 }
291 
294  size_t index) const
295 {
297 
298  if (context_)
299  {
300  ContextGuard context_guard (*context_);
301  MADARA_GUARD_TYPE guard (mutex_);
302  result = context_->get (vector_, settings_);
303 
304  if (index < result.size ())
305  result = result.retrieve_index (index);
306  }
307 
308  return result.to_double ();
309 }
310 
311 int
313  size_t index,
314  type value)
315 {
316  int result = -1;
317 
318  if (context_)
319  {
320  ContextGuard context_guard (*context_);
321  MADARA_GUARD_TYPE guard (mutex_);
322  result = context_->set_index (vector_, index, value, settings_);
323  }
324 
325  return result;
326 }
327 
328 int
330  size_t index,
331  type value,
332  const KnowledgeUpdateSettings & settings)
333 {
334  int result = -1;
335 
336  if (context_)
337  {
338  ContextGuard context_guard (*context_);
339  MADARA_GUARD_TYPE guard (mutex_);
340  result = context_->set_index (vector_, index, value, settings);
341  }
342 
343  return result;
344 }
345 
346 int
348  const std::vector <type> & value)
349 {
350  int result = -1;
351 
352  if (context_)
353  {
354  ContextGuard context_guard (*context_);
355  MADARA_GUARD_TYPE guard (mutex_);
356  context_->set (vector_, value, settings_);
357  }
358 
359  return result;
360 }
361 
362 int
364  const std::vector <type> & value,
365  const KnowledgeUpdateSettings & settings)
366 {
367  int result = -1;
368 
369  if (context_)
370  {
371  ContextGuard context_guard (*context_);
372  MADARA_GUARD_TYPE guard (mutex_);
373  context_->set (vector_, value, settings);
374  }
375 
376  return result;
377 }
378 
379 void
381  size_t /*index*/,
382  uint32_t quality,
383  const KnowledgeReferenceSettings & settings)
384 {
385  if (context_)
386  {
387  ContextGuard context_guard (*context_);
388  MADARA_GUARD_TYPE guard (mutex_);
389  context_->set_quality (name_, quality, true, settings);
390  }
391 }
392 
395  size_t index) const
396 {
398  result = result.retrieve_index (index);
399  return result;
400 }
401 
404  void) const
405 {
406  return context_->get (this->vector_, settings_);
407 }
408 
409 bool
411 {
412  bool result (false);
413 
415  "NativeIntegerVector::is_true: checking for non-zero value\n");
416 
417  if (context_)
418  {
419  ContextGuard context_guard (*context_);
420  MADARA_GUARD_TYPE guard (mutex_);
421  result = context_->get (vector_, settings_).is_true ();
422  }
423 
425  "NativeIntegerVector::is_true: final result is %d\n",
426  (int)result);
427 
428  return result;
429 }
430 
431 bool
433 {
434  return !is_true ();
435 }
436 
437 
438 bool
440 {
441  return is_true ();
442 }
443 
444 bool
446 {
447  return is_false ();
448 }
This class encapsulates an entry in a KnowledgeBase.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
virtual BaseContainer * clone(void) const
Clones this container.
bool is_true(void) const
Checks to see if the record is true.
This class stores a vector of doubles inside of KaRL.
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
madara::knowledge::KnowledgeRecord KnowledgeRecord
void modify(void)
Mark the value as modified.
double to_double(void) const
converts the value to a float/double
std::string name_
Prefix of variable.
ThreadSafeContext * context_
Variable context that we are modifying.
NativeIntegerVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
bool is_false(void) const
Determines if the value of the vector is false.
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 ...
bool is_true(void) const
Determines if all values in the vector are true.
This class stores variables and their values for use by any entity needing state information in a thr...
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
MADARA_LOCK_TYPE mutex_
guard for access and changes
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
::std::vector< KnowledgeRecord > KnowledgeVector
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
void operator=(const NativeIntegerVector &rhs)
Assignment operator.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
VariableReference vector_
Reference to the size field of the vector space.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
static constexpr struct madara::knowledge::tags::string_t string
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void exchange(NativeIntegerVector &other)
Exchanges the vector at this location with the vector at another location.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
size_t size(void) const
Returns the size of the vector.
void transfer_to(NativeIntegerVector &other)
Transfers elements from this vector to another.
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.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
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.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
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.