MADARA  3.1.8
NativeDoubleVector.cpp
Go to the documentation of this file.
1 #include "NativeDoubleVector.h"
4 
6  const KnowledgeUpdateSettings & settings)
7  : BaseContainer ("", settings), context_ (0)
8 {
10  logger::LOG_MAJOR, "NativeDoubleVector::constructor: new object\n");
11 }
12 
14  const std::string & name,
16  int size,
17  const KnowledgeUpdateSettings & settings)
18  : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
19 {
22  "NativeDoubleVector::constructor called for %s[%d]\n",
23  name.c_str (), size);
24 
25  vector_ = knowledge.get_ref (name, settings_);
26  if (size >= 0)
27  {
28  resize (size);
29  }
30 }
31 
33  const std::string & name,
35  int size,
36  const KnowledgeUpdateSettings & settings)
37 : context_ (knowledge.get_context ())
38 {
41  "NativeDoubleVector::constructor called for %s[%d]\n",
42  name.c_str (), size);
43 
44  vector_ = knowledge.get_ref (name, settings);
45  if (size >= 0)
46  {
47  resize (size);
48  }
49 }
50 
52  const NativeDoubleVector & rhs)
53 : BaseContainer (rhs), context_ (rhs.context_),
54  vector_ (rhs.vector_)
55 {
58  "NativeDoubleVector::copy constructor called on %s\n",
59  rhs.name_.c_str ());
60 }
61 
62 
64 {
67  "NativeDoubleVector::destructor called on %s\n",
68  this->name_.c_str ());
69 }
70 
71 void
73 {
76  "NativeDoubleVector::modify called\n");
77 
78  ContextGuard context_guard (*context_);
79  if (context_ && name_ != "")
80  {
83  "NativeDoubleVector::modify: context is valid. Marking %s.\n",
84  this->name_.c_str ());
85 
87  }
88 }
89 
92  void)
93 {
94  std::stringstream result;
95 
96  result << "Native Double Vector: ";
97 
98  if (context_)
99  {
100  ContextGuard context_guard (*context_);
101  MADARA_GUARD_TYPE guard (mutex_);
102 
103  result << this->name_;
104  result << " [" << size () << "]";
105  result << " = " << context_->get (vector_).to_string ();
106  }
107 
108  return result.str ();
109 }
110 
111 void
113 {
114  modify ();
115 }
116 
119  void)
120 {
121  return get_debug_info ();
122 }
123 
126 {
129  "NativeDoubleVector::clone: cloning %s\n",
130  this->name_.c_str ());
131 
132  return new NativeDoubleVector (*this);
133 }
134 
135 void
137  const NativeDoubleVector & rhs)
138 {
139  if (this != &rhs)
140  {
141  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
142 
145  "NativeDoubleVector::assignment: %s: copying from %s.\n",
146  this->name_.c_str (),
147  rhs.name_.c_str ());
148 
149  this->context_ = rhs.context_;
150  this->name_ = rhs.name_;
151  this->settings_ = rhs.settings_;
152  this->vector_ = rhs.vector_;
153  }
154 }
155 
156 void
158  type value)
159 {
160  if (context_ && name_ != "")
161  {
164  "NativeDoubleVector::push_back: %s: valid context, pushing.\n",
165  this->name_.c_str ());
166 
167  ContextGuard context_guard (*context_);
168  MADARA_GUARD_TYPE guard (mutex_);
169 
170  size_t i = size ();
171  resize ((int)i + 1);
172  set (i, value);
173  }
174 }
175 
176 void
178  size_t size)
179 {
180  if (context_ && name_ != "")
181  {
182  ContextGuard context_guard (*context_);
183  MADARA_GUARD_TYPE guard (mutex_);
184 
187  "NativeDoubleVector::resize: %s: resizing to %d\n",
188  this->name_.c_str (),
189  (int)size);
190 
192 
193  value.resize (size);
194 
195  context_->set (vector_, value, settings_);
196  }
197 }
198 
199 size_t
201 {
202  ContextGuard context_guard (*context_);
203  MADARA_GUARD_TYPE guard (mutex_);
204 
205  return context_->get (vector_, settings_).size ();
206 }
207 
208 void
210  const std::string & var_name,
212 {
213  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
214  {
215  context_ = &(knowledge.get_context ());
216 
217  ContextGuard context_guard (*context_);
218  MADARA_GUARD_TYPE guard (mutex_);
219 
222  "NativeDoubleVector::set_name: setting name to %s\n",
223  var_name.c_str ());
224 
225  name_ = var_name;
226 
227  vector_ = knowledge.get_ref (var_name, settings_);
228 
229  if (size > 0)
230  resize (size_t (size));
231  }
232 }
233 
234 void
236  const std::string & var_name,
237  Variables & knowledge, int size)
238 {
239  if (context_ != knowledge.get_context () || name_ != var_name)
240  {
241  context_ = knowledge.get_context ();
242 
243  ContextGuard context_guard (*context_);
244  MADARA_GUARD_TYPE guard (mutex_);
245 
248  "NativeDoubleVector::set_name: setting name to %s\n",
249  var_name.c_str ());
250 
251  name_ = var_name;
252 
253  vector_ = knowledge.get_ref (var_name, settings_);
254 
255  if (size > 0)
256  resize (size_t (size));
257  }
258 }
259 
260 void
262  const std::string & var_name,
264 {
265  if (context_ != &knowledge || name_ != var_name)
266  {
267  context_ = &knowledge;
268 
269  ContextGuard context_guard (*context_);
270  MADARA_GUARD_TYPE guard (mutex_);
271 
274  "NativeDoubleVector::set_name: setting name to %s\n",
275  var_name.c_str ());
276 
277  name_ = var_name;
278 
279  vector_ = knowledge.get_ref (var_name, settings_);
280 
281  if (size > 0)
282  resize (size_t (size));
283  }
284 }
285 
286 void
288  NativeDoubleVector & other)
289 {
290  if (context_ && other.context_)
291  {
292  ContextGuard context_guard (*context_);
293  ContextGuard other_context_guard (*other.context_);
294  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
295 
298  "NativeDoubleVector::exchange: %s exchanging with %s\n",
299  this->name_.c_str (),
300  other.name_.c_str ());
301 
303 
304  context_->set (other.vector_, context_->get (vector_), other.settings_);
305  context_->set (vector_, temp, settings_);
306  }
307 }
308 
309 void
311  NativeDoubleVector & other)
312 {
313  if (context_ && other.context_)
314  {
315  ContextGuard context_guard (*context_);
316  ContextGuard other_context_guard (*other.context_);
317  MADARA_GUARD_TYPE guard (mutex_);
318  MADARA_GUARD_TYPE guard2 (other.mutex_);
319 
322  "NativeDoubleVector::transfer_to: %s transfering to %s\n",
323  this->name_.c_str (),
324  other.name_.c_str ());
325 
326  size_t other_size = other.size ();
327  size_t this_size = this->size ();
328 
329  if (this_size > 0)
330  {
333  "NativeDoubleVector::transfer_to: %s has elements\n",
334  this->name_.c_str ());
335 
336  size_t size = other_size + this_size;
337  other.resize ((int)size);
338 
339  knowledge::KnowledgeRecord rhs (other.context_->get (other.vector_));
341 
342  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
343 
344  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
345  {
346  rhs.set_index (other_size, lhs.retrieve_index (i).to_double ());
347  }
348 
349  other.context_->set (other.vector_, rhs, other.settings_);
350 
351  this->resize (0);
352  }
353  }
354 }
355 
356 void
358  KnowledgeVector & target) const
359 {
360  if (context_)
361  {
362  ContextGuard context_guard (*context_);
363  MADARA_GUARD_TYPE guard (mutex_);
364 
367  "NativeDoubleVector::copy_to: %s: copying elements to vector\n",
368  this->name_.c_str ());
369 
370  target.resize (size ());
371 
372  for (size_t i = 0; i < target.size (); ++i)
373  {
374  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
375  }
376  }
377 }
378 
381  size_t index) const
382 {
384 
385  if (context_)
386  {
387  ContextGuard context_guard (*context_);
388  MADARA_GUARD_TYPE guard (mutex_);
389 
392  "NativeDoubleVector[]: %s: retrieving element from container\n",
393  this->name_.c_str ());
394 
395  result = context_->get (vector_, settings_);
396 
397  if (index < result.size ())
398  result = result.retrieve_index (index);
399  }
400 
401  return result.to_double ();
402 }
403 
404 int
406  size_t index,
407  type value)
408 {
409  int result = -1;
410 
411  if (context_)
412  {
413  ContextGuard context_guard (*context_);
414  MADARA_GUARD_TYPE guard (mutex_);
415 
418  "NativeDoubleVector::set: %s: setting element [%d] to %f\n",
419  this->name_.c_str (),
420  (int)index, value);
421 
422  result = context_->set_index (vector_, index, value, settings_);
423  }
424 
425  return result;
426 }
427 
428 int
430  size_t index,
431  type value,
432  const KnowledgeUpdateSettings & settings)
433 {
434  int result = -1;
435 
436  if (context_)
437  {
438  ContextGuard context_guard (*context_);
439  MADARA_GUARD_TYPE guard (mutex_);
440 
443  "NativeDoubleVector::set: %s: setting element [%d] "
444  "to %f with custom settings\n",
445  this->name_.c_str (),
446  (int)index, value);
447 
448  result = context_->set_index (vector_, index, value, settings);
449  }
450 
451  return result;
452 }
453 
454 int
456  const std::vector <type> & value)
457 {
458  int result = -1;
459 
460  if (context_)
461  {
462  ContextGuard context_guard (*context_);
463  MADARA_GUARD_TYPE guard (mutex_);
464 
467  "NativeDoubleVector::set: %s: setting all elements\n",
468  this->name_.c_str ());
469 
470  context_->set (vector_, value, settings_);
471  }
472 
473  return result;
474 }
475 
476 int
478  const std::vector <type> & value,
479  const KnowledgeUpdateSettings & settings)
480 {
481  int result = -1;
482 
483  if (context_)
484  {
485  ContextGuard context_guard (*context_);
486  MADARA_GUARD_TYPE guard (mutex_);
487 
490  "NativeDoubleVector::set: %s: setting all elements "
491  "with custom settings\n",
492  this->name_.c_str ());
493 
494  context_->set (vector_, value, settings);
495  }
496 
497  return result;
498 }
499 
500 void
502  size_t /*index*/,
503  uint32_t quality,
504  const KnowledgeReferenceSettings & settings)
505 {
506  if (context_)
507  {
508  ContextGuard context_guard (*context_);
509  MADARA_GUARD_TYPE guard (mutex_);
510 
513  "NativeDoubleVector::set: %s: setting quality of knowledge\n",
514  this->name_.c_str ());
515 
516  context_->set_quality (name_, quality, true, settings);
517  }
518 }
519 
522  size_t index) const
523 {
526  "NativeDoubleVector::to_record: %s: retrieving record\n",
527  this->name_.c_str ());
528 
530 
531  result = result.retrieve_index (index);
532  return result;
533 }
534 
537  void) const
538 {
541  "NativeDoubleVector::to_record: %s: retrieving record\n",
542  this->name_.c_str ());
543 
544  return context_->get (this->vector_, settings_);
545 }
546 
547 bool
549 {
550  bool result (false);
551 
553  "NativeDoubleVector::is_true: %s: Checking for non-zero value\n",
554  this->name_.c_str ());
555 
556  if (context_)
557  {
558  ContextGuard context_guard (*context_);
559  MADARA_GUARD_TYPE guard (mutex_);
560  result = context_->get (vector_, settings_).is_true ();
561  }
562 
564  "NativeDoubleVector::is_true: %s: final result is %d\n",
565  this->name_.c_str (),
566  (int)result);
567 
568  return result;
569 }
570 
571 bool
573 {
574  return !is_true ();
575 }
576 
577 
578 bool
580 {
581  return is_true ();
582 }
583 
584 bool
586 {
587  return is_false ();
588 }
This class encapsulates an entry in a KnowledgeBase.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool is_true(void) const
Checks to see if the record is true.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
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
double to_double(void) const
converts the value to a float/double
NativeDoubleVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
std::string name_
Prefix of variable.
bool is_true(void) const
Determines if all values in the vector are true.
virtual BaseContainer * clone(void) const
Clones this container.
double type
trait that describes the value type
This class stores variables and their values for use by any entity needing state information in a thr...
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 ...
size_t size(void) const
Returns the size of the vector.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
void resize(size_t size)
Resizes the vector.
MADARA_LOCK_TYPE mutex_
guard for access and changes
virtual void modify_(void)
Polymorphic modify method used by collection containers.
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.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
type operator[](size_t index) const
Retrieves a copy of the record from the map.
void transfer_to(NativeDoubleVector &other)
Transfers elements from this vector to another.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
::std::vector< KnowledgeRecord > KnowledgeVector
ThreadSafeContext * context_
Variable context that we are modifying.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
VariableReference vector_
Reference to the size field of the vector space.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
bool is_false(void) const
Determines if the value of the vector is false.
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.
This class stores a vector of doubles inside of KaRL.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
static constexpr struct madara::knowledge::tags::string_t string
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
void operator=(const NativeDoubleVector &rhs)
Assignment operator.
void exchange(NativeDoubleVector &other)
Exchanges the vector at this location with the vector at another location.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
void modify(void)
Mark the value as modified.
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.
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.
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
virtual 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(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.