MADARA  3.1.8
NativeDoubleVectorStaged.inl
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NATIVE_DOUBLE_VECTOR_STAGED_INL_
3 #define _MADARA_NATIVE_DOUBLE_VECTOR_STAGED_INL_
4 
5 
9 
10 inline void
12 {
15  "NativeDoubleVectorStaged::modify called\n");
16 
17  ContextGuard context_guard (*context_);
18  if (context_ && name_ != "")
19  {
22  "NativeDoubleVectorStaged::modify: context is valid. Marking %s.\n",
23  this->name_.c_str ());
24 
26  }
27 }
28 
29 inline std::string
31  void)
32 {
33  std::stringstream result;
34 
35  result << "Native Double Vector Staged: ";
36 
37  if (context_)
38  {
39  ContextGuard context_guard (*context_);
40  MADARA_GUARD_TYPE guard (mutex_);
41 
42  result << this->name_;
43  result << " [" << size () << "]";
44  result << " = " << context_->get (vector_).to_string ();
45  }
46 
47  return result.str ();
48 }
49 
50 inline std::string
52  void)
53 {
54  return get_debug_info ();
55 }
56 
57 inline void
59  const NativeDoubleVectorStaged & rhs)
60 {
61  if (this != &rhs)
62  {
63  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
64 
67  "NativeDoubleVectorStaged::assignment: %s: copying from %s.\n",
68  this->name_.c_str (),
69  rhs.name_.c_str ());
70 
71  this->context_ = rhs.context_;
72  this->name_ = rhs.name_;
73  this->settings_ = rhs.settings_;
74  this->vector_ = rhs.vector_;
75  this->value_ = rhs.value_;
76  this->has_changed_ = true;
77  }
78 }
79 
80 inline void
82  const std::vector <type> & rhs)
83 {
84  if (!has_changed_)
85  has_changed_ = true;
86 
88 }
89 
90 inline void
92  type value)
93 {
94  if (!has_changed_)
95  has_changed_ = true;
96 
97  size_t i = size ();
98  resize ((int)i + 1);
99  set (i, value);
100 }
101 
102 inline void
104  size_t size)
105 {
106  if (!has_changed_)
107  has_changed_ = true;
108 
109  value_.resize (size);
110 }
111 
112 inline size_t
114 {
115  return value_.size ();
116 }
117 
118 inline void
120  const std::string & var_name,
122  bool sync)
123 {
124  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
125  {
126  context_ = &(knowledge.get_context ());
127 
128  ContextGuard context_guard (*context_);
129  MADARA_GUARD_TYPE guard (mutex_);
130 
133  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
134  var_name.c_str ());
135 
136  name_ = var_name;
137 
138  vector_ = knowledge.get_ref (var_name, settings_);
139 
140  if (size > 0)
141  {
142  resize (size_t (size));
143  }
144  else if (sync)
145  {
146  std::vector <type> values = knowledge.get (vector_).to_doubles ();
147  value_.set_value (values);
148  }
149  }
150 }
151 
152 inline void
154  const std::string & var_name,
155  Variables & knowledge, int size,
156  bool sync)
157 {
158  if (context_ != knowledge.get_context () || name_ != var_name)
159  {
160  context_ = knowledge.get_context ();
161 
162  ContextGuard context_guard (*context_);
163  MADARA_GUARD_TYPE guard (mutex_);
164 
167  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
168  var_name.c_str ());
169 
170  name_ = var_name;
171 
172  vector_ = knowledge.get_ref (var_name, settings_);
173 
174  if (size > 0)
175  {
176  resize (size_t (size));
177  }
178  else if (sync)
179  {
180  std::vector <type> values = knowledge.get (vector_).to_doubles ();
181  value_.set_value (values);
182  }
183  }
184 }
185 
186 inline void
188  const std::string & var_name,
190  bool sync)
191 {
192  if (context_ != &knowledge || name_ != var_name)
193  {
194  context_ = &knowledge;
195 
196  ContextGuard context_guard (*context_);
197  MADARA_GUARD_TYPE guard (mutex_);
198 
201  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
202  var_name.c_str ());
203 
204  name_ = var_name;
205 
206  vector_ = knowledge.get_ref (var_name, settings_);
207 
208  if (size > 0)
209  {
210  resize (size_t (size));
211  }
212  else if (sync)
213  {
214  std::vector <type> values = knowledge.get (vector_).to_doubles ();
215  value_.set_value (values);
216  }
217  }
218 }
219 
220 inline void
222  NativeDoubleVectorStaged & other)
223 {
224  knowledge::KnowledgeRecord temp (other.value_);
225 
226  other.value_ = value_;
227  value_ = temp;
228 
229  if (!has_changed_)
230  has_changed_ = true;
231 
232  if (!other.has_changed_)
233  other.has_changed_ = true;
234 }
235 
236 inline void
238  NativeDoubleVectorStaged & other)
239 {
240  size_t other_size = other.size ();
241  size_t this_size = this->size ();
242 
243  if (this_size > 0)
244  {
245  size_t size = other_size + this_size;
246  other.resize ((int)size);
247 
248  knowledge::KnowledgeRecord & rhs (other.value_);
250 
251  rhs.set_index (size - 1, lhs.retrieve_index (this_size - 1).to_double ());
252 
253  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
254  {
255  rhs.set_index (j, lhs.retrieve_index (i).to_double ());
256  }
257 
258  lhs.reset_value ();
259 
260  if (!has_changed_)
261  has_changed_ = true;
262 
263  if (!other.has_changed_)
264  other.has_changed_ = true;
265 
266  }
267 }
268 
269 inline void
271  KnowledgeVector & target) const
272 {
273  target.resize (size ());
274 
275  for (size_t i = 0; i < target.size (); ++i)
276  {
277  target[i] = value_.retrieve_index(i);
278  }
279 }
280 
283  size_t index) const
284 {
286 
287  result = result.retrieve_index (index);
288 
289  return result.to_double ();
290 }
291 
292 inline void
294  size_t index,
295  type value)
296 {
297  if (!has_changed_)
298  has_changed_ = true;
299 
300  value_.set_index (index, value);
301 }
302 
303 inline void
305  const std::vector <type> & value)
306 {
307  if (!has_changed_)
308  has_changed_ = true;
309 
311 }
312 
313 inline void
315  size_t /*index*/,
316  uint32_t quality,
317  const KnowledgeReferenceSettings & settings)
318 {
319  if (context_)
320  {
321  ContextGuard context_guard (*context_);
322  MADARA_GUARD_TYPE guard (mutex_);
323 
326  "NativeDoubleVectorStaged::set: %s: setting quality of knowledge\n",
327  this->name_.c_str ());
328 
329  context_->set_quality (name_, quality, true, settings);
330  }
331 }
332 
335  size_t index) const
336 {
338  result = value_.retrieve_index (index);
339  return result;
340 }
341 
344  void) const
345 {
346  return value_;
347 }
348 
349 inline bool
351 {
352  bool result (false);
353 
355  "NativeIntegerVectorStaged::is_true: checking for non-zero value\n");
356 
357  result = value_.is_true ();
358 
360  "NativeIntegerVectorStaged::is_true: final result is %d\n",
361  (int)result);
362 
363  return result;
364 }
365 
366 inline bool
368 {
369  return !is_true ();
370 }
371 
372 inline void
374 {
375  has_changed_ = false;
376  value_ = context_->get (vector_);
377 }
378 
379 inline void
381 {
382  if (has_changed_)
384 }
385 
386 #endif // _MADARA_NATIVE_DOUBLE_VECTOR_STAGED_INL_
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.
bool is_true(void) const
Checks to see if the record is true.
void reset_value(void) noexcept
resets the variable to an integer
bool has_changed_
Tracks if value_ has changed since last read.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1, bool sync=true)
Sets the variable name that this refers to.
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.
madara::knowledge::KnowledgeRecord KnowledgeRecord
double to_double(void) const
converts the value to a float/double
This class stores a vector of doubles inside of KaRL.
std::string name_
Prefix of variable.
bool is_true(void) const
Determines if all values in the vector are true.
VariableReference vector_
Reference to the size field of the vector space.
This class stores variables and their values for use by any entity needing state information in a thr...
void write(void)
Writes the value to the knowledge base.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
void set(size_t index, type value)
Sets a knowledge variable to a specified value.
MADARA_LOCK_TYPE mutex_
guard for access and changes
void read(void)
Reads the value from the knowledge base.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
::std::vector< KnowledgeRecord > KnowledgeVector
void exchange(NativeDoubleVectorStaged &other)
Exchanges the vector at this location with the vector at another location.
KnowledgeRecord value_
The staged value of the vector.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
size_t size(void) const
Returns the size of the vector.
ThreadSafeContext * context_
Variable context that we are modifying.
void transfer_to(NativeDoubleVectorStaged &other)
Transfers elements from this vector to another.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
void operator=(const NativeDoubleVectorStaged &rhs)
Assignment operator.
void set_index(size_t index, T value)
sets the value at the index to the specified value.
static constexpr struct madara::knowledge::tags::string_t string
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.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
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.
bool is_false(void) const
Determines if the value of the vector is false.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
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.
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...
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.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.