MADARA  3.4.1
FlexMap.cpp
Go to the documentation of this file.
1 #include "FlexMap.h"
3 
4 #include <algorithm>
5 
7  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
8  : BaseContainer("", settings), context_(0), delimiter_(delimiter)
9 {
10 }
11 
14  const std::string& delimiter)
15  : BaseContainer(name, settings),
16  context_(&(knowledge.get_context())),
17  delimiter_(delimiter)
18 {
19 }
20 
23  const std::string& delimiter)
24  : BaseContainer(name, settings),
25  context_(knowledge.get_context()),
26  delimiter_(delimiter)
27 {
28 }
29 
31  : BaseContainer(rhs),
32  context_(rhs.context_),
33  variable_(rhs.variable_),
34  delimiter_(rhs.delimiter_)
35 {
36 }
37 
39 
45 {
46  ContextGuard context_guard(*context_);
47  bool result(false);
48 
49  KnowledgeUpdateSettings keep_local(true);
50 
52  "Vector::is_true: checking for non-zero value\n");
53 
54  if (variable_.is_valid())
55  {
56  result = context_->get(variable_, keep_local).is_true();
57  }
58 
60  "FlexMap::is_true: final result is %d\n", (int)result);
61 
62  return result;
63 }
64 
70 {
71  ContextGuard context_guard(*context_);
72  bool result(true);
73 
74  KnowledgeUpdateSettings keep_local(true);
75 
76  if (!variable_.is_valid())
77  {
78  result = context_->get(variable_, keep_local).is_true();
79  }
80 
81  return result;
82 }
83 
85 {
86  if (context_ && name_ != "")
87  {
88  ContextGuard context_guard(*context_);
89 
90  // get all children
92  knowledge.facade_for(*context_);
93  Map map(name_, knowledge, settings_, delimiter_);
94 
95  // modify the children
96  map.modify();
97 
98  // modify ourself
99  if (variable_.is_valid())
100  {
101  knowledge.mark_modified(variable_);
102  }
103  }
104 }
105 
107 {
108  std::stringstream result;
109 
110  result << "Flex Map: ";
111 
112  if (context_ && name_ != "")
113  {
114  ContextGuard context_guard(*context_);
115  MADARA_GUARD_TYPE guard(mutex_);
116 
117  // get all children
119  knowledge.facade_for(*context_);
120  Map map(name_, knowledge, settings_, delimiter_);
121 
122  result << map.get_debug_info();
123  }
124 
125  return result.str();
126 }
127 
129 {
130  modify();
131 }
132 
134 {
135  return get_debug_info();
136 }
137 
140 {
141  return new FlexMap(*this);
142 }
143 
145 {
146  if (this != &rhs)
147  {
148  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
149 
150  this->context_ = rhs.context_;
151  this->name_ = rhs.name_;
152  this->settings_ = rhs.settings_;
153  this->variable_ = rhs.variable_;
154  }
155 }
156 
159 {
160  set(value, settings_);
161 }
162 
164 {
165  set(KnowledgeRecord::Integer(value), settings_);
166 }
167 
169 {
170  set(value, settings_);
171 }
172 
174 {
175  set(value, settings_);
176 }
177 
179  const std::vector<KnowledgeRecord::Integer>& value)
180 {
181  set(value, settings_);
182 }
183 
185  const std::vector<double>& value)
186 {
187  set(value, settings_);
188 }
189 
192  const KnowledgeRecord& default_value) const
193 {
194  if (context_)
195  {
196  ContextGuard context_guard(*context_);
197  MADARA_GUARD_TYPE guard(mutex_);
198 
199  if (!variable_.is_valid())
200  {
201  this->update_variable();
202  }
203 
204  if (context_->exists(variable_))
205  {
206  return context_->get(variable_, settings_);
207  }
208  }
209 
210  return default_value;
211 }
212 
215  KnowledgeRecord::Integer default_value) const
216 {
217  if (context_)
218  {
219  ContextGuard context_guard(*context_);
220  MADARA_GUARD_TYPE guard(mutex_);
221 
222  if (!variable_.is_valid())
223  {
224  this->update_variable();
225  }
226 
227  if (context_->exists(variable_))
228  {
229  return context_->get(variable_, settings_).to_integer();
230  }
231  }
232 
233  return default_value;
234 }
235 
237  double default_value) const
238 {
239  if (context_)
240  {
241  ContextGuard context_guard(*context_);
242  MADARA_GUARD_TYPE guard(mutex_);
243 
244  if (!variable_.is_valid())
245  {
246  this->update_variable();
247  }
248 
249  if (context_->exists(variable_))
250  {
251  return context_->get(variable_, settings_).to_double();
252  }
253  }
254 
255  return default_value;
256 }
257 
259  const std::string& default_value) const
260 {
261  if (context_)
262  {
263  ContextGuard context_guard(*context_);
264  MADARA_GUARD_TYPE guard(mutex_);
265 
266  if (!variable_.is_valid())
267  {
268  this->update_variable();
269  }
270 
271  if (context_->exists(variable_))
272  {
273  return context_->get(variable_, settings_).to_string();
274  }
275  }
276 
277  return default_value;
278 }
279 
281  BufferVector& target) const
282 {
283  if (context_)
284  {
285  ContextGuard context_guard(*context_);
286  MADARA_GUARD_TYPE guard(mutex_);
287 
288  // get all children
290  knowledge.facade_for(*context_);
291 
292  target.set_delimiter(delimiter_);
293  target.set_name(name_, knowledge);
294  }
295 }
296 
298  DoubleVector& target) const
299 {
300  if (context_)
301  {
302  ContextGuard context_guard(*context_);
303  MADARA_GUARD_TYPE guard(mutex_);
304 
305  // get all children
307  knowledge.facade_for(*context_);
308 
309  target.set_delimiter(delimiter_);
310  target.set_name(name_, knowledge);
311  }
312 }
313 
315  IntegerVector& target) const
316 {
317  if (context_)
318  {
319  ContextGuard context_guard(*context_);
320  MADARA_GUARD_TYPE guard(mutex_);
321 
322  // get all children
324  knowledge.facade_for(*context_);
325 
326  target.set_delimiter(delimiter_);
327  target.set_name(name_, knowledge);
328  }
329 }
330 
332  NativeIntegerVector& target) const
333 {
334  if (context_)
335  {
336  ContextGuard context_guard(*context_);
337  MADARA_GUARD_TYPE guard(mutex_);
338 
339  // get all children
341  knowledge.facade_for(*context_);
342 
343  target.set_name(name_, knowledge);
344  }
345 }
346 
348  StringVector& target) const
349 {
350  if (context_)
351  {
352  ContextGuard context_guard(*context_);
353  MADARA_GUARD_TYPE guard(mutex_);
354 
355  // get all children
357  knowledge.facade_for(*context_);
358 
359  target.set_delimiter(delimiter_);
360  target.set_name(name_, knowledge);
361  }
362 }
363 
365 {
366  if (context_)
367  {
368  ContextGuard context_guard(*context_);
369  MADARA_GUARD_TYPE guard(mutex_);
370 
371  // get all children
373  knowledge.facade_for(*context_);
374 
375  target.set_name(name_, knowledge);
376  }
377 }
378 
380  NativeDoubleVector& target) const
381 {
382  if (context_)
383  {
384  ContextGuard context_guard(*context_);
385  MADARA_GUARD_TYPE guard(mutex_);
386 
387  // get all children
389  knowledge.facade_for(*context_);
390 
391  target.set_name(name_, knowledge);
392  }
393 }
394 
396 {
397  if (context_)
398  {
399  ContextGuard context_guard(*context_);
400  MADARA_GUARD_TYPE guard(mutex_);
401 
402  // get all children
404  knowledge.facade_for(*context_);
405 
406  target.set_name(name_, knowledge);
407  }
408 }
409 
411 {
412  if (context_)
413  {
414  ContextGuard context_guard(*context_);
415  MADARA_GUARD_TYPE guard(mutex_);
416 
417  // get all children
419  knowledge.facade_for(*context_);
420 
421  target.set_name(name_, knowledge);
422  }
423 }
424 
426 {
427  if (context_)
428  {
429  ContextGuard context_guard(*context_);
430  MADARA_GUARD_TYPE guard(mutex_);
431 
432  // get all children
434  knowledge.facade_for(*context_);
435 
436  target.set_delimiter(delimiter_);
437  target.set_name(name_, knowledge);
438  }
439 }
440 
442 operator[](const std::string& key)
443 {
444  FlexMap map(settings_, delimiter_);
445 
446  if (key != "" && context_)
447  {
448  ContextGuard context_guard(*context_);
449  MADARA_GUARD_TYPE guard(mutex_);
450 
452  knowledge.facade_for(*context_);
453  map.set_name(name_ + delimiter_ + key, knowledge);
454  }
455 
456  return map;
457 }
458 
460 operator[](size_t index)
461 {
462  FlexMap map(settings_, delimiter_);
463 
464  if (context_)
465  {
466  ContextGuard context_guard(*context_);
467  MADARA_GUARD_TYPE guard(mutex_);
468 
469  std::stringstream buffer;
471  knowledge.facade_for(*context_);
472 
473  buffer << name_ << delimiter_ << index;
474 
475  map.set_name(buffer.str(), knowledge);
476  }
477 
478  return map;
479 }
480 
482  bool first_level_keys_only) const
483 {
484  ContextGuard context_guard(*context_);
485  MADARA_GUARD_TYPE guard(mutex_);
486 
487  size_t result(0);
488 
489  if (!first_level_keys_only)
490  {
491  // get all children
493  knowledge.facade_for(*context_);
494  Map map(name_, knowledge, settings_, delimiter_);
495 
496  result = map.size();
497  }
498  else
499  {
500  // use the context's specialized mapping feature
501  std::vector<std::string> next_keys;
502  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
503  context_->to_map(name_, delimiter_, "", next_keys, all_record_vars, true);
504 
505  result = next_keys.size();
506  }
507 
508  return result;
509 }
510 
512  FlexMap& other, bool refresh_keys, bool delete_keys)
513 {
514  if (context_ && other.context_)
515  {
516  std::lock(*context_, *other.context_, mutex_, other.mutex_);
517 
518  ContextGuard context_guard(*context_, std::adopt_lock);
519  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
520  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
521  guard2(other.mutex_, std::adopt_lock);
522 
523  // use the Map container, which already has exchange implemented
525  knowledge.facade_for(*context_);
526  Map this_map(name_, knowledge, settings_, delimiter_);
527  Map other_map(other.name_, knowledge, other.settings_, delimiter_);
528 
529  this_map.exchange(other_map, refresh_keys, delete_keys);
530  }
531 }
532 
534 {
535  if (context_)
536  {
537  ContextGuard context_guard(*context_);
538  MADARA_GUARD_TYPE guard(mutex_);
539 
540  // use the Map container, which already has clear implemented
542  knowledge.facade_for(*context_);
543  Map map(name_, knowledge, settings_, delimiter_);
544 
545  map.clear();
546 
547  context_->delete_variable(name_, settings_);
548  }
549 }
550 
552  const std::string& key, bool delete_subkeys)
553 {
554  if (context_)
555  {
556  ContextGuard context_guard(*context_);
557  MADARA_GUARD_TYPE guard(mutex_);
558 
559  context_->delete_variable(name_ + delimiter_ + key, settings_);
560 
561  if (delete_subkeys)
562  {
563  // use the Map container, which already has clear implemented
565  knowledge.facade_for(*context_);
566  Map map(name_ + delimiter_ + key, knowledge, settings_, delimiter_);
567 
568  map.clear();
569  }
570  }
571 }
572 
574  const std::string& var_name, KnowledgeBase& knowledge)
575 {
576  if (context_ != &(knowledge.get_context()) || name_ != var_name)
577  {
578  context_ = &(knowledge.get_context());
579 
580  ContextGuard context_guard(*context_);
581  MADARA_GUARD_TYPE guard(mutex_);
582 
583  name_ = var_name;
584 
585  if (context_->exists(var_name, settings_))
586  {
587  KnowledgeUpdateSettings keep_local(true);
588 
589  variable_ = context_->get_ref(var_name, keep_local);
590  }
591  else
592  {
593  // reset variable reference
594  variable_ = VariableReference();
595  }
596  }
597 }
598 
600  const std::string& var_name, Variables& knowledge)
601 {
602  if (context_ != knowledge.get_context() || name_ != var_name)
603  {
604  context_ = knowledge.get_context();
605 
606  ContextGuard context_guard(*context_);
607  MADARA_GUARD_TYPE guard(mutex_);
608 
609  name_ = var_name;
610 
611  if (context_->exists(var_name, settings_))
612  {
613  update_variable();
614  }
615  else
616  {
617  // reset variable reference
618  variable_ = VariableReference();
619  }
620  }
621 }
622 
624 {
625  if (context_ && name_ != "")
626  {
627  ContextGuard context_guard(*context_);
628  MADARA_GUARD_TYPE guard(mutex_);
629 
630  KnowledgeUpdateSettings keep_local(true);
631 
632  variable_ = context_->get_ref(name_, keep_local);
633  }
634 }
635 
637  const std::string& delimiter)
638 {
639  delimiter_ = delimiter;
640 }
641 
643 {
644  return delimiter_;
645 }
646 
652 {
653  ContextGuard context_guard(*context_);
654  bool result(true);
655 
656  KnowledgeUpdateSettings keep_local(true);
657 
658  if (!variable_.is_valid())
659  {
660  result = context_->exists(variable_, keep_local);
661  }
662 
663  return result;
664 }
665 
667  const std::string& key, bool first_level_key) const
668 {
669  bool result(false);
670 
671  if (context_)
672  {
673  ContextGuard context_guard(*context_);
674  MADARA_GUARD_TYPE guard(mutex_);
675 
676  // check if the key exists
677  std::string exact_key = name_ + delimiter_ + key;
678  result = context_->exists(exact_key);
679 
680  if (!result && first_level_key)
681  {
682  std::vector<std::string> curkeys;
683  keys(curkeys);
684 
685  std::vector<std::string>::iterator found;
686 
687  // look for the key in the sub keys
688  found = std::find(curkeys.begin(), curkeys.end(), key);
689 
690  result = found != curkeys.end();
691  }
692  }
693 
694  return result;
695 }
696 
698  std::vector<std::string>& curkeys, bool first_level_keys_only) const
699 {
700  ContextGuard context_guard(*context_);
701  MADARA_GUARD_TYPE guard(mutex_);
702 
703  if (!first_level_keys_only)
704  {
705  // use the Map container, which already has clear implemented
707  knowledge.facade_for(*context_);
708  Map map(name_, knowledge, settings_, delimiter_);
709 
710  map.keys(curkeys);
711  }
712  else
713  {
714  // use the context's specialized mapping feature
715  std::vector<std::string> next_keys;
716  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
717  context_->to_map(name_, delimiter_, "", curkeys, all_record_vars, true);
718  }
719 }
720 
722  const std::string& filename)
723 {
724  int result = -1;
725 
726  if (context_ && name_ != "")
727  {
728  ContextGuard context_guard(*context_);
729  MADARA_GUARD_TYPE guard(mutex_);
730 
731  if (!variable_.is_valid())
732  {
733  update_variable();
734  }
735 
736  result = context_->read_file(variable_, filename, settings_);
737  }
738  return result;
739 }
740 
742 
743  const std::string& filename, const KnowledgeUpdateSettings& settings)
744 {
745  int result = -1;
746 
747  if (context_ && name_ != "")
748  {
749  ContextGuard context_guard(*context_);
750  MADARA_GUARD_TYPE guard(mutex_);
751 
752  if (!variable_.is_valid())
753  {
754  update_variable();
755  }
756 
757  result = context_->read_file(variable_, filename, settings);
758  }
759  return result;
760 }
761 
764 {
765  int result = -1;
766 
767  if (context_ && name_ != "")
768  {
769  ContextGuard context_guard(*context_);
770  MADARA_GUARD_TYPE guard(mutex_);
771 
772  if (!variable_.is_valid())
773  {
774  update_variable();
775  }
776 
777  result = context_->set(variable_, value, settings_);
778  }
779 
780  return result;
781 }
782 
785  const KnowledgeUpdateSettings& settings)
786 {
787  int result = -1;
788 
789  if (context_ && name_ != "")
790  {
791  ContextGuard context_guard(*context_);
792  MADARA_GUARD_TYPE guard(mutex_);
793 
794  if (!variable_.is_valid())
795  {
796  update_variable();
797  }
798 
799  result = context_->set(variable_, value, settings);
800  }
801 
802  return result;
803 }
804 
806 
808 {
809  int result = -1;
810 
811  if (context_ && name_ != "")
812  {
813  ContextGuard context_guard(*context_);
814  MADARA_GUARD_TYPE guard(mutex_);
815 
816  if (!variable_.is_valid())
817  {
818  update_variable();
819  }
820 
821  result = context_->set_index(variable_, index, value, settings_);
822  }
823 
824  return result;
825 }
826 
828 
830  const KnowledgeUpdateSettings& settings)
831 {
832  int result = -1;
833 
834  if (context_ && name_ != "")
835  {
836  ContextGuard context_guard(*context_);
837  MADARA_GUARD_TYPE guard(mutex_);
838 
839  if (!variable_.is_valid())
840  {
841  update_variable();
842  }
843 
844  result = context_->set_index(variable_, index, value, settings);
845  }
846 
847  return result;
848 }
849 
851  const madara::knowledge::KnowledgeRecord::Integer* value, uint32_t size)
852 {
853  int result = -1;
854 
855  if (context_ && name_ != "")
856  {
857  ContextGuard context_guard(*context_);
858  MADARA_GUARD_TYPE guard(mutex_);
859 
860  if (!variable_.is_valid())
861  {
862  update_variable();
863  }
864 
865  result = context_->set(variable_, value, size, settings_);
866  }
867 
868  return result;
869 }
870 
872  const madara::knowledge::KnowledgeRecord::Integer* value, uint32_t size,
873  const KnowledgeUpdateSettings& settings)
874 {
875  int result = -1;
876 
877  if (context_ && name_ != "")
878  {
879  ContextGuard context_guard(*context_);
880  MADARA_GUARD_TYPE guard(mutex_);
881 
882  if (!variable_.is_valid())
883  {
884  update_variable();
885  }
886 
887  result = context_->set(variable_, value, size, settings);
888  }
889 
890  return result;
891 }
892 
894  const std::vector<KnowledgeRecord::Integer>& value)
895 {
896  int result = -1;
897 
898  if (context_ && name_ != "")
899  {
900  ContextGuard context_guard(*context_);
901  MADARA_GUARD_TYPE guard(mutex_);
902 
903  if (!variable_.is_valid())
904  {
905  update_variable();
906  }
907 
908  result = context_->set(variable_, value, settings_);
909  }
910 
911  return result;
912 }
913 
915  const std::vector<KnowledgeRecord::Integer>& value,
916  const KnowledgeUpdateSettings& settings)
917 {
918  int result = -1;
919 
920  if (context_ && name_ != "")
921  {
922  ContextGuard context_guard(*context_);
923  MADARA_GUARD_TYPE guard(mutex_);
924 
925  if (!variable_.is_valid())
926  {
927  update_variable();
928  }
929 
930  result = context_->set(variable_, value, settings);
931  }
932 
933  return result;
934 }
935 
937 {
938  int result = -1;
939 
940  if (context_ && name_ != "")
941  {
942  ContextGuard context_guard(*context_);
943  MADARA_GUARD_TYPE guard(mutex_);
944 
945  if (!variable_.is_valid())
946  {
947  update_variable();
948  }
949 
950  result = context_->set(variable_, value, settings_);
951  }
952 
953  return result;
954 }
955 
957  double value, const KnowledgeUpdateSettings& settings)
958 {
959  int result = -1;
960 
961  if (context_ && name_ != "")
962  {
963  ContextGuard context_guard(*context_);
964  MADARA_GUARD_TYPE guard(mutex_);
965 
966  if (!variable_.is_valid())
967  {
968  update_variable();
969  }
970 
971  result = context_->set(variable_, value, settings);
972  }
973 
974  return result;
975 }
976 
978  size_t index, double value)
979 {
980  int result = -1;
981 
982  if (context_ && name_ != "")
983  {
984  ContextGuard context_guard(*context_);
985  MADARA_GUARD_TYPE guard(mutex_);
986 
987  if (!variable_.is_valid())
988  {
989  update_variable();
990  }
991 
992  result = context_->set_index(variable_, index, value, settings_);
993  }
994 
995  return result;
996 }
997 
999  size_t index, double value, const KnowledgeUpdateSettings& settings)
1000 {
1001  int result = -1;
1002 
1003  if (context_ && name_ != "")
1004  {
1005  ContextGuard context_guard(*context_);
1006  MADARA_GUARD_TYPE guard(mutex_);
1007 
1008  if (!variable_.is_valid())
1009  {
1010  update_variable();
1011  }
1012 
1013  result = context_->set_index(variable_, index, value, settings);
1014  }
1015 
1016  return result;
1017 }
1018 
1020  const double* value, uint32_t size)
1021 {
1022  int result = -1;
1023 
1024  if (context_ && name_ != "")
1025  {
1026  ContextGuard context_guard(*context_);
1027  MADARA_GUARD_TYPE guard(mutex_);
1028 
1029  if (!variable_.is_valid())
1030  {
1031  update_variable();
1032  }
1033 
1034  result = context_->set(variable_, value, size, settings_);
1035  }
1036 
1037  return result;
1038 }
1039 
1041  const double* value, uint32_t size, const KnowledgeUpdateSettings& settings)
1042 {
1043  int result = -1;
1044 
1045  if (context_ && name_ != "")
1046  {
1047  ContextGuard context_guard(*context_);
1048  MADARA_GUARD_TYPE guard(mutex_);
1049 
1050  if (!variable_.is_valid())
1051  {
1052  update_variable();
1053  }
1054 
1055  result = context_->set(variable_, value, size, settings);
1056  }
1057 
1058  return result;
1059 }
1060 
1062  const std::vector<double>& value)
1063 {
1064  int result = -1;
1065 
1066  if (context_ && name_ != "")
1067  {
1068  ContextGuard context_guard(*context_);
1069  MADARA_GUARD_TYPE guard(mutex_);
1070 
1071  if (!variable_.is_valid())
1072  {
1073  update_variable();
1074  }
1075 
1076  result = context_->set(variable_, value, settings_);
1077  }
1078 
1079  return result;
1080 }
1081 
1083  const std::vector<double>& value, const KnowledgeUpdateSettings& settings)
1084 {
1085  int result = -1;
1086 
1087  if (context_ && name_ != "")
1088  {
1089  ContextGuard context_guard(*context_);
1090  MADARA_GUARD_TYPE guard(mutex_);
1091 
1092  if (!variable_.is_valid())
1093  {
1094  update_variable();
1095  }
1096 
1097  result = context_->set(variable_, value, settings);
1098  }
1099 
1100  return result;
1101 }
1102 
1104 {
1105  int result = -1;
1106 
1107  if (context_ && name_ != "")
1108  {
1109  ContextGuard context_guard(*context_);
1110  MADARA_GUARD_TYPE guard(mutex_);
1111 
1112  if (!variable_.is_valid())
1113  {
1114  update_variable();
1115  }
1116 
1117  result = context_->set(variable_, value, settings_);
1118  }
1119 
1120  return result;
1121 }
1122 
1124  const std::string& value, const KnowledgeUpdateSettings& settings)
1125 {
1126  int result = -1;
1127 
1128  if (context_ && name_ != "")
1129  {
1130  ContextGuard context_guard(*context_);
1131  MADARA_GUARD_TYPE guard(mutex_);
1132 
1133  if (!variable_.is_valid())
1134  {
1135  update_variable();
1136  }
1137 
1138  result = context_->set(variable_, value, settings);
1139  }
1140 
1141  return result;
1142 }
1143 
1145  const unsigned char* value, size_t size)
1146 {
1147  int result = -1;
1148 
1149  if (context_ && name_ != "")
1150  {
1151  ContextGuard context_guard(*context_);
1152  MADARA_GUARD_TYPE guard(mutex_);
1153 
1154  if (!variable_.is_valid())
1155  {
1156  update_variable();
1157  }
1158 
1159  result = context_->set_file(variable_, value, size, settings_);
1160  }
1161 
1162  return result;
1163 }
1164 
1166  size_t size, const KnowledgeUpdateSettings& settings)
1167 {
1168  int result = -1;
1169 
1170  if (context_ && name_ != "")
1171  {
1172  ContextGuard context_guard(*context_);
1173  MADARA_GUARD_TYPE guard(mutex_);
1174 
1175  if (!variable_.is_valid())
1176  {
1177  update_variable();
1178  }
1179 
1180  result = context_->set_file(variable_, value, size, settings);
1181  }
1182 
1183  return result;
1184 }
1185 
1187  const unsigned char* value, size_t size)
1188 {
1189  int result = -1;
1190 
1191  if (context_ && name_ != "")
1192  {
1193  ContextGuard context_guard(*context_);
1194  MADARA_GUARD_TYPE guard(mutex_);
1195 
1196  if (!variable_.is_valid())
1197  {
1198  update_variable();
1199  }
1200 
1201  result = context_->set_jpeg(variable_, value, size, settings_);
1202  }
1203 
1204  return result;
1205 }
1206 
1208  size_t size, const KnowledgeUpdateSettings& settings)
1209 {
1210  int result = -1;
1211 
1212  if (context_ && name_ != "")
1213  {
1214  ContextGuard context_guard(*context_);
1215  MADARA_GUARD_TYPE guard(mutex_);
1216 
1217  if (!variable_.is_valid())
1218  {
1219  update_variable();
1220  }
1221 
1222  result = context_->set_jpeg(variable_, value, size, settings);
1223  }
1224 
1225  return result;
1226 }
1227 
1229  uint32_t quality, const KnowledgeReferenceSettings& settings)
1230 {
1231  if (context_)
1232  {
1233  ContextGuard context_guard(*context_);
1234  MADARA_GUARD_TYPE guard(mutex_);
1235 
1236  if (!variable_.is_valid())
1237  {
1238  update_variable();
1239  }
1240 
1241  context_->set_quality(name_, quality, true, settings);
1242  }
1243 }
1244 
1246 {
1247  return is_true();
1248 }
1249 
1251 {
1252  return is_false();
1253 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
const ThreadSafeContext * context_
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.
Settings for applying knowledge updates.
Settings for applying knowledge updates.
Optimized reference to a variable within the knowledge base.
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 character buffers.
Definition: BufferVector.h:32
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
This class stores a vector of doubles inside of KaRL.
Definition: DoubleVector.h:32
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
This class stores a double within a variable context.
Definition: Double.h:33
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Double.cpp:121
This class stores a flexible map of strings and ints to KaRL variables FlexMap differs from Map in th...
Definition: FlexMap.h:54
void modify(void)
Mark the flex map as modified.
Definition: FlexMap.cpp:84
void keys(std::vector< std::string > &curkeys, bool first_level_keys_only=true) const
Returns the keys within the map.
Definition: FlexMap.cpp:697
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: FlexMap.cpp:133
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: FlexMap.cpp:573
bool is_true(void) const
Checks if the value in the record is not false (0)
Definition: FlexMap.cpp:44
FlexMap operator[](const std::string &key)
Retrieves a flex map at the keyed location.
Definition: FlexMap.cpp:442
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: FlexMap.cpp:128
VariableReference variable_
The current location variable.
Definition: FlexMap.h:680
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 ...
Definition: FlexMap.cpp:1250
bool exists(void) const
Checks for the existence of the current location in the context.
Definition: FlexMap.cpp:651
FlexMap(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: FlexMap.cpp:6
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: FlexMap.h:675
void update_variable(void) const
Updates the variable reference if necessary.
Definition: FlexMap.cpp:623
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: FlexMap.cpp:106
int set_index(size_t index, knowledge::KnowledgeRecord::Integer value)
Sets an index within an array to a specified value.
Definition: FlexMap.cpp:805
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:636
void erase(const std::string &key, bool delete_subkeys=true)
Erases a variable from the map.
Definition: FlexMap.cpp:551
int read_file(const std::string &filename)
Read a file into a location in the map.
Definition: FlexMap.cpp:721
void exchange(FlexMap &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the map at this location with the map at another location.
Definition: FlexMap.cpp:511
void operator=(const FlexMap &rhs)
Assignment operator.
Definition: FlexMap.cpp:144
double to_double(double default_value=0.0) const
Returns the value at the location as a double.
Definition: FlexMap.cpp:236
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
Definition: FlexMap.cpp:1245
bool is_false(void) const
Checks if the value in the record is false (0)
Definition: FlexMap.cpp:69
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:642
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
Definition: FlexMap.cpp:1228
knowledge::KnowledgeRecord::Integer to_integer(KnowledgeRecord::Integer default_value=0) const
Returns the value at the location as an integer.
Definition: FlexMap.cpp:214
int set_jpeg(const unsigned char *value, size_t size)
Atomically sets the value of an index to a JPEG image.
Definition: FlexMap.cpp:1186
void clear(void)
Clears the map.
Definition: FlexMap.cpp:533
size_t size(bool first_level_keys_only=true) const
Returns the size of the map.
Definition: FlexMap.cpp:481
void to_container(BufferVector &target) const
Fills a BufferVector container with all subkeys.
Definition: FlexMap.cpp:280
std::string to_string(const std::string &default_value="") const
Returns the value at the location as a string.
Definition: FlexMap.cpp:258
int set_file(const unsigned char *value, size_t size)
Atomically sets the value of an index to an arbitrary string.
Definition: FlexMap.cpp:1144
knowledge::KnowledgeRecord to_record(const KnowledgeRecord &default_value=KnowledgeRecord(0)) const
Retrieves a copy of the record from the current location.
Definition: FlexMap.cpp:191
virtual BaseContainer * clone(void) const
Clones this container.
Definition: FlexMap.cpp:139
int set(knowledge::KnowledgeRecord::Integer value=knowledge::KnowledgeRecord::MODIFIED)
Sets the location within the map to the specified value.
Definition: FlexMap.cpp:762
This class stores a vector of integers inside of KaRL.
Definition: IntegerVector.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
This class stores an integer within a variable context.
Definition: Integer.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Integer.inl:54
This class stores a map of strings to KaRL variables.
Definition: Map.h:33
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
Definition: Map.cpp:449
size_t size(void) const
Returns the size of the map.
Definition: Map.cpp:200
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:411
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Map.cpp:85
void exchange(Map &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the map at this location with the map at another location.
Definition: Map.cpp:239
void modify(void)
Mark the vector as modified.
Definition: Map.cpp:70
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
Definition: Map.cpp:501
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:355
This class stores a vector of doubles inside of KaRL.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
This class stores a vector of doubles inside of KaRL.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
This class stores a string within a variable context.
Definition: String.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: String.cpp:124
constexpr string_t string
Provides functions and classes for the distributed knowledge base.