MADARA  3.4.1
StringVector.cpp
Go to the documentation of this file.
1 #include "StringVector.h"
3 
5  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
6  : BaseContainer("", settings), context_(0), delimiter_(delimiter)
7 {
8 }
9 
11  const std::string& name, KnowledgeBase& knowledge, int size,
12  bool delete_vars, const KnowledgeUpdateSettings& settings,
13  const std::string& delimiter)
14  : BaseContainer(name, settings),
15  context_(&(knowledge.get_context())),
16  delimiter_(delimiter)
17 {
18  size_ = get_size_ref();
19  resize(size, delete_vars);
20 }
21 
23  const std::string& name, Variables& knowledge, int size, bool delete_vars,
24  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
25  : BaseContainer(name, settings),
26  context_(knowledge.get_context()),
27  delimiter_(delimiter)
28 {
29  size_ = get_size_ref();
30  resize(size, delete_vars);
31 }
32 
34  const StringVector& rhs)
35  : BaseContainer(rhs),
36  context_(rhs.context_),
37  vector_(rhs.vector_),
38  size_(rhs.size_),
39  delimiter_(rhs.delimiter_)
40 {
41 }
42 
44 
46 {
47  if (context_ && name_ != "")
48  {
49  ContextGuard context_guard(*context_);
50  for (size_t index = 0; index < vector_.size(); ++index)
51  context_->mark_modified(vector_[index]);
52 
53  context_->mark_modified(size_);
54  }
55 }
56 
58 {
59  std::stringstream result;
60 
61  result << "String Vector: ";
62 
63  if (context_)
64  {
65  ContextGuard context_guard(*context_);
66  MADARA_GUARD_TYPE guard(mutex_);
67  size_t elements = vector_.size();
68 
69  result << this->name_;
70  result << " [" << elements << "]";
71  result << " = [";
72 
73  if (elements > 0)
74  {
75  result << context_->get(vector_[0]).to_string();
76 
77  for (size_t index = 1; index < elements; ++index)
78  {
79  result << ", " << context_->get(vector_[index]).to_string();
80  }
81  }
82 
83  result << "]";
84  }
85 
86  return result.str();
87 }
88 
90 {
91  modify();
92 }
93 
95 {
96  return get_debug_info();
97 }
98 
101 {
102  return new StringVector(*this);
103 }
104 
106 {
107  if (context_ && name_ != "" && index < vector_.size())
108  {
109  ContextGuard context_guard(*context_);
110  context_->mark_modified(vector_[index]);
111  }
112 }
113 
115  const StringVector& rhs)
116 {
117  if (this != &rhs)
118  {
119  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
120 
121  this->context_ = rhs.context_;
122  this->name_ = rhs.name_;
123  this->settings_ = rhs.settings_;
124  this->size_ = rhs.size_;
125  this->vector_ = rhs.vector_;
126  }
127 }
128 
131 {
132  VariableReference ref;
133 
134  if (context_ && name_ != "")
135  {
136  KnowledgeUpdateSettings keep_local(true);
137  std::stringstream buffer;
138 
139  ContextGuard context_guard(*context_);
140  MADARA_GUARD_TYPE guard(mutex_);
141 
142  buffer << name_;
143  buffer << delimiter_;
144  buffer << "size";
145 
146  ref = context_->get_ref(buffer.str(), keep_local);
147  }
148 
149  return ref;
150 }
151 
153 {
154  if (context_ && name_ != "")
155  {
156  ContextGuard context_guard(*context_);
157  MADARA_GUARD_TYPE guard(mutex_);
158 
159  if (!size_.is_valid())
160  {
161  size_ = get_size_ref();
162  }
163 
164  size_t i = size();
165  resize((int)i + 1);
166  set(i, value);
167  }
168 }
169 
171  int size, bool delete_vars)
172 {
173  if (context_ && name_ != "")
174  {
175  ContextGuard context_guard(*context_);
176  MADARA_GUARD_TYPE guard(mutex_);
177 
178  if (!size_.is_valid())
179  {
180  size_ = get_size_ref();
181  }
182 
183  if (size >= 0)
184  {
185  size_t old_size = vector_.size();
186 
187  if (old_size != (size_t)size)
188  {
189  vector_.resize(size);
190 
191  context_->set(
192  size_, knowledge::KnowledgeRecord::Integer(size), settings_);
193 
194  if ((size_t)size > old_size)
195  {
196  for (; old_size < (size_t)size; ++old_size)
197  {
198  std::stringstream buffer;
199  buffer << name_;
200  buffer << delimiter_;
201  buffer << old_size;
202  vector_[old_size] = context_->get_ref(buffer.str(), settings_);
203  }
204  }
205  else if (delete_vars)
206  {
207  for (; (size_t)size < old_size; ++size)
208  {
209  std::stringstream buffer;
210  buffer << name_;
211  buffer << delimiter_;
212  buffer << size;
213 
214  context_->delete_variable(buffer.str(), settings_);
215  }
216  }
217  }
218  }
219  else
220  {
221  // dynamically allocate size from the context
222  size_t cur_size = context_->get(size_, settings_).to_integer();
223 
224  size_t old_size = vector_.size();
225 
226  if (old_size != cur_size)
227  {
228  vector_.resize(cur_size);
229 
230  if (cur_size > old_size)
231  {
232  for (; old_size < cur_size; ++old_size)
233  {
234  std::stringstream buffer;
235  buffer << name_;
236  buffer << delimiter_;
237  buffer << old_size;
238  vector_[old_size] = context_->get_ref(buffer.str(), settings_);
239  }
240  }
241  else if (delete_vars)
242  {
243  for (; cur_size < old_size; ++cur_size)
244  {
245  std::stringstream buffer;
246  buffer << name_;
247  buffer << delimiter_;
248  buffer << cur_size;
249 
250  context_->delete_variable(buffer.str(), settings_);
251  }
252  }
253  }
254  }
255  }
256 }
257 
259 {
260  MADARA_GUARD_TYPE guard(mutex_);
261  return vector_.size();
262 }
263 
265  const std::string& var_name, KnowledgeBase& knowledge, int size)
266 {
267  if (context_ != &(knowledge.get_context()) || name_ != var_name)
268  {
269  context_ = &(knowledge.get_context());
270 
271  ContextGuard context_guard(*context_);
272  MADARA_GUARD_TYPE guard(mutex_);
273 
274  name_ = var_name;
275 
276  vector_.clear();
277 
278  size_ = get_size_ref();
279 
280  resize(size);
281  }
282 }
283 
285  const std::string& var_name, Variables& knowledge, int size)
286 {
287  if (context_ != knowledge.get_context() || name_ != var_name)
288  {
289  context_ = knowledge.get_context();
290 
291  ContextGuard context_guard(*context_);
292  MADARA_GUARD_TYPE guard(mutex_);
293 
294  name_ = var_name;
295 
296  vector_.clear();
297  resize(size);
298  }
299 }
300 
302  const std::string& var_name, ThreadSafeContext& knowledge, int size)
303 {
304  if (context_ != &knowledge || name_ != var_name)
305  {
306  context_ = &knowledge;
307 
308  ContextGuard context_guard(*context_);
309  MADARA_GUARD_TYPE guard(mutex_);
310 
311  name_ = var_name;
312 
313  vector_.clear();
314  resize(size);
315  }
316 }
317 
319  const std::string& delimiter)
320 {
321  delimiter_ = delimiter;
322  if (context_)
323  {
324  ContextGuard context_guard(*context_);
325  MADARA_GUARD_TYPE guard(mutex_);
326 
327  vector_.clear();
328  resize(-1);
329  }
330 }
331 
333 {
334  return delimiter_;
335 }
336 
338  StringVector& other, bool refresh_keys, bool delete_keys)
339 {
340  if (context_ && other.context_)
341  {
342  std::lock(*context_, *other.context_, mutex_, other.mutex_);
343 
344  ContextGuard context_guard(*context_, std::adopt_lock);
345  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
346  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
347  guard2(other.mutex_, std::adopt_lock);
348 
349  if (refresh_keys)
350  {
351  other.resize();
352  this->resize();
353  }
354 
355  size_t other_size = other.vector_.size();
356  size_t this_size = this->vector_.size();
357 
358  for (size_t i = 0; i < this_size; ++i)
359  {
360  // temp = this[i];
362  context_->get(this->vector_[i], settings_);
363 
364  if (i < other_size)
365  {
366  // this[i] = other[i];
367  context_->set(this->vector_[i],
368  context_->get(other.vector_[i], other.settings_), settings_);
369 
370  // other[i] = temp;
371  other.context_->set(other.vector_[i], temp, other.settings_);
372  }
373  else
374  {
375  if (delete_keys)
376  {
377  std::stringstream buffer;
378  buffer << this->name_;
379  buffer << delimiter_;
380  buffer << i;
381  this->context_->delete_variable(buffer.str(), other.settings_);
382  }
383  else
384  {
386  this->context_->set(this->vector_[i], zero, this->settings_);
387  }
388 
389  {
390  std::stringstream buffer;
391  buffer << other.name_;
392  buffer << delimiter_;
393  buffer << i;
394 
395  // other[i] = temp;
396  other.context_->set(buffer.str(), temp, other.settings_);
397  }
398  }
399  }
400 
401  // copy the other vector's elements to this vector's location
402  for (size_t i = this_size; i < other_size; ++i)
403  {
404  std::stringstream buffer;
405  buffer << this->name_;
406  buffer << delimiter_;
407  buffer << i;
408  context_->set(buffer.str(),
409  other.context_->get(other.vector_[i], other.settings_),
410  this->settings_);
411  }
412 
413  // set the size appropriately
414  this->context_->set(this->size_,
415  knowledge::KnowledgeRecord::Integer(other_size), this->settings_);
416  other.context_->set(other.size_,
418 
419  if (refresh_keys)
420  {
421  this->resize(-1, true);
422  other.resize(-1, true);
423  }
424  }
425 }
426 
428  StringVector& other)
429 {
430  if (context_ && other.context_)
431  {
432  std::lock(*context_, *other.context_, mutex_, other.mutex_);
433 
434  ContextGuard context_guard(*context_, std::adopt_lock);
435  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
436  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
437  guard2(other.mutex_, std::adopt_lock);
438 
439  size_t other_size = other.vector_.size();
440  size_t this_size = this->vector_.size();
441 
442  size_t size = other_size + this_size;
443  other.resize((int)size);
444 
445  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
446  {
447  other.context_->set(other.vector_[j], (*this)[i], other.settings_);
448  }
449 
450  this->resize(0, true);
451  }
452 }
453 
455  KnowledgeVector& target) const
456 {
457  if (context_)
458  {
459  ContextGuard context_guard(*context_);
460  MADARA_GUARD_TYPE guard(mutex_);
461 
462  target.resize(vector_.size());
463 
464  for (size_t i = 0; i < vector_.size(); ++i)
465  {
466  target[i] = knowledge::KnowledgeRecord((*this)[i]);
467  }
468  }
469 }
470 
472  std::vector<type>& target) const
473 {
474  if (context_)
475  {
476  ContextGuard context_guard(*context_);
477  MADARA_GUARD_TYPE guard(mutex_);
478 
479  target.resize(vector_.size());
480 
481  for (size_t i = 0; i < vector_.size(); ++i)
482  {
483  target[i] = (*this)[i];
484  }
485  }
486 }
487 
490 {
492  KnowledgeUpdateSettings keep_local(true);
493 
494  if (index < vector_.size() && context_)
495  {
496  ContextGuard context_guard(*context_);
497  MADARA_GUARD_TYPE guard(mutex_);
498  result = context_->get(vector_[index], keep_local);
499  }
500 
501  return result.to_string();
502 }
503 
506 {
508 
509  if (index < vector_.size() && context_)
510  {
511  ContextGuard context_guard(*context_);
512  MADARA_GUARD_TYPE guard(mutex_);
513  result = context_->get(vector_[index], settings_);
514  }
515 
516  return result;
517 }
518 
520 {
521  bool result(false);
522 
523  if (index < vector_.size() && context_)
524  {
525  ContextGuard context_guard(*context_);
526  MADARA_GUARD_TYPE guard(mutex_);
527  result = context_->exists(vector_[index]);
528  }
529 
530  return result;
531 }
532 
534  size_t index, const type& value)
535 {
536  int result = -1;
537 
538  if (index < vector_.size() && context_)
539  {
540  ContextGuard context_guard(*context_);
541  MADARA_GUARD_TYPE guard(mutex_);
542  result = context_->set(vector_[index], value, settings_);
543  }
544 
545  return result;
546 }
547 
549  size_t index, const type& value, const KnowledgeUpdateSettings& settings)
550 {
551  int result = -1;
552 
553  if (index < vector_.size() && context_)
554  {
555  ContextGuard context_guard(*context_);
556  MADARA_GUARD_TYPE guard(mutex_);
557  result = context_->set(vector_[index], value, settings);
558  }
559 
560  return result;
561 }
562 
564  const std::vector<type>& value)
565 {
566  int result = -1;
567 
568  if (context_)
569  {
570  ContextGuard context_guard(*context_);
571  MADARA_GUARD_TYPE guard(mutex_);
572 
573  if (vector_.size() < value.size())
574  resize((int)value.size(), false);
575 
576  for (size_t i = 0; i < value.size(); ++i)
577  {
578  context_->set(vector_[i], value[i], settings_);
579  }
580 
581  result = 0;
582  }
583 
584  return result;
585 }
586 
588  const std::vector<type>& value, const KnowledgeUpdateSettings& settings)
589 {
590  int result = -1;
591 
592  if (context_)
593  {
594  ContextGuard context_guard(*context_);
595  MADARA_GUARD_TYPE guard(mutex_);
596  if (vector_.size() < value.size())
597  resize((int)value.size(), false);
598 
599  for (size_t i = 0; i < value.size(); ++i)
600  {
601  context_->set(vector_[i], value[i], settings);
602  }
603 
604  result = 0;
605  }
606 
607  return result;
608 }
609 
611  size_t index, uint32_t quality, const KnowledgeReferenceSettings& settings)
612 {
613  if (index < vector_.size() && context_)
614  {
615  ContextGuard context_guard(*context_);
616  MADARA_GUARD_TYPE guard(mutex_);
617  context_->set_quality(vector_[index].get_name(), quality, true, settings);
618  }
619 }
620 
622 {
623  bool result(false);
624 
626  "StringVector::is_true: Checking for truth\n");
627 
628  if (context_)
629  {
630  ContextGuard context_guard(*context_);
631  MADARA_GUARD_TYPE guard(mutex_);
632 
633  result = true;
634 
636  "StringVector::is_true: context was not null. Result changed to %d\n",
637  (int)result);
638 
639  for (size_t index = 0; index < vector_.size(); ++index)
640  {
642  "StringVector::is_true: checking index %d, is_false of %d. \n",
643  (int)result, (int)context_->get(vector_[index]).is_false());
644 
645  if (context_->get(vector_[index]).is_false())
646  {
648  "StringVector::is_true: result is false, breaking\n");
649 
650  result = false;
651  break;
652  }
653  }
654 
655  if (vector_.size() == 0)
656  result = false;
657  }
658 
660  "StringVector::is_true: final result is %d\n", (int)result);
661 
662  return result;
663 }
664 
666 {
667  return !is_true();
668 }
669 
671 {
672  return is_true();
673 }
674 
676 {
677  return is_false();
678 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
const ThreadSafeContext * context_
madara::knowledge::KnowledgeRecord KnowledgeRecord
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
Settings for applying knowledge updates.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the current value of a variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:34
KnowledgeUpdateSettings settings_
Settings for modifications.
std::string name_
Prefix of variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:32
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void modify(void)
Mark the vector as modified.
void operator=(const StringVector &rhs)
Assignment operator.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
std::string type
trait that describes the value type
Definition: StringVector.h:35
VariableReference get_size_ref(void)
Returns a reference to the size field of the current name.
std::vector< VariableReference > vector_
Values of the array.
Definition: StringVector.h:360
StringVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: StringVector.cpp:4
bool is_false(void) const
Determines if the value of the vector is false.
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 exchange(StringVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
bool is_true(void) const
Determines if all values in the vector are true.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: StringVector.h:355
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
virtual 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.
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 ...
void transfer_to(StringVector &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.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
VariableReference size_
Reference to the size field of the vector space.
Definition: StringVector.h:365
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)
Sets a knowledge variable to a specified value.
size_t size(void) const
Returns the size of the local vector.
virtual BaseContainer * clone(void) const
Clones this container.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
::std::vector< KnowledgeRecord > KnowledgeVector
::std::vector< std::string > StringVector