MADARA  3.4.1
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 
8 
10  void)
11 {
13  "NativeDoubleVectorStaged::modify called\n");
14 
15  ContextGuard context_guard(*context_);
16  if (context_ && name_ != "")
17  {
19  "NativeDoubleVectorStaged::modify: context is valid. Marking %s.\n",
20  this->name_.c_str());
21 
23  }
24 }
25 
26 inline std::string
28 {
29  std::stringstream result;
30 
31  result << "Native Double Vector Staged: ";
32 
33  if (context_)
34  {
35  ContextGuard context_guard(*context_);
36  MADARA_GUARD_TYPE guard(mutex_);
37 
38  result << this->name_;
39  result << " [" << size() << "]";
40  result << " = " << context_->get(vector_).to_string();
41  }
42 
43  return result.str();
44 }
45 
46 inline std::string
48 {
49  return get_debug_info();
50 }
51 
53  const NativeDoubleVectorStaged& rhs)
54 {
55  if (this != &rhs)
56  {
57  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
58 
60  "NativeDoubleVectorStaged::assignment: %s: copying from %s.\n",
61  this->name_.c_str(), rhs.name_.c_str());
62 
63  this->context_ = rhs.context_;
64  this->name_ = rhs.name_;
65  this->settings_ = rhs.settings_;
66  this->vector_ = rhs.vector_;
67  this->value_ = rhs.value_;
68  this->has_changed_ = true;
69  }
70 }
71 
73  const std::vector<type>& rhs)
74 {
75  if (!has_changed_)
76  has_changed_ = true;
77 
78  value_ = knowledge::KnowledgeRecord(rhs);
79 }
80 
82  type value)
83 {
84  if (!has_changed_)
85  has_changed_ = true;
86 
87  size_t i = size();
88  resize((int)i + 1);
89  set(i, value);
90 }
91 
93  size_t size)
94 {
95  if (!has_changed_)
96  has_changed_ = true;
97 
98  value_.resize(size);
99 }
100 
102  void) const
103 {
104  return value_.size();
105 }
106 
108  const std::string& var_name, KnowledgeBase& knowledge, int size, bool sync)
109 {
110  if (context_ != &(knowledge.get_context()) || name_ != var_name)
111  {
112  context_ = &(knowledge.get_context());
113 
114  ContextGuard context_guard(*context_);
115  MADARA_GUARD_TYPE guard(mutex_);
116 
118  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
119  var_name.c_str());
120 
121  name_ = var_name;
122 
123  vector_ = knowledge.get_ref(var_name, settings_);
124 
125  if (size > 0)
126  {
127  resize(size_t(size));
128  }
129  else if (sync)
130  {
131  std::vector<type> values = knowledge.get(vector_).to_doubles();
132  value_.set_value(values);
133  }
134  }
135 }
136 
138  const std::string& var_name, Variables& knowledge, int size, bool sync)
139 {
140  if (context_ != knowledge.get_context() || name_ != var_name)
141  {
142  context_ = knowledge.get_context();
143 
144  ContextGuard context_guard(*context_);
145  MADARA_GUARD_TYPE guard(mutex_);
146 
148  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
149  var_name.c_str());
150 
151  name_ = var_name;
152 
153  vector_ = knowledge.get_ref(var_name, settings_);
154 
155  if (size > 0)
156  {
157  resize(size_t(size));
158  }
159  else if (sync)
160  {
161  std::vector<type> values = knowledge.get(vector_).to_doubles();
162  value_.set_value(values);
163  }
164  }
165 }
166 
168  const std::string& var_name, ThreadSafeContext& knowledge, int size,
169  bool sync)
170 {
171  if (context_ != &knowledge || name_ != var_name)
172  {
173  context_ = &knowledge;
174 
175  ContextGuard context_guard(*context_);
176  MADARA_GUARD_TYPE guard(mutex_);
177 
179  "NativeDoubleVectorStaged::set_name: setting name to %s\n",
180  var_name.c_str());
181 
182  name_ = var_name;
183 
184  vector_ = knowledge.get_ref(var_name, settings_);
185 
186  if (size > 0)
187  {
188  resize(size_t(size));
189  }
190  else if (sync)
191  {
192  std::vector<type> values = knowledge.get(vector_).to_doubles();
193  value_.set_value(values);
194  }
195  }
196 }
197 
200 {
202 
203  other.value_ = value_;
204  value_ = temp;
205 
206  if (!has_changed_)
207  has_changed_ = true;
208 
209  if (!other.has_changed_)
210  other.has_changed_ = true;
211 }
212 
213 inline void
216 {
217  size_t other_size = other.size();
218  size_t this_size = this->size();
219 
220  if (this_size > 0)
221  {
222  size_t size = other_size + this_size;
223  other.resize((int)size);
224 
226  knowledge::KnowledgeRecord& lhs(value_);
227 
228  rhs.set_index(size - 1, lhs.retrieve_index(this_size - 1).to_double());
229 
230  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
231  {
232  rhs.set_index(j, lhs.retrieve_index(i).to_double());
233  }
234 
235  lhs.reset_value();
236 
237  if (!has_changed_)
238  has_changed_ = true;
239 
240  if (!other.has_changed_)
241  other.has_changed_ = true;
242  }
243 }
244 
246  KnowledgeVector& target) const
247 {
248  target.resize(size());
249 
250  for (size_t i = 0; i < target.size(); ++i)
251  {
252  target[i] = value_.retrieve_index(i);
253  }
254 }
255 
258  size_t index) const
259 {
261 
262  result = result.retrieve_index(index);
263 
264  return result.to_double();
265 }
266 
268  size_t index, type value)
269 {
270  if (!has_changed_)
271  has_changed_ = true;
272 
273  value_.set_index(index, value);
274 }
275 
277  const std::vector<type>& value)
278 {
279  if (!has_changed_)
280  has_changed_ = true;
281 
282  value_ = knowledge::KnowledgeRecord(value);
283 }
284 
285 inline void
287  size_t /*index*/, uint32_t quality,
288  const KnowledgeReferenceSettings& settings)
289 {
290  if (context_)
291  {
292  ContextGuard context_guard(*context_);
293  MADARA_GUARD_TYPE guard(mutex_);
294 
296  "NativeDoubleVectorStaged::set: %s: setting quality of knowledge\n",
297  this->name_.c_str());
298 
299  context_->set_quality(name_, quality, true, settings);
300  }
301 }
302 
305  size_t index) const
306 {
308  result = value_.retrieve_index(index);
309  return result;
310 }
311 
314 {
315  return value_;
316 }
317 
319  void) const
320 {
321  bool result(false);
322 
324  "NativeIntegerVectorStaged::is_true: checking for non-zero value\n");
325 
326  result = value_.is_true();
327 
329  "NativeIntegerVectorStaged::is_true: final result is %d\n", (int)result);
330 
331  return result;
332 }
333 
335  void) const
336 {
337  return !is_true();
338 }
339 
341 {
342  has_changed_ = false;
343  value_ = context_->get(vector_);
344 }
345 
347 {
348  if (has_changed_)
349  context_->set(vector_, value_);
350 }
351 
352 #endif // _MADARA_NATIVE_DOUBLE_VECTOR_STAGED_INL_
#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.
void reset_value(void) noexcept
resets the variable to an integer
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
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...
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
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.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
bool has_changed_
Tracks if value_ has changed since last read.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1, bool sync=true)
Sets the variable name that this refers to.
size_t size(void) const
Returns the size of the vector.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
void set(size_t index, type value)
Sets a knowledge variable to a specified value.
void read(void)
Reads the value from the knowledge base.
void transfer_to(NativeDoubleVectorStaged &other)
Transfers elements from this vector to another.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
void exchange(NativeDoubleVectorStaged &other)
Exchanges the vector at this location with the vector at another location.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void operator=(const NativeDoubleVectorStaged &rhs)
Assignment operator.
type operator[](size_t index) const
Retrieves a copy of the record from the map.
bool is_true(void) const
Determines if all values in the vector are true.
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.
ThreadSafeContext * context_
Variable context that we are modifying.
void write(void)
Writes the value to the knowledge base.
VariableReference vector_
Reference to the size field of the vector space.
bool is_false(void) const
Determines if the value of the vector is false.
KnowledgeRecord value_
The staged value of the vector.
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