MADARA  3.4.1
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, KnowledgeBase& knowledge, int size,
12  const KnowledgeUpdateSettings& settings)
13  : BaseContainer(name, settings), context_(&(knowledge.get_context()))
14 {
15  vector_ = knowledge.get_ref(name, KnowledgeUpdateSettings(true));
16  if (size >= 0)
17  {
18  resize(size);
19  }
20 }
21 
23  const std::string& name, Variables& knowledge, int size,
24  const KnowledgeUpdateSettings& settings)
25  : BaseContainer(name, settings), context_(knowledge.get_context())
26 {
27  vector_ = knowledge.get_ref(name, settings);
28  if (size >= 0)
29  {
30  resize(size);
31  }
32 }
33 
35  const NativeIntegerVector& rhs)
36  : BaseContainer(rhs), context_(rhs.context_), vector_(rhs.vector_)
37 {
38 }
39 
41 
43 {
44  ContextGuard context_guard(*context_);
45  if (context_ && name_ != "")
46  {
47  context_->mark_modified(vector_);
48  }
49 }
50 
52  void)
53 {
54  std::stringstream result;
55 
56  result << "Native Integer Vector: ";
57 
58  if (context_)
59  {
60  ContextGuard context_guard(*context_);
61  MADARA_GUARD_TYPE guard(mutex_);
62 
63  result << this->name_;
64  result << " [" << size() << "]";
65  result << " = " << context_->get(vector_).to_string();
66  }
67 
68  return result.str();
69 }
70 
72 {
73  modify();
74 }
75 
77  void)
78 {
79  return get_debug_info();
80 }
81 
84 {
85  return new NativeIntegerVector(*this);
86 }
87 
89  const NativeIntegerVector& rhs)
90 {
91  if (this != &rhs)
92  {
93  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
94 
95  this->context_ = rhs.context_;
96  this->name_ = rhs.name_;
97  this->settings_ = rhs.settings_;
98  this->vector_ = rhs.vector_;
99  }
100 }
101 
103 {
104  if (context_ && name_ != "")
105  {
106  ContextGuard context_guard(*context_);
107  MADARA_GUARD_TYPE guard(mutex_);
108 
109  size_t i = size();
110  resize((int)i + 1);
111  set(i, value);
112  }
113 }
114 
116 {
117  if (context_ && name_ != "")
118  {
119  ContextGuard context_guard(*context_);
120  MADARA_GUARD_TYPE guard(mutex_);
121 
122  knowledge::KnowledgeRecord value = context_->get(vector_, settings_);
123 
124  value.resize(size);
125 
126  context_->set(vector_, value, settings_);
127  }
128 }
129 
131 {
132  ContextGuard context_guard(*context_);
133  MADARA_GUARD_TYPE guard(mutex_);
134 
135  return context_->get(vector_, settings_).size();
136 }
137 
139  const std::string& var_name, KnowledgeBase& knowledge, int size)
140 {
141  if (context_ != &(knowledge.get_context()) || name_ != var_name)
142  {
143  context_ = &(knowledge.get_context());
144 
145  ContextGuard context_guard(*context_);
146  MADARA_GUARD_TYPE guard(mutex_);
147 
148  name_ = var_name;
149 
150  vector_ = knowledge.get_ref(var_name, settings_);
151 
152  if (size > 0)
153  resize(size_t(size));
154  }
155 }
156 
158  const std::string& var_name, Variables& knowledge, int size)
159 {
160  if (context_ != knowledge.get_context() || name_ != var_name)
161  {
162  context_ = knowledge.get_context();
163 
164  ContextGuard context_guard(*context_);
165  MADARA_GUARD_TYPE guard(mutex_);
166 
167  name_ = var_name;
168 
169  vector_ = knowledge.get_ref(var_name, settings_);
170 
171  if (size > 0)
172  resize(size_t(size));
173  }
174 }
175 
177  const std::string& var_name, ThreadSafeContext& knowledge, int size)
178 {
179  if (context_ != &knowledge || name_ != var_name)
180  {
181  context_ = &knowledge;
182 
183  ContextGuard context_guard(*context_);
184  MADARA_GUARD_TYPE guard(mutex_);
185 
186  name_ = var_name;
187 
188  vector_ = knowledge.get_ref(var_name, settings_);
189 
190  if (size > 0)
191  resize(size_t(size));
192  }
193 }
194 
196  NativeIntegerVector& other)
197 {
198  if (context_ && other.context_)
199  {
200  std::lock(*context_, *other.context_, mutex_, other.mutex_);
201 
202  ContextGuard context_guard(*context_, std::adopt_lock);
203  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
204  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
205  guard2(other.mutex_, std::adopt_lock);
206 
207  knowledge::KnowledgeRecord temp(context_->get(other.vector_));
208 
209  context_->set(other.vector_, context_->get(vector_), other.settings_);
210  context_->set(vector_, temp, settings_);
211  }
212 }
213 
215  NativeIntegerVector& other)
216 {
217  if (context_ && other.context_)
218  {
219  std::lock(*context_, *other.context_, mutex_, other.mutex_);
220 
221  ContextGuard context_guard(*context_, std::adopt_lock);
222  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
223  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
224  guard2(other.mutex_, std::adopt_lock);
225 
226  size_t other_size = other.size();
227  size_t this_size = this->size();
228 
229  if (this_size > 0)
230  {
231  size_t size = other_size + this_size;
232  other.resize((int)size);
233 
234  knowledge::KnowledgeRecord rhs(other.context_->get(other.vector_));
235  knowledge::KnowledgeRecord lhs(context_->get(vector_));
236 
237  rhs.set_index(size - 1, lhs.retrieve_index(this_size - 1).to_double());
238 
239  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
240  {
241  rhs.set_index(other_size, lhs.retrieve_index(i).to_double());
242  }
243 
244  other.context_->set(other.vector_, rhs, other.settings_);
245 
246  this->resize(0);
247  }
248  }
249 }
250 
252  KnowledgeVector& target) const
253 {
254  if (context_)
255  {
256  ContextGuard context_guard(*context_);
257  MADARA_GUARD_TYPE guard(mutex_);
258 
259  target.resize(size());
260 
261  for (size_t i = 0; i < target.size(); ++i)
262  {
263  target[i] = knowledge::KnowledgeRecord((*this)[i]);
264  }
265  }
266 }
267 
270  size_t index) const
271 {
273 
274  if (context_)
275  {
276  ContextGuard context_guard(*context_);
277  MADARA_GUARD_TYPE guard(mutex_);
278  result = context_->get(vector_, settings_);
279 
280  if (index < result.size())
281  result = result.retrieve_index(index);
282  }
283 
284  return result.to_integer();
285 }
286 
288  size_t index, type value)
289 {
290  int result = -1;
291 
292  if (context_)
293  {
294  ContextGuard context_guard(*context_);
295  MADARA_GUARD_TYPE guard(mutex_);
296  result = context_->set_index(vector_, index, value, settings_);
297  }
298 
299  return result;
300 }
301 
303  size_t index, type value, const KnowledgeUpdateSettings& settings)
304 {
305  int result = -1;
306 
307  if (context_)
308  {
309  ContextGuard context_guard(*context_);
310  MADARA_GUARD_TYPE guard(mutex_);
311  result = context_->set_index(vector_, index, value, settings);
312  }
313 
314  return result;
315 }
316 
318  const std::vector<type>& value)
319 {
320  int result = -1;
321 
322  if (context_)
323  {
324  ContextGuard context_guard(*context_);
325  MADARA_GUARD_TYPE guard(mutex_);
326  context_->set(vector_, value, settings_);
327  }
328 
329  return result;
330 }
331 
333  const std::vector<type>& value, const KnowledgeUpdateSettings& settings)
334 {
335  int result = -1;
336 
337  if (context_)
338  {
339  ContextGuard context_guard(*context_);
340  MADARA_GUARD_TYPE guard(mutex_);
341  context_->set(vector_, value, settings);
342  }
343 
344  return result;
345 }
346 
348  size_t /*index*/, uint32_t quality,
349  const KnowledgeReferenceSettings& settings)
350 {
351  if (context_)
352  {
353  ContextGuard context_guard(*context_);
354  MADARA_GUARD_TYPE guard(mutex_);
355  context_->set_quality(name_, quality, true, settings);
356  }
357 }
358 
360 {
361  bool result(false);
362 
363  if (context_)
364  {
365  ContextGuard context_guard(*context_);
366  MADARA_GUARD_TYPE guard(mutex_);
367  result = context_->exists(vector_);
368  }
369 
370  return result;
371 }
372 
373 std::vector<madara::knowledge::KnowledgeRecord::Integer>
375 {
376  return context_->get(this->vector_, settings_).to_integers();
377 }
378 
381  size_t index) const
382 {
384  context_->get(this->vector_, settings_);
385  result = result.retrieve_index(index);
386  return result;
387 }
388 
391 {
392  return context_->get(this->vector_, settings_);
393 }
394 
396 {
397  bool result(false);
398 
400  "NativeIntegerVector::is_true: checking for non-zero value\n");
401 
402  if (context_)
403  {
404  ContextGuard context_guard(*context_);
405  MADARA_GUARD_TYPE guard(mutex_);
406  result = context_->get(vector_, settings_).is_true();
407  }
408 
410  "NativeIntegerVector::is_true: final result is %d\n", (int)result);
411 
412  return result;
413 }
414 
416 {
417  return !is_true();
418 }
419 
421 {
422  return is_true();
423 }
424 
426 {
427  return is_false();
428 }
#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
Integer to_integer(void) const
converts the value to an integer.
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.
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.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
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.
void operator=(const NativeIntegerVector &rhs)
Assignment operator.
bool exists(void) const
Checks to see if the variable has ever been assigned a value.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
NativeIntegerVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
void transfer_to(NativeIntegerVector &other)
Transfers elements from this vector to another.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
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.
void modify(void)
Mark the value as modified.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
size_t size(void) const
Returns the size of the vector.
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(NativeIntegerVector &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.
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.
std::vector< KnowledgeRecord::Integer > to_integers(void) const
Retrieves the native integer array from inside the record.
virtual BaseContainer * clone(void) const
Clones this container.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
::std::vector< KnowledgeRecord > KnowledgeVector