MADARA  3.2.3
NativeIntegerVector.cpp
Go to the documentation of this file.
1 #include "NativeIntegerVector.h"
3 
5  const KnowledgeUpdateSettings & settings)
6 : BaseContainer ("", settings), context_ (0)
7 {
8 }
9 
11  const std::string & name,
13  int size,
14  const KnowledgeUpdateSettings & settings)
15 : BaseContainer (name, settings), context_ (&(knowledge.get_context ()))
16 {
17  vector_ = knowledge.get_ref (name, KnowledgeUpdateSettings (true));
18  if (size >= 0)
19  {
20  resize (size);
21  }
22 }
23 
25  const std::string & name,
27  int size,
28  const KnowledgeUpdateSettings & settings)
29  : BaseContainer (name, settings), context_ (knowledge.get_context ())
30 {
31  vector_ = knowledge.get_ref (name, settings);
32  if (size >= 0)
33  {
34  resize (size);
35  }
36 }
37 
39  const NativeIntegerVector & rhs)
40 : BaseContainer (rhs), context_ (rhs.context_),
41  vector_ (rhs.vector_)
42 {
43 
44 }
45 
46 
48 {
49 
50 }
51 
52 void
54 {
55  ContextGuard context_guard (*context_);
56  if (context_ && name_ != "")
57  {
59  }
60 }
61 
64  void)
65 {
66  std::stringstream result;
67 
68  result << "Native Integer Vector: ";
69 
70  if (context_)
71  {
72  ContextGuard context_guard (*context_);
73  MADARA_GUARD_TYPE guard (mutex_);
74 
75  result << this->name_;
76  result << " [" << size () << "]";
77  result << " = " << context_->get (vector_).to_string ();
78  }
79 
80  return result.str ();
81 }
82 
83 void
85 {
86  modify ();
87 }
88 
91  void)
92 {
93  return get_debug_info ();
94 }
95 
98 {
99  return new NativeIntegerVector (*this);
100 }
101 
102 void
104  const NativeIntegerVector & rhs)
105 {
106  if (this != &rhs)
107  {
108  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
109 
110  this->context_ = rhs.context_;
111  this->name_ = rhs.name_;
112  this->settings_ = rhs.settings_;
113  this->vector_ = rhs.vector_;
114  }
115 }
116 
117 void
119  type value)
120 {
121  if (context_ && name_ != "")
122  {
123  ContextGuard context_guard (*context_);
124  MADARA_GUARD_TYPE guard (mutex_);
125 
126  size_t i = size ();
127  resize ((int)i + 1);
128  set (i, value);
129  }
130 }
131 
132 void
134  size_t size)
135 {
136  if (context_ && name_ != "")
137  {
138  ContextGuard context_guard (*context_);
139  MADARA_GUARD_TYPE guard (mutex_);
140 
142 
143  value.resize (size);
144 
145  context_->set (vector_, value, settings_);
146  }
147 }
148 
149 size_t
151 {
152  ContextGuard context_guard (*context_);
153  MADARA_GUARD_TYPE guard (mutex_);
154 
155  return context_->get (vector_, settings_).size ();
156 }
157 
158 void
160  const std::string & var_name,
162 {
163  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
164  {
165  context_ = &(knowledge.get_context ());
166 
167  ContextGuard context_guard (*context_);
168  MADARA_GUARD_TYPE guard (mutex_);
169 
170  name_ = var_name;
171 
172  vector_ = knowledge.get_ref (var_name, settings_);
173 
174  if (size > 0)
175  resize (size_t (size));
176  }
177 }
178 
179 void
181  const std::string & var_name,
182  Variables & knowledge, int size)
183 {
184  if (context_ != knowledge.get_context () || name_ != var_name)
185  {
186  context_ = knowledge.get_context ();
187 
188  ContextGuard context_guard (*context_);
189  MADARA_GUARD_TYPE guard (mutex_);
190 
191  name_ = var_name;
192 
193  vector_ = knowledge.get_ref (var_name, settings_);
194 
195  if (size > 0)
196  resize (size_t (size));
197  }
198 }
199 
200 void
202  const std::string & var_name,
204 {
205  if (context_ != &knowledge || name_ != var_name)
206  {
207  context_ = &knowledge;
208 
209  ContextGuard context_guard (*context_);
210  MADARA_GUARD_TYPE guard (mutex_);
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 
221 void
223  NativeIntegerVector & other)
224 {
225  if (context_ && other.context_)
226  {
227  std::lock(*context_, *other.context_, mutex_, other.mutex_);
228 
229  ContextGuard context_guard (*context_, std::adopt_lock);
230  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
231  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
232  guard2 (other.mutex_, std::adopt_lock);
233 
234 
236 
237  context_->set (other.vector_, context_->get (vector_), other.settings_);
238  context_->set (vector_, temp, settings_);
239  }
240 }
241 
242 void
244  NativeIntegerVector & 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 
255 
256  size_t other_size = other.size ();
257  size_t this_size = this->size ();
258 
259  if (this_size > 0)
260  {
261  size_t size = other_size + this_size;
262  other.resize ((int)size);
263 
264  knowledge::KnowledgeRecord rhs (other.context_->get (other.vector_));
266 
267  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
268 
269  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
270  {
271  rhs.set_index (other_size, lhs.retrieve_index (i).to_double ());
272  }
273 
274  other.context_->set (other.vector_, rhs, other.settings_);
275 
276  this->resize (0);
277  }
278  }
279 }
280 
281 void
283  KnowledgeVector & target) const
284 {
285  if (context_)
286  {
287  ContextGuard context_guard (*context_);
288  MADARA_GUARD_TYPE guard (mutex_);
289 
290  target.resize (size ());
291 
292  for (size_t i = 0; i < target.size (); ++i)
293  {
294  target[i] = knowledge::KnowledgeRecord ((*this)[i]);
295  }
296  }
297 }
298 
301  size_t index) const
302 {
304 
305  if (context_)
306  {
307  ContextGuard context_guard (*context_);
308  MADARA_GUARD_TYPE guard (mutex_);
309  result = context_->get (vector_, settings_);
310 
311  if (index < result.size ())
312  result = result.retrieve_index (index);
313  }
314 
315  return result.to_integer ();
316 }
317 
318 int
320  size_t index,
321  type value)
322 {
323  int result = -1;
324 
325  if (context_)
326  {
327  ContextGuard context_guard (*context_);
328  MADARA_GUARD_TYPE guard (mutex_);
329  result = context_->set_index (vector_, index, value, settings_);
330  }
331 
332  return result;
333 }
334 
335 int
337  size_t index,
338  type value,
339  const KnowledgeUpdateSettings & settings)
340 {
341  int result = -1;
342 
343  if (context_)
344  {
345  ContextGuard context_guard (*context_);
346  MADARA_GUARD_TYPE guard (mutex_);
347  result = context_->set_index (vector_, index, value, settings);
348  }
349 
350  return result;
351 }
352 
353 int
355  const std::vector <type> & value)
356 {
357  int result = -1;
358 
359  if (context_)
360  {
361  ContextGuard context_guard (*context_);
362  MADARA_GUARD_TYPE guard (mutex_);
363  context_->set (vector_, value, settings_);
364  }
365 
366  return result;
367 }
368 
369 int
371  const std::vector <type> & value,
372  const KnowledgeUpdateSettings & settings)
373 {
374  int result = -1;
375 
376  if (context_)
377  {
378  ContextGuard context_guard (*context_);
379  MADARA_GUARD_TYPE guard (mutex_);
380  context_->set (vector_, value, settings);
381  }
382 
383  return result;
384 }
385 
386 void
388  size_t /*index*/,
389  uint32_t quality,
390  const KnowledgeReferenceSettings & settings)
391 {
392  if (context_)
393  {
394  ContextGuard context_guard (*context_);
395  MADARA_GUARD_TYPE guard (mutex_);
396  context_->set_quality (name_, quality, true, settings);
397  }
398 }
399 
402  size_t index) const
403 {
405  result = result.retrieve_index (index);
406  return result;
407 }
408 
411  void) const
412 {
413  return context_->get (this->vector_, settings_);
414 }
415 
416 bool
418 {
419  bool result (false);
420 
422  "NativeIntegerVector::is_true: checking for non-zero value\n");
423 
424  if (context_)
425  {
426  ContextGuard context_guard (*context_);
427  MADARA_GUARD_TYPE guard (mutex_);
428  result = context_->get (vector_, settings_).is_true ();
429  }
430 
432  "NativeIntegerVector::is_true: final result is %d\n",
433  (int)result);
434 
435  return result;
436 }
437 
438 bool
440 {
441  return !is_true ();
442 }
443 
444 
445 bool
447 {
448  return is_true ();
449 }
450 
451 bool
453 {
454  return is_false ();
455 }
This class encapsulates an entry in a KnowledgeBase.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
virtual BaseContainer * clone(void) const
Clones this container.
bool is_true(void) const
Checks to see if the record is true.
This class stores a vector of doubles inside of KaRL.
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
madara::knowledge::KnowledgeRecord KnowledgeRecord
void modify(void)
Mark the value as modified.
std::string name_
Prefix of variable.
ThreadSafeContext * context_
Variable context that we are modifying.
NativeIntegerVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
bool is_false(void) const
Determines if the value of the vector is false.
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 ...
bool is_true(void) const
Determines if all values in the vector are true.
This class stores variables and their values for use by any entity needing state information in a thr...
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
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_LOCK_TYPE mutex_
guard for access and changes
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
::std::vector< KnowledgeRecord > KnowledgeVector
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
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
static struct madara::knowledge::tags::string_t string
void operator=(const NativeIntegerVector &rhs)
Assignment operator.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
VariableReference vector_
Reference to the size field of the vector space.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Integer to_integer(void) const
converts the value to an integer.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void exchange(NativeIntegerVector &other)
Exchanges the vector at this location with the vector at another location.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
Settings for applying knowledge updates.
size_t size(void) const
Returns the size of the vector.
void transfer_to(NativeIntegerVector &other)
Transfers elements from this vector to another.
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.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
void resize(size_t new_size)
resizes an array to a new size
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
uint32_t size(void) const
returns the size of the value
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
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...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.