MADARA  3.4.1
NativeDoubleVector.cpp
Go to the documentation of this file.
1 #include "NativeDoubleVector.h"
4 
6  const KnowledgeUpdateSettings& settings)
7  : BaseContainer("", settings), context_(0)
8 {
10  "NativeDoubleVector::constructor: new object\n");
11 }
12 
14  const std::string& name, KnowledgeBase& knowledge, int size,
15  const KnowledgeUpdateSettings& settings)
16  : BaseContainer(name, settings), context_(&(knowledge.get_context()))
17 {
19  "NativeDoubleVector::constructor called for %s[%d]\n", name.c_str(),
20  size);
21 
22  vector_ = knowledge.get_ref(name, settings_);
23  if (size >= 0)
24  {
25  resize(size);
26  }
27 }
28 
30  const std::string& name, Variables& knowledge, int size,
31  const KnowledgeUpdateSettings& settings)
32  : context_(knowledge.get_context())
33 {
35  "NativeDoubleVector::constructor called for %s[%d]\n", name.c_str(),
36  size);
37 
38  vector_ = knowledge.get_ref(name, settings);
39  if (size >= 0)
40  {
41  resize(size);
42  }
43 }
44 
46  const NativeDoubleVector& rhs)
47  : BaseContainer(rhs), context_(rhs.context_), vector_(rhs.vector_)
48 {
50  "NativeDoubleVector::copy constructor called on %s\n", rhs.name_.c_str());
51 }
52 
54 {
56  "NativeDoubleVector::destructor called on %s\n", this->name_.c_str());
57 }
58 
60 {
62  "NativeDoubleVector::modify called\n");
63 
64  ContextGuard context_guard(*context_);
65  if (context_ && name_ != "")
66  {
68  "NativeDoubleVector::modify: context is valid. Marking %s.\n",
69  this->name_.c_str());
70 
71  context_->mark_modified(vector_);
72  }
73 }
74 
76  void)
77 {
78  std::stringstream result;
79 
80  result << "Native Double Vector: ";
81 
82  if (context_)
83  {
84  ContextGuard context_guard(*context_);
85  MADARA_GUARD_TYPE guard(mutex_);
86 
87  result << this->name_;
88  result << " [" << size() << "]";
89  result << " = " << context_->get(vector_).to_string();
90  }
91 
92  return result.str();
93 }
94 
96 {
97  modify();
98 }
99 
101  void)
102 {
103  return get_debug_info();
104 }
105 
108 {
110  "NativeDoubleVector::clone: cloning %s\n", this->name_.c_str());
111 
112  return new NativeDoubleVector(*this);
113 }
114 
116  const NativeDoubleVector& rhs)
117 {
118  if (this != &rhs)
119  {
120  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
121 
123  "NativeDoubleVector::assignment: %s: copying from %s.\n",
124  this->name_.c_str(), rhs.name_.c_str());
125 
126  this->context_ = rhs.context_;
127  this->name_ = rhs.name_;
128  this->settings_ = rhs.settings_;
129  this->vector_ = rhs.vector_;
130  }
131 }
132 
134 {
135  if (context_ && name_ != "")
136  {
138  "NativeDoubleVector::push_back: %s: valid context, pushing.\n",
139  this->name_.c_str());
140 
141  ContextGuard context_guard(*context_);
142  MADARA_GUARD_TYPE guard(mutex_);
143 
144  size_t i = size();
145  resize((int)i + 1);
146  set(i, value);
147  }
148 }
149 
151 {
152  if (context_ && name_ != "")
153  {
154  ContextGuard context_guard(*context_);
155  MADARA_GUARD_TYPE guard(mutex_);
156 
158  "NativeDoubleVector::resize: %s: resizing to %d\n", this->name_.c_str(),
159  (int)size);
160 
161  knowledge::KnowledgeRecord value = context_->get(vector_, settings_);
162 
163  value.resize(size);
164 
165  context_->set(vector_, value, settings_);
166  }
167 }
168 
170 {
171  ContextGuard context_guard(*context_);
172  MADARA_GUARD_TYPE guard(mutex_);
173 
174  return context_->get(vector_, settings_).size();
175 }
176 
178  const std::string& var_name, KnowledgeBase& knowledge, int size)
179 {
180  if (context_ != &(knowledge.get_context()) || name_ != var_name)
181  {
182  context_ = &(knowledge.get_context());
183 
184  ContextGuard context_guard(*context_);
185  MADARA_GUARD_TYPE guard(mutex_);
186 
188  "NativeDoubleVector::set_name: setting name to %s\n", var_name.c_str());
189 
190  name_ = var_name;
191 
192  vector_ = knowledge.get_ref(var_name, settings_);
193 
194  if (size > 0)
195  resize(size_t(size));
196  }
197 }
198 
200  const std::string& var_name, Variables& knowledge, int size)
201 {
202  if (context_ != knowledge.get_context() || name_ != var_name)
203  {
204  context_ = knowledge.get_context();
205 
206  ContextGuard context_guard(*context_);
207  MADARA_GUARD_TYPE guard(mutex_);
208 
210  "NativeDoubleVector::set_name: setting name to %s\n", var_name.c_str());
211 
212  name_ = var_name;
213 
214  vector_ = knowledge.get_ref(var_name, settings_);
215 
216  if (size > 0)
217  resize(size_t(size));
218  }
219 }
220 
222  const std::string& var_name, ThreadSafeContext& knowledge, int size)
223 {
224  if (context_ != &knowledge || name_ != var_name)
225  {
226  context_ = &knowledge;
227 
228  ContextGuard context_guard(*context_);
229  MADARA_GUARD_TYPE guard(mutex_);
230 
232  "NativeDoubleVector::set_name: setting name to %s\n", var_name.c_str());
233 
234  name_ = var_name;
235 
236  vector_ = knowledge.get_ref(var_name, settings_);
237 
238  if (size > 0)
239  resize(size_t(size));
240  }
241 }
242 
244  NativeDoubleVector& other)
245 {
246  if (context_ && other.context_)
247  {
248  std::lock(*context_, *other.context_, mutex_, other.mutex_);
249 
250  ContextGuard context_guard(*context_, std::adopt_lock);
251  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
252  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
253  guard2(other.mutex_, std::adopt_lock);
254 
256  "NativeDoubleVector::exchange: %s exchanging with %s\n",
257  this->name_.c_str(), other.name_.c_str());
258 
259  knowledge::KnowledgeRecord temp(context_->get(other.vector_));
260 
261  context_->set(other.vector_, context_->get(vector_), other.settings_);
262  context_->set(vector_, temp, settings_);
263  }
264 }
265 
267  NativeDoubleVector& other)
268 {
269  if (context_ && other.context_)
270  {
271  std::lock(*context_, *other.context_, mutex_, other.mutex_);
272 
273  ContextGuard context_guard(*context_, std::adopt_lock);
274  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
275  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
276  guard2(other.mutex_, std::adopt_lock);
277 
279  "NativeDoubleVector::transfer_to: %s transfering to %s\n",
280  this->name_.c_str(), other.name_.c_str());
281 
282  size_t other_size = other.size();
283  size_t this_size = this->size();
284 
285  if (this_size > 0)
286  {
288  "NativeDoubleVector::transfer_to: %s has elements\n",
289  this->name_.c_str());
290 
291  size_t size = other_size + this_size;
292  other.resize((int)size);
293 
294  knowledge::KnowledgeRecord rhs(other.context_->get(other.vector_));
295  knowledge::KnowledgeRecord lhs(context_->get(vector_));
296 
297  rhs.set_index(size - 1, lhs.retrieve_index(this_size - 1).to_double());
298 
299  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
300  {
301  rhs.set_index(other_size, lhs.retrieve_index(i).to_double());
302  }
303 
304  other.context_->set(other.vector_, rhs, other.settings_);
305 
306  this->resize(0);
307  }
308  }
309 }
310 
312  KnowledgeVector& target) const
313 {
314  if (context_)
315  {
316  ContextGuard context_guard(*context_);
317  MADARA_GUARD_TYPE guard(mutex_);
318 
320  "NativeDoubleVector::copy_to: %s: copying elements to vector\n",
321  this->name_.c_str());
322 
323  target.resize(size());
324 
325  for (size_t i = 0; i < target.size(); ++i)
326  {
327  target[i] = knowledge::KnowledgeRecord((*this)[i]);
328  }
329  }
330 }
331 
334  size_t index) const
335 {
337 
338  if (context_)
339  {
340  ContextGuard context_guard(*context_);
341  MADARA_GUARD_TYPE guard(mutex_);
342 
344  "NativeDoubleVector[]: %s: retrieving element from container\n",
345  this->name_.c_str());
346 
347  result = context_->get(vector_, settings_);
348 
349  if (index < result.size())
350  result = result.retrieve_index(index);
351  }
352 
353  return result.to_double();
354 }
355 
357  size_t index, type value)
358 {
359  int result = -1;
360 
361  if (context_)
362  {
363  ContextGuard context_guard(*context_);
364  MADARA_GUARD_TYPE guard(mutex_);
365 
367  "NativeDoubleVector::set: %s: setting element [%d] to %f\n",
368  this->name_.c_str(), (int)index, value);
369 
370  result = context_->set_index(vector_, index, value, settings_);
371  }
372 
373  return result;
374 }
375 
377  size_t index, type value, const KnowledgeUpdateSettings& settings)
378 {
379  int result = -1;
380 
381  if (context_)
382  {
383  ContextGuard context_guard(*context_);
384  MADARA_GUARD_TYPE guard(mutex_);
385 
387  "NativeDoubleVector::set: %s: setting element [%d] "
388  "to %f with custom settings\n",
389  this->name_.c_str(), (int)index, value);
390 
391  result = context_->set_index(vector_, index, value, settings);
392  }
393 
394  return result;
395 }
396 
398  const std::vector<type>& value)
399 {
400  int result = -1;
401 
402  if (context_)
403  {
404  ContextGuard context_guard(*context_);
405  MADARA_GUARD_TYPE guard(mutex_);
406 
408  "NativeDoubleVector::set: %s: setting all elements\n",
409  this->name_.c_str());
410 
411  context_->set(vector_, value, settings_);
412  }
413 
414  return result;
415 }
416 
418  const std::vector<type>& value, const KnowledgeUpdateSettings& settings)
419 {
420  int result = -1;
421 
422  if (context_)
423  {
424  ContextGuard context_guard(*context_);
425  MADARA_GUARD_TYPE guard(mutex_);
426 
428  "NativeDoubleVector::set: %s: setting all elements "
429  "with custom settings\n",
430  this->name_.c_str());
431 
432  context_->set(vector_, value, settings);
433  }
434 
435  return result;
436 }
437 
439  const double* values, uint32_t size)
440 {
441  int result = -1;
442 
443  if (context_)
444  {
445  ContextGuard context_guard(*context_);
446  MADARA_GUARD_TYPE guard(mutex_);
447 
449  "NativeDoubleVector::set: %s: setting all elements\n",
450  this->name_.c_str());
451 
452  context_->set(vector_, values, size, settings_);
453  }
454 
455  return result;
456 }
457 
459  uint32_t size, const KnowledgeUpdateSettings& settings)
460 {
461  int result = -1;
462 
463  if (context_)
464  {
465  ContextGuard context_guard(*context_);
466  MADARA_GUARD_TYPE guard(mutex_);
467 
469  "NativeDoubleVector::set: %s: setting all elements "
470  "with custom settings\n",
471  this->name_.c_str());
472 
473  context_->set(vector_, values, size, settings);
474  }
475 
476  return result;
477 }
478 
480  size_t /*index*/, uint32_t quality,
481  const KnowledgeReferenceSettings& settings)
482 {
483  if (context_)
484  {
485  ContextGuard context_guard(*context_);
486  MADARA_GUARD_TYPE guard(mutex_);
487 
489  "NativeDoubleVector::set: %s: setting quality of knowledge\n",
490  this->name_.c_str());
491 
492  context_->set_quality(name_, quality, true, settings);
493  }
494 }
495 
497 {
498  bool result(false);
499 
500  if (context_)
501  {
502  ContextGuard context_guard(*context_);
503  MADARA_GUARD_TYPE guard(mutex_);
504  result = context_->exists(vector_);
505  }
506 
507  return result;
508 }
509 
510 std::vector<double>
512 {
514  "NativeDoubleVector::to)doubles: %s: retrieving double vector\n",
515  this->name_.c_str());
516 
517  return context_->get(this->vector_, settings_).to_doubles();
518 }
519 
522 {
524  "NativeDoubleVector::to_record: %s: retrieving record\n",
525  this->name_.c_str());
526 
528  context_->get(this->vector_, settings_);
529 
530  result = result.retrieve_index(index);
531  return result;
532 }
533 
536 {
538  "NativeDoubleVector::to_record: %s: retrieving record\n",
539  this->name_.c_str());
540 
541  return context_->get(this->vector_, settings_);
542 }
543 
545 {
546  bool result(false);
547 
549  "NativeDoubleVector::is_true: %s: Checking for non-zero value\n",
550  this->name_.c_str());
551 
552  if (context_)
553  {
554  ContextGuard context_guard(*context_);
555  MADARA_GUARD_TYPE guard(mutex_);
556  result = context_->get(vector_, settings_).is_true();
557  }
558 
560  "NativeDoubleVector::is_true: %s: final result is %d\n",
561  this->name_.c_str(), (int)result);
562 
563  return result;
564 }
565 
567 {
568  return !is_true();
569 }
570 
572 {
573  return is_true();
574 }
575 
577 {
578  return is_false();
579 }
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
#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.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
double to_double(void) const
converts the value to a float/double.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
uint32_t size(void) const
returns the size of the value
void resize(size_t new_size)
resizes an array to a new size
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.
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 doubles inside of KaRL.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void modify(void)
Mark the value as modified.
size_t size(void) const
Returns the size of the vector.
virtual BaseContainer * clone(void) const
Clones this container.
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.
NativeDoubleVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
void operator=(const NativeDoubleVector &rhs)
Assignment operator.
double type
trait that describes the value type
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
void transfer_to(NativeDoubleVector &other)
Transfers elements from this vector to another.
std::vector< double > to_doubles(void) const
Retrieves the native double array from inside the record.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
virtual 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.
bool is_true(void) const
Determines if all values in the vector are true.
ThreadSafeContext * context_
Variable context that we are modifying.
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 exists(void) const
Checks to see if the variable has ever been assigned a value.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
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 copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void exchange(NativeDoubleVector &other)
Exchanges the vector at this location with the vector at another location.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
bool is_false(void) const
Determines if the value of the vector is false.
VariableReference vector_
Reference to the size field of the vector space.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
::std::vector< KnowledgeRecord > KnowledgeVector
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger