MADARA  3.1.8
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  ContextGuard context_guard (*context_);
375  ContextGuard other_context_guard (*other.context_);
376  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
377 
378  if (refresh_keys)
379  {
380  other.resize ();
381  this->resize ();
382  }
383 
384  size_t other_size = other.vector_.size ();
385  size_t this_size = this->vector_.size ();
386 
387  for (size_t i = 0; i < this_size; ++i)
388  {
389  // temp = this[i];
391 
392  if (i < other_size)
393  {
394  // this[i] = other[i];
395  context_->set (this->vector_[i],
396  context_->get (other.vector_[i], other.settings_),
397  settings_);
398 
399  // other[i] = temp;
400  other.context_->set (other.vector_[i], temp, other.settings_);
401  }
402  else
403  {
404  if (delete_keys)
405  {
406  std::stringstream buffer;
407  buffer << this->name_;
408  buffer << delimiter_;
409  buffer << i;
410  this->context_->delete_variable (buffer.str (), other.settings_);
411  }
412  else
413  {
415  this->context_->set (this->vector_[i], zero, this->settings_);
416  }
417 
418  {
419  std::stringstream buffer;
420  buffer << other.name_;
421  buffer << delimiter_;
422  buffer << i;
423 
424  // other[i] = temp;
425  other.context_->set (buffer.str (), temp, other.settings_);
426  }
427  }
428 
429  }
430 
431  // copy the other vector's elements to this vector's location
432  for (size_t i = this_size; i < other_size; ++i)
433  {
434  std::stringstream buffer;
435  buffer << this->name_;
436  buffer << delimiter_;
437  buffer << i;
438  context_->set (buffer.str (),
439  other.context_->get (other.vector_[i], other.settings_), this->settings_);
440  }
441 
442  // set the size appropriately
443  this->context_->set (this->size_,
444  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
445  other.context_->set (other.size_,
447 
448  if (refresh_keys)
449  {
450  this->resize (-1, true);
451  other.resize (-1, true);
452  }
453  }
454 }
455 
456 void
458 {
459  if (context_ && other.context_)
460  {
461  ContextGuard context_guard (*context_);
462  ContextGuard other_context_guard (*other.context_);
463  MADARA_GUARD_TYPE guard (mutex_);
464  MADARA_GUARD_TYPE guard2 (other.mutex_);
465 
466  size_t other_size = other.vector_.size ();
467  size_t this_size = this->vector_.size ();
468 
469  size_t size = other_size + this_size;
470  other.resize ((int)size);
471 
472  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
473  {
474  other.context_->set (other.vector_[j], (*this)[i], other.settings_);
475  }
476 
477  this->resize (0, true);
478  }
479 }
480 
481 void
483  KnowledgeVector & target) const
484 {
485  if (context_)
486  {
487  ContextGuard context_guard (*context_);
488  MADARA_GUARD_TYPE guard (mutex_);
489 
490  target.resize (vector_.size ());
491 
492  for (size_t i = 0; i < vector_.size (); ++i)
493  {
494  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
495  }
496  }
497 }
498 
499 void
501 std::vector <type> & target) const
502 {
503  if (context_)
504  {
505  ContextGuard context_guard (*context_);
506  MADARA_GUARD_TYPE guard (mutex_);
507 
508  target.resize (vector_.size ());
509 
510  for (size_t i = 0; i < vector_.size (); ++i)
511  {
512  target[i] = (*this)[i];
513  }
514  }
515 }
516 
519  size_t index) const
520 {
522  KnowledgeUpdateSettings keep_local (true);
523 
524  if (index < vector_.size () && context_)
525  {
526  ContextGuard context_guard (*context_);
527  MADARA_GUARD_TYPE guard (mutex_);
528  result = context_->get (vector_[index], keep_local);
529  }
530 
531  return result.to_string ();
532 }
533 
536  size_t index) const
537 {
539 
540  if (index < vector_.size () && context_)
541  {
542  ContextGuard context_guard (*context_);
543  MADARA_GUARD_TYPE guard (mutex_);
544  result = context_->get (vector_[index], settings_);
545  }
546 
547  return result;
548 }
549 
550 bool
552  size_t index) const
553 {
554  bool result (false);
555 
556  if (index < vector_.size () && context_)
557  {
558  ContextGuard context_guard (*context_);
559  MADARA_GUARD_TYPE guard (mutex_);
560  result = context_->exists (vector_[index]);
561  }
562 
563  return result;
564 }
565 
566 int
568  size_t index,
569  const type & value)
570 {
571  int result = -1;
572 
573  if (index < vector_.size () && context_)
574  {
575  ContextGuard context_guard (*context_);
576  MADARA_GUARD_TYPE guard (mutex_);
577  result = context_->set (vector_[index], value, settings_);
578  }
579 
580  return result;
581 }
582 
583 int
585  size_t index,
586  const type & value,
587  const KnowledgeUpdateSettings & settings)
588 {
589  int result = -1;
590 
591  if (index < vector_.size () && context_)
592  {
593  ContextGuard context_guard (*context_);
594  MADARA_GUARD_TYPE guard (mutex_);
595  result = context_->set (vector_[index], value, settings);
596  }
597 
598  return result;
599 }
600 
601 int
603  const std::vector <type> & value)
604 {
605  int result = -1;
606 
607  if (context_)
608  {
609  ContextGuard context_guard (*context_);
610  MADARA_GUARD_TYPE guard (mutex_);
611 
612  if (vector_.size () < value.size ())
613  resize ((int)value.size (), false);
614 
615  for (size_t i = 0; i < value.size (); ++i)
616  {
617  context_->set (vector_[i], value[i], settings_);
618  }
619 
620  result = 0;
621  }
622 
623  return result;
624 }
625 
626 int
628  const std::vector <type> & value,
629  const KnowledgeUpdateSettings & settings)
630 {
631  int result = -1;
632 
633  if (context_)
634  {
635  ContextGuard context_guard (*context_);
636  MADARA_GUARD_TYPE guard (mutex_);
637  if (vector_.size () < value.size ())
638  resize ((int)value.size (), false);
639 
640  for (size_t i = 0; i < value.size (); ++i)
641  {
642  context_->set (vector_[i], value[i], settings);
643  }
644 
645  result = 0;
646  }
647 
648  return result;
649 }
650 
651 void
653  size_t index,
654  uint32_t quality,
655  const KnowledgeReferenceSettings & settings)
656 {
657  if (index < vector_.size () && context_)
658  {
659  ContextGuard context_guard (*context_);
660  MADARA_GUARD_TYPE guard (mutex_);
661  context_->set_quality (vector_[index].get_name (), quality,
662  true, settings);
663  }
664 }
665 
666 bool
668 {
669  bool result (false);
670 
672  "StringVector::is_true: Checking for truth\n");
673 
674  if (context_)
675  {
676  ContextGuard context_guard (*context_);
677  MADARA_GUARD_TYPE guard (mutex_);
678 
679  result = true;
680 
682  "StringVector::is_true: context was not null. Result changed to %d\n",
683  (int)result);
684 
685  for (size_t index = 0; index < vector_.size (); ++index)
686  {
687 
689  "StringVector::is_true: checking index %d, is_false of %d. \n",
690  (int)result, (int)context_->get (vector_[index]).is_false ());
691 
692  if (context_->get (vector_[index]).is_false ())
693  {
695  "StringVector::is_true: result is false, breaking\n");
696 
697  result = false;
698  break;
699  }
700  }
701 
702  if (vector_.size () == 0)
703  result = false;
704  }
705 
707  "StringVector::is_true: final result is %d\n", (int)result);
708 
709  return result;
710 }
711 
712 bool
714 {
715  return !is_true ();
716 }
717 
718 
719 bool
721 {
722  return is_true ();
723 }
724 
725 bool
727 {
728  return is_false ();
729 }
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 get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
void modify(void)
Mark the vector as modified.
StringVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: StringVector.cpp:5
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
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 ...
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.