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