MADARA  3.2.3
DoubleVector.cpp
Go to the documentation of this file.
1 #include "DoubleVector.h"
3 
4 
6  const KnowledgeUpdateSettings & settings,
7  const std::string & delimiter)
8  : BaseContainer ("", settings), context_ (0), delimiter_ (delimiter)
9 {
10 }
11 
13  const std::string & name,
15  int size,
16  bool delete_vars,
17  const KnowledgeUpdateSettings & settings,
18  const std::string & delimiter)
19  : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
20  delimiter_ (delimiter)
21 {
22  size_ = get_size_ref ();
23  resize (size, delete_vars);
24 }
25 
27  const std::string & name,
29  int size,
30  bool delete_vars,
31  const KnowledgeUpdateSettings & settings,
32  const std::string & delimiter)
33  : BaseContainer (name, settings), context_ (knowledge.get_context ()),
34  delimiter_ (delimiter)
35 {
36  size_ = get_size_ref ();
37  resize (size, delete_vars);
38 }
39 
41  const DoubleVector & rhs)
42 : BaseContainer (rhs), context_ (rhs.context_),
43  vector_ (rhs.vector_),
44  size_ (rhs.size_),
46 {
47 }
48 
49 
51 {
52 
53 }
54 
55 void
57 {
58  if (context_ && name_ != "")
59  {
60  ContextGuard context_guard (*context_);
61  for (size_t index = 0; index < vector_.size (); ++index)
62  context_->mark_modified (vector_[index]);
63 
65  }
66 }
67 
70 {
71  std::stringstream result;
72 
73  result << "Double Vector: ";
74 
75  if (context_)
76  {
77  ContextGuard context_guard (*context_);
78  MADARA_GUARD_TYPE guard (mutex_);
79  size_t elements = vector_.size ();
80 
81  result << this->name_;
82  result << " [" << elements << "]";
83  result << " = [";
84 
85  if (elements > 0)
86  {
87  result << context_->get (vector_[0]).to_string ();
88 
89  for (size_t index = 1; index < elements; ++index)
90  {
91  result << ", " << context_->get (vector_[index]).to_string ();
92  }
93  }
94 
95  result << "]";
96  }
97 
98  return result.str ();
99 }
100 
101 void
103 {
104  modify ();
105 }
106 
109 {
110  return get_debug_info ();
111 }
112 
115 {
116  return new DoubleVector (*this);
117 }
118 
119 void
121 {
122  if (context_ && name_ != "" && index < vector_.size ())
123  {
124  ContextGuard context_guard (*context_);
125  context_->mark_modified (vector_[index]);
126  }
127 }
128 
129 void
131  const DoubleVector & rhs)
132 {
133  if (this != &rhs)
134  {
135  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
136 
137  this->context_ = rhs.context_;
138  this->name_ = rhs.name_;
139  this->settings_ = rhs.settings_;
140  this->size_ = rhs.size_;
141  this->vector_ = rhs.vector_;
142  this->delimiter_ = rhs.delimiter_;
143  }
144 }
145 
146 void
148 {
149  if (context_ && name_ != "")
150  {
151  ContextGuard context_guard (*context_);
152  MADARA_GUARD_TYPE guard (mutex_);
153 
154  if (!size_.is_valid ())
155  {
156  size_ = get_size_ref ();
157  }
158 
159  size_t i = size ();
160  resize ((int)i + 1);
161  set (i, value);
162  }
163 }
164 
167 {
168  VariableReference ref;
169 
170  if (context_ && name_ != "")
171  {
172  KnowledgeUpdateSettings keep_local (true);
173  std::stringstream buffer;
174 
175  ContextGuard context_guard (*context_);
176  MADARA_GUARD_TYPE guard (mutex_);
177 
178  buffer << name_;
179  buffer << delimiter_;
180  buffer << "size";
181 
182  ref = context_->get_ref (buffer.str (), keep_local);
183  }
184 
185  return ref;
186 }
187 
188 void
190  int size, bool delete_vars)
191 {
192  if (context_ && name_ != "")
193  {
194  ContextGuard context_guard (*context_);
195  MADARA_GUARD_TYPE guard (mutex_);
196 
197  if (!size_.is_valid ())
198  {
199  size_ = get_size_ref ();
200  }
201 
202  if (size >= 0)
203  {
204  size_t old_size = vector_.size ();
205 
206  if (old_size != (size_t)size)
207  {
208  vector_.resize (size);
209 
211 
212  if ((size_t)size > old_size)
213  {
214  for (; old_size < (size_t)size; ++old_size)
215  {
216  std::stringstream buffer;
217  buffer << name_;
218  buffer << delimiter_;
219  buffer << old_size;
220  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
221  }
222  }
223  else if (delete_vars)
224  {
225  for (; (size_t)size < old_size; ++size)
226  {
227  std::stringstream buffer;
228  buffer << name_;
229  buffer << delimiter_;
230  buffer << size;
231 
232  context_->delete_variable (buffer.str (), settings_);
233  }
234  }
235  }
236  }
237  else
238  {
239  // dynamically allocate size from the context
240  size_t cur_size =
241  (size_t) context_->get (size_, settings_).to_integer ();
242 
243  size_t old_size = vector_.size ();
244 
245  if (old_size != cur_size)
246  {
247  vector_.resize (cur_size);
248 
249  if (cur_size > old_size)
250  {
251  for (; old_size < (size_t)cur_size; ++old_size)
252  {
253  std::stringstream buffer;
254  buffer << name_;
255  buffer << delimiter_;
256  buffer << old_size;
257  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
258  }
259  }
260  else if (delete_vars)
261  {
262  for (; (size_t)cur_size < old_size; ++cur_size)
263  {
264  std::stringstream buffer;
265  buffer << name_;
266  buffer << delimiter_;
267  buffer << cur_size;
268 
269  context_->delete_variable (buffer.str (), settings_);
270  }
271  }
272  }
273  }
274  }
275 }
276 
277 size_t
279 {
280  MADARA_GUARD_TYPE guard (mutex_);
281  return vector_.size ();
282 }
283 
284 void
286  const std::string & var_name,
288 {
289  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
290  {
291  context_ = &(knowledge.get_context ());
292 
293  ContextGuard context_guard (*context_);
294  MADARA_GUARD_TYPE guard (mutex_);
295 
296  name_ = var_name;
297 
298  vector_.clear ();
299 
300  size_ = get_size_ref ();
301 
302  resize (size);
303  }
304 }
305 
306 void
308  const std::string & var_name,
309  Variables & knowledge, int size)
310 {
311  if (context_ != knowledge.get_context () || name_ != var_name)
312  {
313  context_ = knowledge.get_context ();
314 
315  ContextGuard context_guard (*context_);
316  MADARA_GUARD_TYPE guard (mutex_);
317 
318  name_ = var_name;
319 
320  vector_.clear ();
321  resize (size);
322  }
323 }
324 
325 void
327  const std::string & var_name,
329 {
330  if (context_ != &knowledge || name_ != var_name)
331  {
332  context_ = &knowledge;
333 
334  ContextGuard context_guard (*context_);
335  MADARA_GUARD_TYPE guard (mutex_);
336 
337  name_ = var_name;
338 
339  vector_.clear ();
340  resize (size);
341  }
342 }
343 
344 void
346 const std::string & delimiter)
347 {
348  delimiter_ = delimiter;
349  if (context_)
350  {
351  ContextGuard context_guard (*context_);
352  MADARA_GUARD_TYPE guard (mutex_);
353 
354  vector_.clear ();
355  resize (-1);
356  }
357 }
358 
359 
362 {
363  return delimiter_;
364 }
365 
366 void
368  DoubleVector & other, bool refresh_keys, bool delete_keys)
369 {
370  if (context_ && other.context_)
371  {
372  std::lock(*context_, *other.context_, mutex_, other.mutex_);
373 
374  ContextGuard context_guard (*context_, std::adopt_lock);
375  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
376  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
377  guard2 (other.mutex_, std::adopt_lock);
378 
379 
380  if (refresh_keys)
381  {
382  other.resize ();
383  this->resize ();
384  }
385 
386  size_t other_size = other.vector_.size ();
387  size_t this_size = this->vector_.size ();
388 
389  for (size_t i = 0; i < this_size; ++i)
390  {
391  // temp = this[i];
393 
394  if (i < other_size)
395  {
396  // this[i] = other[i];
397  context_->set (this->vector_[i],
398  context_->get (other.vector_[i], other.settings_),
399  settings_);
400 
401  // other[i] = temp;
402  other.context_->set (other.vector_[i], temp, other.settings_);
403  }
404  else
405  {
406  if (delete_keys)
407  {
408  std::stringstream buffer;
409  buffer << this->name_;
410  buffer << delimiter_;
411  buffer << i;
412  this->context_->delete_variable (buffer.str (), other.settings_);
413  }
414  else
415  {
417  this->context_->set (this->vector_[i], zero, this->settings_);
418  }
419 
420  {
421  std::stringstream buffer;
422  buffer << other.name_;
423  buffer << delimiter_;
424  buffer << i;
425 
426  // other[i] = temp;
427  other.context_->set (buffer.str (), temp, other.settings_);
428  }
429  }
430 
431  }
432 
433  // copy the other vector's elements to this vector's location
434  for (size_t i = this_size; i < other_size; ++i)
435  {
436  std::stringstream buffer;
437  buffer << this->name_;
438  buffer << delimiter_;
439  buffer << i;
440  context_->set (buffer.str (),
441  other.context_->get (other.vector_[i],
442  other.settings_), this->settings_);
443  }
444 
445  // set the size appropriately
446  this->context_->set (this->size_,
447  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
448  other.context_->set (other.size_,
450 
451  if (refresh_keys)
452  {
453  this->resize (-1, true);
454  other.resize (-1, true);
455  }
456  }
457 }
458 
459 void
461  DoubleVector & other)
462 {
463  if (context_ && other.context_)
464  {
465  std::lock(*context_, *other.context_, mutex_, other.mutex_);
466 
467  ContextGuard context_guard (*context_, std::adopt_lock);
468  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
469  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
470  guard2 (other.mutex_, std::adopt_lock);
471 
472 
473  size_t other_size = other.vector_.size ();
474  size_t this_size = this->vector_.size ();
475 
476  size_t size = other_size + this_size;
477  other.resize ((int)size);
478 
479  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
480  {
481  other.context_->set (other.vector_[j], (*this)[i], other.settings_);
482  }
483 
484  this->resize (0, true);
485  }
486 }
487 
488 void
490  KnowledgeVector & target) const
491 {
492  if (context_)
493  {
494  ContextGuard context_guard (*context_);
495  MADARA_GUARD_TYPE guard (mutex_);
496 
497  target.resize (vector_.size ());
498 
499  for (size_t i = 0; i < vector_.size (); ++i)
500  {
501  target[i] = knowledge::KnowledgeRecord((*this)[i]);
502  }
503  }
504 }
505 
506 void
508 std::vector <double> & target) const
509 {
510  if (context_)
511  {
512  ContextGuard context_guard (*context_);
513  MADARA_GUARD_TYPE guard (mutex_);
514 
515  target.resize (vector_.size ());
516 
517  for (size_t i = 0; i < vector_.size (); ++i)
518  {
519  target[i] = (*this)[i];
520  }
521  }
522 }
523 
526  size_t index) const
527 {
529  KnowledgeUpdateSettings keep_local (true);
530 
531  if (index < vector_.size () && context_)
532  {
533  ContextGuard context_guard (*context_);
534  MADARA_GUARD_TYPE guard (mutex_);
535  result = context_->get (vector_[index], keep_local);
536  }
537 
538  return result.to_double ();
539 }
540 
543  size_t index) const
544 {
546  KnowledgeUpdateSettings keep_local (true);
547 
548  if (index < vector_.size () && context_)
549  {
550  ContextGuard context_guard (*context_);
551  MADARA_GUARD_TYPE guard (mutex_);
552  result = context_->get (vector_[index], keep_local);
553  }
554 
555  return result;
556 }
557 
560 {
562  KnowledgeUpdateSettings keep_local (true);
563 
564  // if we have something to actually set
565  if (vector_.size () > 0 && context_)
566  {
567  ContextGuard context_guard (*context_);
568  MADARA_GUARD_TYPE guard (mutex_);
569 
570  // set last element first so we're not constantly resizing
571  result.set_index (vector_.size () - 1,
572  context_->get (vector_[vector_.size () - 1], keep_local).to_double ());
573 
574  for (size_t i = 0; i < vector_.size () - 1; ++i)
575  {
576  result.set_index (i,
577  context_->get (vector_[i], keep_local).to_double ());
578  }
579  }
580 
581  return result;
582 }
583 
584 bool
586  size_t index) const
587 {
588  bool result (false);
589 
590  if (index < vector_.size () && context_)
591  {
592  ContextGuard context_guard (*context_);
593  MADARA_GUARD_TYPE guard (mutex_);
594  result = context_->exists (vector_[index]);
595  }
596 
597  return result;
598 }
599 
600 int
602 size_t index,
603 type value)
604 {
605  int result = -1;
606 
607  if (index < vector_.size () && context_)
608  {
609  ContextGuard context_guard (*context_);
610  MADARA_GUARD_TYPE guard (mutex_);
611  result = context_->set (vector_[index], value, settings_);
612  }
613 
614  return result;
615 }
616 
617 
618 int
620 const std::vector <type> & value)
621 {
622  int result = -1;
623 
624  if (context_)
625  {
626  ContextGuard context_guard (*context_);
627  MADARA_GUARD_TYPE guard (mutex_);
628  if (vector_.size () < value.size ())
629  resize ((int)value.size (), false);
630 
631  for (size_t i = 0; i < value.size (); ++i)
632  {
633  context_->set (vector_[i], value[i], settings_);
634  }
635 
636  result = 0;
637  }
638 
639  return result;
640 }
641 
642 int
644  size_t index,
645  type value,
646  const KnowledgeUpdateSettings & settings)
647 {
648  int result = -1;
649 
650  if (index < vector_.size () && context_)
651  {
652  ContextGuard context_guard (*context_);
653  MADARA_GUARD_TYPE guard (mutex_);
654  result = context_->set (vector_[index], value, settings);
655  }
656 
657  return result;
658 }
659 
660 
661 int
663  const std::vector <type> & value,
664  const KnowledgeUpdateSettings & settings)
665 {
666  int result = -1;
667 
668  if (context_)
669  {
670  ContextGuard context_guard (*context_);
671  MADARA_GUARD_TYPE guard (mutex_);
672  if (vector_.size () < value.size ())
673  resize ((int)value.size (), false);
674 
675  for (size_t i = 0; i < value.size (); ++i)
676  {
677  context_->set (vector_[i], value[i], settings);
678  }
679 
680  result = 0;
681  }
682 
683  return result;
684 }
685 
686 void
688  size_t index,
689  uint32_t quality,
690  const KnowledgeReferenceSettings & settings)
691 {
692  if (index < vector_.size () && context_)
693  {
694  ContextGuard context_guard (*context_);
695  MADARA_GUARD_TYPE guard (mutex_);
696  context_->set_quality (vector_[index].get_name (), quality,
697  true, settings);
698  }
699 }
700 
701 bool
703 {
704  bool result (false);
705 
707  "DoubleVector::is_true: Checking for truth\n");
708 
709  if (context_)
710  {
711  ContextGuard context_guard (*context_);
712  MADARA_GUARD_TYPE guard (mutex_);
713 
714  result = true;
715 
717  "DoubleVector::is_true: context was not null. Result changed to %d\n",
718  (int)result);
719 
720  for (size_t index = 0; index < vector_.size (); ++index)
721  {
722 
724  "DoubleVector::is_true: checking index %d, is_false of %d. \n",
725  (int)result, (int)context_->get (vector_[index]).is_false ());
726 
727  if (context_->get (vector_[index]).is_false ())
728  {
730  "DoubleVector::is_true: result is false, breaking\n");
731 
732  result = false;
733  break;
734  }
735  }
736 
737  if (vector_.size () == 0)
738  result = false;
739  }
740 
742  "DoubleVector::is_true: final result is %d\n", (int)result);
743 
744  return result;
745 }
746 
747 bool
749 {
750  return !is_true ();
751 }
752 
753 
754 bool
756 {
757  return is_true ();
758 }
759 
760 bool
762 {
763  return is_false ();
764 }
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.
void modify(void)
Mark the vector as modified.
double type
trait that describes the value type
Definition: DoubleVector.h:35
VariableReference size_
Reference to the size field of the vector space.
Definition: DoubleVector.h:374
std::string get_name(void) const
Returns the name of the container.
madara::knowledge::KnowledgeRecord KnowledgeRecord
type operator[](size_t index) const
Retrieves a copy of the record from the vector.
double to_double(void) const
converts the value to a float/double.
std::string name_
Prefix of variable.
VariableReference get_size_ref(void)
Returns a reference to the size field of the current name.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
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 local vector.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Optimized reference to a variable within the knowledge base.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
::std::vector< KnowledgeRecord > KnowledgeVector
bool is_true(void) const
Determines if all values in the vector are true.
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: DoubleVector.h:379
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
static struct madara::knowledge::tags::string_t string
bool is_false(void) const
Determines if the value of the vector is false.
void operator=(const DoubleVector &rhs)
Assignment operator.
void exchange(DoubleVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
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.
std::vector< VariableReference > vector_
Values of the array.
Definition: DoubleVector.h:369
void transfer_to(DoubleVector &other)
Transfers elements from this vector to another.
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.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
virtual BaseContainer * clone(void) const
Clones this container.
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 ...
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: DoubleVector.h:364
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
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.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
This class stores a vector of doubles inside of KaRL.
Definition: DoubleVector.h:31
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
DoubleVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: DoubleVector.cpp:5
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
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...
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.