MADARA  3.4.1
NativeIntegerVectorStaged.inl
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
3 #define _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
4 
7 
9  void)
10 {
11  ContextGuard context_guard(*context_);
12  if (context_ && name_ != "")
13  {
15  }
16 }
17 
18 inline std::string
20 {
21  std::stringstream result;
22 
23  result << "Native Integer Vector Staged: ";
24 
25  if (context_)
26  {
27  result << this->name_;
28  result << " [" << size() << "]";
29  result << " = " << value_.to_string();
30  }
31 
32  return result.str();
33 }
34 
35 inline std::string
37 {
38  return get_debug_info();
39 }
40 
42  const NativeIntegerVectorStaged& rhs)
43 {
44  if (this != &rhs)
45  {
46  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
47 
48  this->context_ = rhs.context_;
49  this->name_ = rhs.name_;
50  this->settings_ = rhs.settings_;
51  this->vector_ = rhs.vector_;
52  this->value_ = rhs.value_;
53  this->has_changed_ = true;
54  }
55 }
56 
58  const std::vector<type>& rhs)
59 {
60  if (!has_changed_)
61  has_changed_ = true;
62 
63  value_ = knowledge::KnowledgeRecord(rhs);
64 }
65 
67  type value)
68 {
69  if (!has_changed_)
70  has_changed_ = true;
71 
72  size_t i = size();
73  resize((int)i + 1);
74  set(i, value);
75 }
76 
78  size_t size)
79 {
80  if (!has_changed_)
81  has_changed_ = true;
82 
83  value_.resize(size);
84 }
85 
87  void) const
88 {
89  return value_.size();
90 }
91 
93  const std::string& var_name, KnowledgeBase& knowledge, int size, bool sync)
94 {
95  if (context_ != &(knowledge.get_context()) || name_ != var_name)
96  {
97  context_ = &(knowledge.get_context());
98 
99  ContextGuard context_guard(*context_);
100  MADARA_GUARD_TYPE guard(mutex_);
101 
102  name_ = var_name;
103 
104  vector_ = knowledge.get_ref(var_name, settings_);
105 
106  if (size > 0)
107  {
108  resize(size_t(size));
109  }
110  else if (sync)
111  {
112  std::vector<type> values = knowledge.get(vector_).to_integers();
113  value_.set_value(values);
114  }
115  }
116 }
117 
119  const std::string& var_name, Variables& knowledge, int size, bool sync)
120 {
121  if (context_ != knowledge.get_context() || name_ != var_name)
122  {
123  context_ = knowledge.get_context();
124 
125  ContextGuard context_guard(*context_);
126  MADARA_GUARD_TYPE guard(mutex_);
127 
128  name_ = var_name;
129 
130  vector_ = knowledge.get_ref(var_name, settings_);
131 
132  if (size > 0)
133  {
134  resize(size_t(size));
135  }
136  else if (sync)
137  {
138  std::vector<type> values = knowledge.get(vector_).to_integers();
139  value_.set_value(values);
140  }
141  }
142 }
143 
145  const std::string& var_name, ThreadSafeContext& knowledge, int size,
146  bool sync)
147 {
148  if (context_ != &knowledge || name_ != var_name)
149  {
150  context_ = &knowledge;
151 
152  ContextGuard context_guard(*context_);
153  MADARA_GUARD_TYPE guard(mutex_);
154 
155  name_ = var_name;
156 
157  vector_ = knowledge.get_ref(var_name, settings_);
158 
159  if (size > 0)
160  {
161  resize(size_t(size));
162  }
163  else if (sync)
164  {
165  std::vector<type> values = knowledge.get(vector_).to_integers();
166  value_.set_value(values);
167  }
168  }
169 }
170 
173 {
175 
176  other.value_ = value_;
177  value_ = temp;
178 
179  if (!has_changed_)
180  has_changed_ = true;
181 
182  if (!other.has_changed_)
183  other.has_changed_ = true;
184 }
185 
186 inline void
189 {
190  size_t other_size = other.size();
191  size_t this_size = this->size();
192 
193  if (this_size > 0)
194  {
195  size_t size = other_size + this_size;
196  other.resize((int)size);
197 
199  knowledge::KnowledgeRecord& lhs(value_);
200 
201  rhs.set_index(size - 1, lhs.retrieve_index(this_size - 1).to_double());
202 
203  for (size_t i = 0, j = other_size; i < this_size - 1; ++i, ++j)
204  {
205  rhs.set_index(j, lhs.retrieve_index(i).to_double());
206  }
207 
208  lhs.reset_value();
209 
210  if (!has_changed_)
211  has_changed_ = true;
212  }
213 }
214 
216  KnowledgeVector& target) const
217 {
218  target.resize(size());
219 
220  for (size_t i = 0; i < target.size(); ++i)
221  {
222  target[i] = value_.retrieve_index(i);
223  }
224 }
225 
228  size_t index) const
229 {
231 
232  result = result.retrieve_index(index);
233 
234  return result.to_integer();
235 }
236 
238  size_t index, type value)
239 {
240  if (!has_changed_)
241  has_changed_ = true;
242 
243  value_.set_index(index, value);
244 }
245 
247  const std::vector<type>& value)
248 {
249  if (!has_changed_)
250  has_changed_ = true;
251 
252  value_ = knowledge::KnowledgeRecord(value);
253 }
254 
255 inline void
257  size_t /*index*/, uint32_t quality,
258  const KnowledgeReferenceSettings& settings)
259 {
260  if (context_)
261  {
262  ContextGuard context_guard(*context_);
263  MADARA_GUARD_TYPE guard(mutex_);
264  context_->set_quality(name_, quality, true, settings);
265  }
266 }
267 
270  size_t index) const
271 {
273  result = value_.retrieve_index(index);
274  return result;
275 }
276 
279 {
280  return value_;
281 }
282 
284  void) const
285 {
286  bool result(false);
287 
289  "NativeIntegerVectorStaged::is_true: checking for non-zero value\n");
290 
291  result = value_.is_true();
292 
294  "NativeIntegerVectorStaged::is_true: final result is %d\n", (int)result);
295 
296  return result;
297 }
298 
300  void) const
301 {
302  return !is_true();
303 }
304 
306 {
307  has_changed_ = false;
308  value_ = context_->get(vector_);
309 }
310 
312  void)
313 {
314  if (has_changed_)
315  context_->set(vector_, value_);
316 }
317 
318 #endif // _MADARA_NATIVE_INTEGER_VECTOR_STAGED_INL_
#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.
Integer to_integer(void) const
converts the value to an integer.
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.
bool has_changed_
Tracks if value_ has changed since last read.
void operator=(const NativeIntegerVectorStaged &rhs)
Assignment operator.
virtual 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.
void write(void)
Writes the value to the knowledge base.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
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 set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1, bool sync=true)
Sets the variable name that this refers to.
void push_back(type value)
Pushes the value to the end of the array after incrementing the array size.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void read(void)
Reads the value from the knowledge base.
void transfer_to(NativeIntegerVectorStaged &other)
Transfers elements from this vector to another.
void set(size_t index, type value)
Sets a knowledge variable to a specified value.
KnowledgeRecord value_
The staged value of the vector.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
size_t size(void) const
Returns the size of the vector.
ThreadSafeContext * context_
Variable context that we are modifying.
knowledge::KnowledgeRecord to_record(void) const
Retrieves the entire vector as a native double array in a record.
VariableReference vector_
Reference to the size field of the vector space.
void exchange(NativeIntegerVectorStaged &other)
Exchanges the vector at this location with the vector at another location.
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.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
::std::vector< KnowledgeRecord > KnowledgeVector