MADARA  3.2.3
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  std::lock(*context_, *other.context_, mutex_, other.mutex_);
293 
294  ContextGuard context_guard (*context_, std::adopt_lock);
295  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
296  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
297  guard2 (other.mutex_, std::adopt_lock);
298 
299 
302  "NativeDoubleVector::exchange: %s exchanging with %s\n",
303  this->name_.c_str (),
304  other.name_.c_str ());
305 
307 
308  context_->set (other.vector_, context_->get (vector_), other.settings_);
309  context_->set (vector_, temp, settings_);
310  }
311 }
312 
313 void
315  NativeDoubleVector & other)
316 {
317  if (context_ && other.context_)
318  {
319  std::lock(*context_, *other.context_, mutex_, other.mutex_);
320 
321  ContextGuard context_guard (*context_, std::adopt_lock);
322  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
323  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
324  guard2 (other.mutex_, std::adopt_lock);
325 
326 
329  "NativeDoubleVector::transfer_to: %s transfering to %s\n",
330  this->name_.c_str (),
331  other.name_.c_str ());
332 
333  size_t other_size = other.size ();
334  size_t this_size = this->size ();
335 
336  if (this_size > 0)
337  {
340  "NativeDoubleVector::transfer_to: %s has elements\n",
341  this->name_.c_str ());
342 
343  size_t size = other_size + this_size;
344  other.resize ((int)size);
345 
346  knowledge::KnowledgeRecord rhs (other.context_->get (other.vector_));
348 
349  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
350 
351  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
352  {
353  rhs.set_index (other_size, lhs.retrieve_index (i).to_double ());
354  }
355 
356  other.context_->set (other.vector_, rhs, other.settings_);
357 
358  this->resize (0);
359  }
360  }
361 }
362 
363 void
365  KnowledgeVector & target) const
366 {
367  if (context_)
368  {
369  ContextGuard context_guard (*context_);
370  MADARA_GUARD_TYPE guard (mutex_);
371 
374  "NativeDoubleVector::copy_to: %s: copying elements to vector\n",
375  this->name_.c_str ());
376 
377  target.resize (size ());
378 
379  for (size_t i = 0; i < target.size (); ++i)
380  {
381  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
382  }
383  }
384 }
385 
388  size_t index) const
389 {
391 
392  if (context_)
393  {
394  ContextGuard context_guard (*context_);
395  MADARA_GUARD_TYPE guard (mutex_);
396 
399  "NativeDoubleVector[]: %s: retrieving element from container\n",
400  this->name_.c_str ());
401 
402  result = context_->get (vector_, settings_);
403 
404  if (index < result.size ())
405  result = result.retrieve_index (index);
406  }
407 
408  return result.to_double ();
409 }
410 
411 int
413  size_t index,
414  type value)
415 {
416  int result = -1;
417 
418  if (context_)
419  {
420  ContextGuard context_guard (*context_);
421  MADARA_GUARD_TYPE guard (mutex_);
422 
425  "NativeDoubleVector::set: %s: setting element [%d] to %f\n",
426  this->name_.c_str (),
427  (int)index, value);
428 
429  result = context_->set_index (vector_, index, value, settings_);
430  }
431 
432  return result;
433 }
434 
435 int
437  size_t index,
438  type value,
439  const KnowledgeUpdateSettings & settings)
440 {
441  int result = -1;
442 
443  if (context_)
444  {
445  ContextGuard context_guard (*context_);
446  MADARA_GUARD_TYPE guard (mutex_);
447 
450  "NativeDoubleVector::set: %s: setting element [%d] "
451  "to %f with custom settings\n",
452  this->name_.c_str (),
453  (int)index, value);
454 
455  result = context_->set_index (vector_, index, value, settings);
456  }
457 
458  return result;
459 }
460 
461 int
463  const std::vector <type> & value)
464 {
465  int result = -1;
466 
467  if (context_)
468  {
469  ContextGuard context_guard (*context_);
470  MADARA_GUARD_TYPE guard (mutex_);
471 
474  "NativeDoubleVector::set: %s: setting all elements\n",
475  this->name_.c_str ());
476 
477  context_->set (vector_, value, settings_);
478  }
479 
480  return result;
481 }
482 
483 int
485  const std::vector <type> & value,
486  const KnowledgeUpdateSettings & settings)
487 {
488  int result = -1;
489 
490  if (context_)
491  {
492  ContextGuard context_guard (*context_);
493  MADARA_GUARD_TYPE guard (mutex_);
494 
497  "NativeDoubleVector::set: %s: setting all elements "
498  "with custom settings\n",
499  this->name_.c_str ());
500 
501  context_->set (vector_, value, settings);
502  }
503 
504  return result;
505 }
506 
507 int
509  const double * values, uint32_t size)
510 {
511  int result = -1;
512 
513  if (context_)
514  {
515  ContextGuard context_guard (*context_);
516  MADARA_GUARD_TYPE guard (mutex_);
517 
520  "NativeDoubleVector::set: %s: setting all elements\n",
521  this->name_.c_str ());
522 
523  context_->set (vector_, values, size, settings_);
524  }
525 
526  return result;
527 }
528 
529 int
531  const double * values, uint32_t size,
532  const KnowledgeUpdateSettings & settings)
533 {
534  int result = -1;
535 
536  if (context_)
537  {
538  ContextGuard context_guard (*context_);
539  MADARA_GUARD_TYPE guard (mutex_);
540 
543  "NativeDoubleVector::set: %s: setting all elements "
544  "with custom settings\n",
545  this->name_.c_str ());
546 
547  context_->set (vector_, values, size, settings);
548  }
549 
550  return result;
551 }
552 
553 void
555  size_t /*index*/,
556  uint32_t quality,
557  const KnowledgeReferenceSettings & settings)
558 {
559  if (context_)
560  {
561  ContextGuard context_guard (*context_);
562  MADARA_GUARD_TYPE guard (mutex_);
563 
566  "NativeDoubleVector::set: %s: setting quality of knowledge\n",
567  this->name_.c_str ());
568 
569  context_->set_quality (name_, quality, true, settings);
570  }
571 }
572 
575  size_t index) const
576 {
579  "NativeDoubleVector::to_record: %s: retrieving record\n",
580  this->name_.c_str ());
581 
583 
584  result = result.retrieve_index (index);
585  return result;
586 }
587 
590  void) const
591 {
594  "NativeDoubleVector::to_record: %s: retrieving record\n",
595  this->name_.c_str ());
596 
597  return context_->get (this->vector_, settings_);
598 }
599 
600 bool
602 {
603  bool result (false);
604 
606  "NativeDoubleVector::is_true: %s: Checking for non-zero value\n",
607  this->name_.c_str ());
608 
609  if (context_)
610  {
611  ContextGuard context_guard (*context_);
612  MADARA_GUARD_TYPE guard (mutex_);
613  result = context_->get (vector_, settings_).is_true ();
614  }
615 
617  "NativeDoubleVector::is_true: %s: final result is %d\n",
618  this->name_.c_str (),
619  (int)result);
620 
621  return result;
622 }
623 
624 bool
626 {
627  return !is_true ();
628 }
629 
630 
631 bool
633 {
634  return is_true ();
635 }
636 
637 bool
639 {
640  return is_false ();
641 }
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 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.
static struct madara::knowledge::tags::string_t string
#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:45
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.
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.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
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.
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.
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.