MADARA  3.2.3
StringVector.cpp
Go to the documentation of this file.
1 #include "StringVector.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 StringVector & rhs)
42  : BaseContainer (rhs), context_ (rhs.context_),
43  vector_ (rhs.vector_),
44  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 << "String 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 
103 void
105 {
106  modify ();
107 }
108 
111 {
112  return get_debug_info ();
113 }
114 
117 {
118  return new StringVector (*this);
119 }
120 
121 void
123 {
124  if (context_ && name_ != "" && index < vector_.size ())
125  {
126  ContextGuard context_guard (*context_);
127  context_->mark_modified (vector_[index]);
128  }
129 }
130 
131 void
133  const StringVector & rhs)
134 {
135  if (this != &rhs)
136  {
137  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
138 
139  this->context_ = rhs.context_;
140  this->name_ = rhs.name_;
141  this->settings_ = rhs.settings_;
142  this->size_ = rhs.size_;
143  this->vector_ = rhs.vector_;
144  }
145 }
146 
149 {
150  VariableReference ref;
151 
152  if (context_ && name_ != "")
153  {
154  KnowledgeUpdateSettings keep_local (true);
155  std::stringstream buffer;
156 
157  ContextGuard context_guard (*context_);
158  MADARA_GUARD_TYPE guard (mutex_);
159 
160  buffer << name_;
161  buffer << delimiter_;
162  buffer << "size";
163 
164  ref = context_->get_ref (buffer.str (), keep_local);
165  }
166 
167  return ref;
168 }
169 
170 
171 void
173 {
174  if (context_ && name_ != "")
175  {
176  ContextGuard context_guard (*context_);
177  MADARA_GUARD_TYPE guard (mutex_);
178 
179  if (!size_.is_valid ())
180  {
181  size_ = get_size_ref ();
182  }
183 
184  size_t i = size ();
185  resize ((int)i + 1);
186  set (i, value);
187  }
188 }
189 
190 void
192  int size, bool delete_vars)
193 {
194  if (context_ && name_ != "")
195  {
196  ContextGuard context_guard (*context_);
197  MADARA_GUARD_TYPE guard (mutex_);
198 
199  if (!size_.is_valid ())
200  {
201  size_ = get_size_ref ();
202  }
203 
204  if (size >= 0)
205  {
206  size_t old_size = vector_.size ();
207 
208  if (old_size != (size_t)size)
209  {
210  vector_.resize (size);
211 
213 
214  if ((size_t)size > old_size)
215  {
216  for (; old_size < (size_t)size; ++old_size)
217  {
218  std::stringstream buffer;
219  buffer << name_;
220  buffer << delimiter_;
221  buffer << old_size;
222  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
223  }
224  }
225  else if (delete_vars)
226  {
227  for (; (size_t)size < old_size; ++size)
228  {
229  std::stringstream buffer;
230  buffer << name_;
231  buffer << delimiter_;
232  buffer << size;
233 
234  context_->delete_variable (buffer.str (), settings_);
235  }
236  }
237  }
238  }
239  else
240  {
241  // dynamically allocate size from the context
242  size_t cur_size =
244 
245  size_t old_size = vector_.size ();
246 
247  if (old_size != cur_size)
248  {
249  vector_.resize (cur_size);
250 
251  if (cur_size > old_size)
252  {
253  for (; old_size < cur_size; ++old_size)
254  {
255  std::stringstream buffer;
256  buffer << name_;
257  buffer << delimiter_;
258  buffer << old_size;
259  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
260  }
261  }
262  else if (delete_vars)
263  {
264  for (; cur_size < old_size; ++cur_size)
265  {
266  std::stringstream buffer;
267  buffer << name_;
268  buffer << delimiter_;
269  buffer << cur_size;
270 
271  context_->delete_variable (buffer.str (), settings_);
272  }
273  }
274  }
275  }
276  }
277 }
278 
279 size_t
281 {
282  MADARA_GUARD_TYPE guard (mutex_);
283  return vector_.size ();
284 }
285 
286 void
288  const std::string & var_name,
290 {
291  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
292  {
293  context_ = &(knowledge.get_context ());
294 
295  ContextGuard context_guard (*context_);
296  MADARA_GUARD_TYPE guard (mutex_);
297 
298  name_ = var_name;
299 
300  vector_.clear ();
301 
302  size_ = get_size_ref ();
303 
304  resize (size);
305  }
306 }
307 
308 void
310  const std::string & var_name,
311  Variables & knowledge, int size)
312 {
313  if (context_ != knowledge.get_context () || name_ != var_name)
314  {
315  context_ = knowledge.get_context ();
316 
317  ContextGuard context_guard (*context_);
318  MADARA_GUARD_TYPE guard (mutex_);
319 
320  name_ = var_name;
321 
322  vector_.clear ();
323  resize (size);
324  }
325 }
326 
327 void
329  const std::string & var_name,
331 {
332  if (context_ != &knowledge || name_ != var_name)
333  {
334  context_ = &knowledge;
335 
336  ContextGuard context_guard (*context_);
337  MADARA_GUARD_TYPE guard (mutex_);
338 
339  name_ = var_name;
340 
341  vector_.clear ();
342  resize (size);
343  }
344 }
345 
346 void
348 const std::string & delimiter)
349 {
350  delimiter_ = delimiter;
351  if (context_)
352  {
353  ContextGuard context_guard (*context_);
354  MADARA_GUARD_TYPE guard (mutex_);
355 
356  vector_.clear ();
357  resize (-1);
358  }
359 }
360 
361 
364 {
365  return delimiter_;
366 }
367 
368 void
370  StringVector & other, bool refresh_keys, bool delete_keys)
371 {
372  if (context_ && other.context_)
373  {
374  std::lock(*context_, *other.context_, mutex_, other.mutex_);
375 
376  ContextGuard context_guard (*context_, std::adopt_lock);
377  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
378  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
379  guard2 (other.mutex_, std::adopt_lock);
380 
381 
382  if (refresh_keys)
383  {
384  other.resize ();
385  this->resize ();
386  }
387 
388  size_t other_size = other.vector_.size ();
389  size_t this_size = this->vector_.size ();
390 
391  for (size_t i = 0; i < this_size; ++i)
392  {
393  // temp = this[i];
395 
396  if (i < other_size)
397  {
398  // this[i] = other[i];
399  context_->set (this->vector_[i],
400  context_->get (other.vector_[i], other.settings_),
401  settings_);
402 
403  // other[i] = temp;
404  other.context_->set (other.vector_[i], temp, other.settings_);
405  }
406  else
407  {
408  if (delete_keys)
409  {
410  std::stringstream buffer;
411  buffer << this->name_;
412  buffer << delimiter_;
413  buffer << i;
414  this->context_->delete_variable (buffer.str (), other.settings_);
415  }
416  else
417  {
419  this->context_->set (this->vector_[i], zero, this->settings_);
420  }
421 
422  {
423  std::stringstream buffer;
424  buffer << other.name_;
425  buffer << delimiter_;
426  buffer << i;
427 
428  // other[i] = temp;
429  other.context_->set (buffer.str (), temp, other.settings_);
430  }
431  }
432 
433  }
434 
435  // copy the other vector's elements to this vector's location
436  for (size_t i = this_size; i < other_size; ++i)
437  {
438  std::stringstream buffer;
439  buffer << this->name_;
440  buffer << delimiter_;
441  buffer << i;
442  context_->set (buffer.str (),
443  other.context_->get (other.vector_[i], other.settings_), this->settings_);
444  }
445 
446  // set the size appropriately
447  this->context_->set (this->size_,
448  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
449  other.context_->set (other.size_,
451 
452  if (refresh_keys)
453  {
454  this->resize (-1, true);
455  other.resize (-1, true);
456  }
457  }
458 }
459 
460 void
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 <type> & 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_string ();
539 }
540 
543  size_t index) const
544 {
546 
547  if (index < vector_.size () && context_)
548  {
549  ContextGuard context_guard (*context_);
550  MADARA_GUARD_TYPE guard (mutex_);
551  result = context_->get (vector_[index], settings_);
552  }
553 
554  return result;
555 }
556 
557 bool
559  size_t index) const
560 {
561  bool result (false);
562 
563  if (index < vector_.size () && context_)
564  {
565  ContextGuard context_guard (*context_);
566  MADARA_GUARD_TYPE guard (mutex_);
567  result = context_->exists (vector_[index]);
568  }
569 
570  return result;
571 }
572 
573 int
575  size_t index,
576  const type & value)
577 {
578  int result = -1;
579 
580  if (index < vector_.size () && context_)
581  {
582  ContextGuard context_guard (*context_);
583  MADARA_GUARD_TYPE guard (mutex_);
584  result = context_->set (vector_[index], value, settings_);
585  }
586 
587  return result;
588 }
589 
590 int
592  size_t index,
593  const type & value,
594  const KnowledgeUpdateSettings & settings)
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 
608 int
610  const std::vector <type> & value)
611 {
612  int result = -1;
613 
614  if (context_)
615  {
616  ContextGuard context_guard (*context_);
617  MADARA_GUARD_TYPE guard (mutex_);
618 
619  if (vector_.size () < value.size ())
620  resize ((int)value.size (), false);
621 
622  for (size_t i = 0; i < value.size (); ++i)
623  {
624  context_->set (vector_[i], value[i], settings_);
625  }
626 
627  result = 0;
628  }
629 
630  return result;
631 }
632 
633 int
635  const std::vector <type> & value,
636  const KnowledgeUpdateSettings & settings)
637 {
638  int result = -1;
639 
640  if (context_)
641  {
642  ContextGuard context_guard (*context_);
643  MADARA_GUARD_TYPE guard (mutex_);
644  if (vector_.size () < value.size ())
645  resize ((int)value.size (), false);
646 
647  for (size_t i = 0; i < value.size (); ++i)
648  {
649  context_->set (vector_[i], value[i], settings);
650  }
651 
652  result = 0;
653  }
654 
655  return result;
656 }
657 
658 void
660  size_t index,
661  uint32_t quality,
662  const KnowledgeReferenceSettings & settings)
663 {
664  if (index < vector_.size () && context_)
665  {
666  ContextGuard context_guard (*context_);
667  MADARA_GUARD_TYPE guard (mutex_);
668  context_->set_quality (vector_[index].get_name (), quality,
669  true, settings);
670  }
671 }
672 
673 bool
675 {
676  bool result (false);
677 
679  "StringVector::is_true: Checking for truth\n");
680 
681  if (context_)
682  {
683  ContextGuard context_guard (*context_);
684  MADARA_GUARD_TYPE guard (mutex_);
685 
686  result = true;
687 
689  "StringVector::is_true: context was not null. Result changed to %d\n",
690  (int)result);
691 
692  for (size_t index = 0; index < vector_.size (); ++index)
693  {
694 
696  "StringVector::is_true: checking index %d, is_false of %d. \n",
697  (int)result, (int)context_->get (vector_[index]).is_false ());
698 
699  if (context_->get (vector_[index]).is_false ())
700  {
702  "StringVector::is_true: result is false, breaking\n");
703 
704  result = false;
705  break;
706  }
707  }
708 
709  if (vector_.size () == 0)
710  result = false;
711  }
712 
714  "StringVector::is_true: final result is %d\n", (int)result);
715 
716  return result;
717 }
718 
719 bool
721 {
722  return !is_true ();
723 }
724 
725 
726 bool
728 {
729  return is_true ();
730 }
731 
732 bool
734 {
735  return is_false ();
736 }
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.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
VariableReference get_size_ref(void)
Returns a reference to the size field of the current name.
std::string get_name(void) const
Returns the name of the container.
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::string name_
Prefix of variable.
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: StringVector.h:381
virtual BaseContainer * clone(void) const
Clones this container.
This class stores variables and their values for use by any entity needing state information in a thr...
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: StringVector.h:366
bool is_false(void) const
Determines if the value of the vector is false.
void transfer_to(StringVector &other)
Transfers elements from this vector to another.
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.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:31
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
::std::vector< KnowledgeRecord > KnowledgeVector
void operator=(const StringVector &rhs)
Assignment operator.
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
std::vector< VariableReference > vector_
Values of the array.
Definition: StringVector.h:371
static struct madara::knowledge::tags::string_t string
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
void modify(void)
Mark the vector as modified.
StringVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: StringVector.cpp:5
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Integer to_integer(void) const
converts the value to an integer.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
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.
VariableReference size_
Reference to the size field of the vector space.
Definition: StringVector.h:376
bool is_true(void) const
Determines if all values in the vector are true.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
int set(size_t index, const type &value)
Sets a knowledge variable to a specified value.
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.
Settings for applying knowledge updates.
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 ...
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.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
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.
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 exchange(StringVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
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...
std::string type
trait that describes the value type
Definition: StringVector.h:35
size_t size(void) const
Returns the size of the local vector.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.