MADARA  3.2.3
FlexMap.cpp
Go to the documentation of this file.
1 #include "FlexMap.h"
3 
4 #include <algorithm>
5 
7  const KnowledgeUpdateSettings & settings,
8  const std::string & delimiter)
9  : BaseContainer ("", settings), context_ (0), delimiter_ (delimiter)
10 {
11 }
12 
13 
15  const std::string & name,
17  const KnowledgeUpdateSettings & settings,
18  const std::string & delimiter)
19  : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
20  delimiter_ (delimiter)
21 {
22 }
23 
25  const std::string & name,
27  const KnowledgeUpdateSettings & settings,
28  const std::string & delimiter)
29  : BaseContainer (name, settings), context_ (knowledge.get_context ()),
30  delimiter_ (delimiter)
31 {
32 }
33 
34 
36  : BaseContainer (rhs), context_ (rhs.context_),
37  variable_ (rhs.variable_),
39 {
40 
41 }
42 
44 {
45 
46 }
47 
53 {
54  ContextGuard context_guard (*context_);
55  bool result (false);
56 
57  KnowledgeUpdateSettings keep_local (true);
58 
60  "Vector::is_true: checking for non-zero value\n");
61 
62  if (!variable_.is_valid ())
63  {
64  result = context_->get (variable_, keep_local).is_true ();
65  }
66 
68  "FlexMap::is_true: final result is %d\n", (int)result);
69 
70  return result;
71 }
72 
78 {
79  ContextGuard context_guard (*context_);
80  bool result (true);
81 
82  KnowledgeUpdateSettings keep_local (true);
83 
84  if (!variable_.is_valid ())
85  {
86  result = context_->get (variable_, keep_local).is_true ();
87  }
88 
89  return result;
90 }
91 
92 void
94 {
95  if (context_ && name_ != "")
96  {
97  ContextGuard context_guard (*context_);
98 
99  // get all children
101  knowledge.facade_for (*context_);
102  Map map (name_, knowledge, settings_, delimiter_);
103 
104  // modify the children
105  map.modify ();
106 
107  // modify ourself
108  if (variable_.is_valid ())
109  {
110  knowledge.mark_modified (variable_);
111  }
112  }
113 }
114 
117 {
118  std::stringstream result;
119 
120  result << "Flex Map: ";
121 
122  if (context_ && name_ != "")
123  {
124  ContextGuard context_guard (*context_);
125  MADARA_GUARD_TYPE guard (mutex_);
126 
127  // get all children
129  knowledge.facade_for (*context_);
130  Map map (name_, knowledge, settings_, delimiter_);
131 
132  result << map.get_debug_info ();
133  }
134 
135  return result.str ();
136 }
137 
138 
139 void
141 {
142  modify ();
143 }
144 
147 {
148  return get_debug_info ();
149 }
150 
153 {
154  return new FlexMap (*this);
155 }
156 
157 void
159  const FlexMap & rhs)
160 {
161  if (this != &rhs)
162  {
163  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
164 
165  this->context_ = rhs.context_;
166  this->name_ = rhs.name_;
167  this->settings_ = rhs.settings_;
168  this->variable_ = rhs.variable_;
169  }
170 }
171 
172 
173 void
176 {
177  set (value, settings_);
178 }
179 
180 void
182 int value)
183 {
184  set (KnowledgeRecord::Integer (value), settings_);
185 }
186 
187 void
189  double value)
190 {
191  set (value, settings_);
192 }
193 
194 void
196  const std::string value)
197 {
198  set (value, settings_);
199 }
200 
201 void
203  const std::vector <KnowledgeRecord::Integer> & value)
204 {
205  set (value, settings_);
206 }
207 
208 void
210 const std::vector <double> & value)
211 {
212  set (value, settings_);
213 }
214 
215 
218  const KnowledgeRecord & default_value) const
219 {
220  if (context_)
221  {
222  ContextGuard context_guard (*context_);
223  MADARA_GUARD_TYPE guard (mutex_);
224 
225  if (!variable_.is_valid ())
226  {
227  this->update_variable ();
228  }
229 
230  if (context_->exists (variable_))
231  {
232  return context_->get (variable_, settings_);
233  }
234  }
235 
236  return default_value;
237 }
238 
241  KnowledgeRecord::Integer default_value) const
242 {
243  if (context_)
244  {
245  ContextGuard context_guard (*context_);
246  MADARA_GUARD_TYPE guard (mutex_);
247 
248  if (!variable_.is_valid ())
249  {
250  this->update_variable ();
251  }
252 
253  if (context_->exists (variable_))
254  {
255  return context_->get (variable_, settings_).to_integer ();
256  }
257  }
258 
259  return default_value;
260 }
261 
262 double
264 {
265  if (context_)
266  {
267  ContextGuard context_guard (*context_);
268  MADARA_GUARD_TYPE guard (mutex_);
269 
270  if (!variable_.is_valid ())
271  {
272  this->update_variable ();
273  }
274 
275  if (context_->exists (variable_))
276  {
277  return context_->get (variable_, settings_).to_double ();
278  }
279  }
280 
281  return default_value;
282 }
283 
286  const std::string & default_value) const
287 {
288  if (context_)
289  {
290  ContextGuard context_guard (*context_);
291  MADARA_GUARD_TYPE guard (mutex_);
292 
293  if (!variable_.is_valid ())
294  {
295  this->update_variable ();
296  }
297 
298  if (context_->exists (variable_))
299  {
300  return context_->get (variable_, settings_).to_string ();
301  }
302  }
303 
304  return default_value;
305 }
306 
307 
308 void
310  BufferVector & target) const
311 {
312  if (context_)
313  {
314  ContextGuard context_guard (*context_);
315  MADARA_GUARD_TYPE guard (mutex_);
316 
317  // get all children
319  knowledge.facade_for (*context_);
320 
321  target.set_delimiter (delimiter_);
322  target.set_name (name_, knowledge);
323  }
324 }
325 
326 
327 void
329  DoubleVector & target) const
330 {
331  if (context_)
332  {
333  ContextGuard context_guard (*context_);
334  MADARA_GUARD_TYPE guard (mutex_);
335 
336  // get all children
338  knowledge.facade_for (*context_);
339 
340  target.set_delimiter (delimiter_);
341  target.set_name (name_, knowledge);
342  }
343 }
344 
345 
346 void
348  IntegerVector & 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 
364 void
366  NativeIntegerVector & target) const
367 {
368  if (context_)
369  {
370  ContextGuard context_guard (*context_);
371  MADARA_GUARD_TYPE guard (mutex_);
372 
373  // get all children
375  knowledge.facade_for (*context_);
376 
377  target.set_name (name_, knowledge);
378  }
379 }
380 
381 
382 void
384  StringVector & target) const
385 {
386  if (context_)
387  {
388  ContextGuard context_guard (*context_);
389  MADARA_GUARD_TYPE guard (mutex_);
390 
391  // get all children
393  knowledge.facade_for (*context_);
394 
395  target.set_delimiter (delimiter_);
396  target.set_name (name_, knowledge);
397  }
398 }
399 
400 
401 void
403  Double & target) const
404 {
405  if (context_)
406  {
407  ContextGuard context_guard (*context_);
408  MADARA_GUARD_TYPE guard (mutex_);
409 
410  // get all children
412  knowledge.facade_for (*context_);
413 
414  target.set_name (name_, knowledge);
415  }
416 }
417 
418 void
420  NativeDoubleVector & target) const
421 {
422  if (context_)
423  {
424  ContextGuard context_guard (*context_);
425  MADARA_GUARD_TYPE guard (mutex_);
426 
427  // get all children
429  knowledge.facade_for (*context_);
430 
431  target.set_name (name_, knowledge);
432  }
433 }
434 
435 
436 void
438  Integer & target) const
439 {
440  if (context_)
441  {
442  ContextGuard context_guard (*context_);
443  MADARA_GUARD_TYPE guard (mutex_);
444 
445  // get all children
447  knowledge.facade_for (*context_);
448 
449  target.set_name (name_, knowledge);
450  }
451 }
452 
453 
454 void
456  String & target) const
457 {
458  if (context_)
459  {
460  ContextGuard context_guard (*context_);
461  MADARA_GUARD_TYPE guard (mutex_);
462 
463  // get all children
465  knowledge.facade_for (*context_);
466 
467  target.set_name (name_, knowledge);
468  }
469 }
470 
471 
472 void
474  Map & target) const
475 {
476  if (context_)
477  {
478  ContextGuard context_guard (*context_);
479  MADARA_GUARD_TYPE guard (mutex_);
480 
481  // get all children
483  knowledge.facade_for (*context_);
484 
485  target.set_delimiter (delimiter_);
486  target.set_name (name_, knowledge);
487  }
488 }
489 
490 
493  const std::string & key)
494 {
496 
497  if (key != "" && context_)
498  {
499  ContextGuard context_guard (*context_);
500  MADARA_GUARD_TYPE guard (mutex_);
501 
503  knowledge.facade_for (*context_);
504  map.set_name (name_ + delimiter_ + key, knowledge);
505  }
506 
507  return map;
508 }
509 
512  size_t index)
513 {
515 
516  if (context_)
517  {
518  ContextGuard context_guard (*context_);
519  MADARA_GUARD_TYPE guard (mutex_);
520 
521  std::stringstream buffer;
523  knowledge.facade_for (*context_);
524 
525  buffer << name_ << delimiter_ << index;
526 
527  map.set_name (buffer.str (), knowledge);
528  }
529 
530  return map;
531 }
532 
533 size_t
535  bool first_level_keys_only) const
536 {
537  ContextGuard context_guard (*context_);
538  MADARA_GUARD_TYPE guard (mutex_);
539 
540  size_t result (0);
541 
542  if (!first_level_keys_only)
543  {
544  // get all children
546  knowledge.facade_for (*context_);
547  Map map (name_, knowledge, settings_, delimiter_);
548 
549  result = map.size ();
550  }
551  else
552  {
553  // use the context's specialized mapping feature
554  std::vector<std::string> next_keys;
555  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
557  next_keys, all_record_vars, true);
558 
559  result = next_keys.size ();
560  }
561 
562  return result;
563 }
564 
565 void
567  FlexMap & other, bool refresh_keys, bool delete_keys)
568 {
569  if (context_ && other.context_)
570  {
571  std::lock(*context_, *other.context_, mutex_, other.mutex_);
572 
573  ContextGuard context_guard (*context_, std::adopt_lock);
574  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
575  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
576  guard2 (other.mutex_, std::adopt_lock);
577 
578 
579  // use the Map container, which already has exchange implemented
581  knowledge.facade_for (*context_);
582  Map this_map (name_, knowledge, settings_, delimiter_);
583  Map other_map (other.name_, knowledge, other.settings_, delimiter_);
584 
585  this_map.exchange (other_map, refresh_keys, delete_keys);
586  }
587 }
588 
589 void
591 {
592  if (context_)
593  {
594  ContextGuard context_guard (*context_);
595  MADARA_GUARD_TYPE guard (mutex_);
596 
597  // use the Map container, which already has clear implemented
599  knowledge.facade_for (*context_);
600  Map map (name_, knowledge, settings_, delimiter_);
601 
602  map.clear ();
603 
605  }
606 }
607 
608 void
610  const std::string & key, bool delete_subkeys)
611 {
612  if (context_)
613  {
614  ContextGuard context_guard (*context_);
615  MADARA_GUARD_TYPE guard (mutex_);
616 
618 
619  if (delete_subkeys)
620  {
621  // use the Map container, which already has clear implemented
623  knowledge.facade_for (*context_);
624  Map map (name_ + delimiter_ + key, knowledge, settings_, delimiter_);
625 
626  map.clear ();
627  }
628  }
629 }
630 
631 void
633  const std::string & var_name,
635 {
636  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
637  {
638  context_ = &(knowledge.get_context ());
639 
640  ContextGuard context_guard (*context_);
641  MADARA_GUARD_TYPE guard (mutex_);
642 
643  name_ = var_name;
644 
645  if (context_->exists (var_name, settings_))
646  {
647  KnowledgeUpdateSettings keep_local (true);
648 
649  variable_ = context_->get_ref (var_name, keep_local);
650  }
651  else
652  {
653  // reset variable reference
655  }
656  }
657 }
658 
659 void
661  const std::string & var_name,
663 {
664  if (context_ != knowledge.get_context () || name_ != var_name)
665  {
666  context_ = knowledge.get_context ();
667 
668  ContextGuard context_guard (*context_);
669  MADARA_GUARD_TYPE guard (mutex_);
670 
671  name_ = var_name;
672 
673  if (context_->exists (var_name, settings_))
674  {
675  update_variable ();
676  }
677  else
678  {
679  // reset variable reference
681  }
682  }
683 }
684 
685 void
687 {
688  if (context_ && name_ != "")
689  {
690  ContextGuard context_guard (*context_);
691  MADARA_GUARD_TYPE guard (mutex_);
692 
693  KnowledgeUpdateSettings keep_local (true);
694 
695  variable_ = context_->get_ref (name_, keep_local);
696  }
697 }
698 
699 void
701 const std::string & delimiter)
702 {
703  delimiter_ = delimiter;
704 }
705 
706 
709 {
710  return delimiter_;
711 }
712 
717 bool
719 {
720  ContextGuard context_guard (*context_);
721  bool result (true);
722 
723  KnowledgeUpdateSettings keep_local (true);
724 
725  if (!variable_.is_valid ())
726  {
727  result = context_->exists (variable_, keep_local);
728  }
729 
730  return result;
731 }
732 
733 bool
735  const std::string & key, bool first_level_key) const
736 {
737  bool result (false);
738 
739  if (context_)
740  {
741  ContextGuard context_guard (*context_);
742  MADARA_GUARD_TYPE guard (mutex_);
743 
744  // check if the key exists
745  std::string exact_key = name_ + delimiter_ + key;
746  result = context_->exists (exact_key);
747 
748  if (!result && first_level_key)
749  {
750  std::vector <std::string> curkeys;
751  keys (curkeys);
752 
753  std::vector <std::string>::iterator found;
754 
755  // look for the key in the sub keys
756  found = std::find (curkeys.begin (), curkeys.end (), key);
757 
758  result = found != curkeys.end ();
759  }
760  }
761 
762  return result;
763 }
764 
765 void
767  std::vector <std::string> & curkeys,
768  bool first_level_keys_only) const
769 {
770  ContextGuard context_guard (*context_);
771  MADARA_GUARD_TYPE guard (mutex_);
772 
773  if (!first_level_keys_only)
774  {
775  // use the Map container, which already has clear implemented
777  knowledge.facade_for (*context_);
778  Map map (name_, knowledge, settings_, delimiter_);
779 
780  map.keys (curkeys);
781  }
782  else
783  {
784  // use the context's specialized mapping feature
785  std::vector<std::string> next_keys;
786  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
788  curkeys, all_record_vars, true);
789  }
790 }
791 
792 int
794  const std::string & filename)
795 {
796  int result = -1;
797 
798  if (context_ && name_ != "")
799  {
800  ContextGuard context_guard (*context_);
801  MADARA_GUARD_TYPE guard (mutex_);
802 
803  if (!variable_.is_valid ())
804  {
805  update_variable ();
806  }
807 
808  result = context_->read_file (variable_, filename, settings_);
809  }
810  return result;
811 }
812 
813 int
815 
816  const std::string & filename,
817  const KnowledgeUpdateSettings & settings)
818 {
819  int result = -1;
820 
821  if (context_ && name_ != "")
822  {
823  ContextGuard context_guard (*context_);
824  MADARA_GUARD_TYPE guard (mutex_);
825 
826  if (!variable_.is_valid ())
827  {
828  update_variable ();
829  }
830 
831  result = context_->read_file (variable_, filename, settings);
832  }
833  return result;
834 }
835 
838 {
839  int result = -1;
840 
841  if (context_ && name_ != "")
842  {
843  ContextGuard context_guard (*context_);
844  MADARA_GUARD_TYPE guard (mutex_);
845 
846  if (!variable_.is_valid ())
847  {
848  update_variable ();
849  }
850 
851  result = context_->set (variable_, value, settings_);
852  }
853 
854  return result;
855 }
856 
859  const KnowledgeUpdateSettings & settings)
860 {
861  int result = -1;
862 
863  if (context_ && name_ != "")
864  {
865  ContextGuard context_guard (*context_);
866  MADARA_GUARD_TYPE guard (mutex_);
867 
868  if (!variable_.is_valid ())
869  {
870  update_variable ();
871  }
872 
873  result = context_->set (variable_, value, settings);
874  }
875 
876  return result;
877 }
878 
880 
881  size_t index,
883 {
884  int result = -1;
885 
886  if (context_ && name_ != "")
887  {
888  ContextGuard context_guard (*context_);
889  MADARA_GUARD_TYPE guard (mutex_);
890 
891  if (!variable_.is_valid ())
892  {
893  update_variable ();
894  }
895 
896  result = context_->set_index (variable_, index, value, settings_);
897  }
898 
899  return result;
900 }
901 
903 
904  size_t index,
906  const KnowledgeUpdateSettings & settings)
907 {
908  int result = -1;
909 
910  if (context_ && name_ != "")
911  {
912  ContextGuard context_guard (*context_);
913  MADARA_GUARD_TYPE guard (mutex_);
914 
915  if (!variable_.is_valid ())
916  {
917  update_variable ();
918  }
919 
920  result = context_->set_index (variable_, index, value, settings);
921  }
922 
923  return result;
924 }
925 
926 
929  uint32_t size)
930 {
931  int result = -1;
932 
933  if (context_ && name_ != "")
934  {
935  ContextGuard context_guard (*context_);
936  MADARA_GUARD_TYPE guard (mutex_);
937 
938  if (!variable_.is_valid ())
939  {
940  update_variable ();
941  }
942 
943  result = context_->set (variable_, value, size, settings_);
944  }
945 
946  return result;
947 }
948 
951  uint32_t size,
952  const KnowledgeUpdateSettings & settings)
953 {
954  int result = -1;
955 
956  if (context_ && name_ != "")
957  {
958  ContextGuard context_guard (*context_);
959  MADARA_GUARD_TYPE guard (mutex_);
960 
961  if (!variable_.is_valid ())
962  {
963  update_variable ();
964  }
965 
966  result = context_->set (variable_, value, size, settings);
967  }
968 
969  return result;
970 }
971 
973  const std::vector <KnowledgeRecord::Integer> & value)
974 {
975  int result = -1;
976 
977  if (context_ && name_ != "")
978  {
979  ContextGuard context_guard (*context_);
980  MADARA_GUARD_TYPE guard (mutex_);
981 
982  if (!variable_.is_valid ())
983  {
984  update_variable ();
985  }
986 
987  result = context_->set (variable_, value, settings_);
988  }
989 
990  return result;
991 }
992 
994  const std::vector <KnowledgeRecord::Integer> & value,
995  const KnowledgeUpdateSettings & settings)
996 {
997  int result = -1;
998 
999  if (context_ && name_ != "")
1000  {
1001  ContextGuard context_guard (*context_);
1002  MADARA_GUARD_TYPE guard (mutex_);
1003 
1004  if (!variable_.is_valid ())
1005  {
1006  update_variable ();
1007  }
1008 
1009  result = context_->set (variable_, value, settings);
1010  }
1011 
1012  return result;
1013 }
1014 
1016  double value)
1017 {
1018  int result = -1;
1019 
1020  if (context_ && name_ != "")
1021  {
1022  ContextGuard context_guard (*context_);
1023  MADARA_GUARD_TYPE guard (mutex_);
1024 
1025  if (!variable_.is_valid ())
1026  {
1027  update_variable ();
1028  }
1029 
1030  result = context_->set (variable_, value, settings_);
1031  }
1032 
1033  return result;
1034 }
1035 
1037  double value,
1038  const KnowledgeUpdateSettings & settings)
1039 {
1040  int result = -1;
1041 
1042  if (context_ && name_ != "")
1043  {
1044  ContextGuard context_guard (*context_);
1045  MADARA_GUARD_TYPE guard (mutex_);
1046 
1047  if (!variable_.is_valid ())
1048  {
1049  update_variable ();
1050  }
1051 
1052  result = context_->set (variable_, value, settings);
1053  }
1054 
1055  return result;
1056 }
1057 
1059  size_t index,
1060  double value)
1061 {
1062  int result = -1;
1063 
1064  if (context_ && name_ != "")
1065  {
1066  ContextGuard context_guard (*context_);
1067  MADARA_GUARD_TYPE guard (mutex_);
1068 
1069  if (!variable_.is_valid ())
1070  {
1071  update_variable ();
1072  }
1073 
1074  result = context_->set_index (variable_, index, value, settings_);
1075  }
1076 
1077  return result;
1078 }
1079 
1081  size_t index,
1082  double value,
1083  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_index (variable_, index, value, settings);
1098  }
1099 
1100  return result;
1101 }
1102 
1104  const double * value,
1105  uint32_t size)
1106 {
1107  int result = -1;
1108 
1109  if (context_ && name_ != "")
1110  {
1111  ContextGuard context_guard (*context_);
1112  MADARA_GUARD_TYPE guard (mutex_);
1113 
1114  if (!variable_.is_valid ())
1115  {
1116  update_variable ();
1117  }
1118 
1119  result = context_->set (variable_, value, size, settings_);
1120  }
1121 
1122  return result;
1123 }
1124 
1126  const double * value,
1127  uint32_t size,
1128  const KnowledgeUpdateSettings & settings)
1129 {
1130  int result = -1;
1131 
1132  if (context_ && name_ != "")
1133  {
1134  ContextGuard context_guard (*context_);
1135  MADARA_GUARD_TYPE guard (mutex_);
1136 
1137  if (!variable_.is_valid ())
1138  {
1139  update_variable ();
1140  }
1141 
1142  result = context_->set (variable_, value, size, settings);
1143  }
1144 
1145  return result;
1146 }
1147 
1149  const std::vector <double> & value)
1150 {
1151  int result = -1;
1152 
1153  if (context_ && name_ != "")
1154  {
1155  ContextGuard context_guard (*context_);
1156  MADARA_GUARD_TYPE guard (mutex_);
1157 
1158  if (!variable_.is_valid ())
1159  {
1160  update_variable ();
1161  }
1162 
1163  result = context_->set (variable_, value, settings_);
1164  }
1165 
1166  return result;
1167 }
1168 
1170  const std::vector <double> & value,
1171  const KnowledgeUpdateSettings & settings)
1172 {
1173  int result = -1;
1174 
1175  if (context_ && name_ != "")
1176  {
1177  ContextGuard context_guard (*context_);
1178  MADARA_GUARD_TYPE guard (mutex_);
1179 
1180  if (!variable_.is_valid ())
1181  {
1182  update_variable ();
1183  }
1184 
1185  result = context_->set (variable_, value, settings);
1186  }
1187 
1188  return result;
1189 }
1190 
1192  const std::string & value)
1193 {
1194  int result = -1;
1195 
1196  if (context_ && name_ != "")
1197  {
1198  ContextGuard context_guard (*context_);
1199  MADARA_GUARD_TYPE guard (mutex_);
1200 
1201  if (!variable_.is_valid ())
1202  {
1203  update_variable ();
1204  }
1205 
1206  result = context_->set (variable_, value, settings_);
1207  }
1208 
1209  return result;
1210 }
1211 
1213  const std::string & value,
1214  const KnowledgeUpdateSettings & settings)
1215 {
1216  int result = -1;
1217 
1218  if (context_ && name_ != "")
1219  {
1220  ContextGuard context_guard (*context_);
1221  MADARA_GUARD_TYPE guard (mutex_);
1222 
1223  if (!variable_.is_valid ())
1224  {
1225  update_variable ();
1226  }
1227 
1228  result = context_->set (variable_, value, settings);
1229  }
1230 
1231  return result;
1232 }
1233 
1235  const unsigned char * value, size_t size)
1236 {
1237  int result = -1;
1238 
1239  if (context_ && name_ != "")
1240  {
1241  ContextGuard context_guard (*context_);
1242  MADARA_GUARD_TYPE guard (mutex_);
1243 
1244  if (!variable_.is_valid ())
1245  {
1246  update_variable ();
1247  }
1248 
1249  result = context_->set_file (variable_, value, size, settings_);
1250  }
1251 
1252  return result;
1253 }
1254 
1256  const unsigned char * value, size_t size,
1257  const KnowledgeUpdateSettings & settings)
1258 {
1259  int result = -1;
1260 
1261  if (context_ && name_ != "")
1262  {
1263  ContextGuard context_guard (*context_);
1264  MADARA_GUARD_TYPE guard (mutex_);
1265 
1266  if (!variable_.is_valid ())
1267  {
1268  update_variable ();
1269  }
1270 
1271  result = context_->set_file (variable_, value, size, settings);
1272  }
1273 
1274  return result;
1275 }
1276 
1278  const unsigned char * value, size_t size)
1279 {
1280  int result = -1;
1281 
1282  if (context_ && name_ != "")
1283  {
1284  ContextGuard context_guard (*context_);
1285  MADARA_GUARD_TYPE guard (mutex_);
1286 
1287  if (!variable_.is_valid ())
1288  {
1289  update_variable ();
1290  }
1291 
1292  result = context_->set_jpeg (variable_, value, size, settings_);
1293  }
1294 
1295  return result;
1296 }
1297 
1299  const unsigned char * value, size_t size,
1300  const KnowledgeUpdateSettings & settings)
1301 {
1302  int result = -1;
1303 
1304  if (context_ && name_ != "")
1305  {
1306  ContextGuard context_guard (*context_);
1307  MADARA_GUARD_TYPE guard (mutex_);
1308 
1309  if (!variable_.is_valid ())
1310  {
1311  update_variable ();
1312  }
1313 
1314  result = context_->set_jpeg (variable_, value, size, settings);
1315  }
1316 
1317  return result;
1318 }
1319 
1320 void
1322  uint32_t quality,
1323  const KnowledgeReferenceSettings & settings)
1324 {
1325  if (context_)
1326  {
1327  ContextGuard context_guard (*context_);
1328  MADARA_GUARD_TYPE guard (mutex_);
1329 
1330  if (!variable_.is_valid ())
1331  {
1332  update_variable ();
1333  }
1334 
1335  context_->set_quality (name_, quality, true, settings);
1336  }
1337 }
1338 
1339 bool
1341 {
1342  return is_true ();
1343 }
1344 
1345 bool
1347 {
1348  return is_false ();
1349 }
This class encapsulates an entry in a KnowledgeBase.
This class stores a double within a variable context.
Definition: Double.h:32
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
This class stores a vector of character buffers.
Definition: BufferVector.h:31
double to_double(double default_value=0.0) const
Returns the value at the location as a double.
Definition: FlexMap.cpp:263
bool is_true(void) const
Checks to see if the record is true.
This class stores a vector of doubles inside of KaRL.
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)
Sets the variable name that this refers to.
Definition: Integer.inl:58
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
bool is_true(void) const
Checks if the value in the record is not false (0)
Definition: FlexMap.cpp:52
double to_double(void) const
converts the value to a float/double.
std::string name_
Prefix of variable.
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
Definition: Map.cpp:516
void operator=(const FlexMap &rhs)
Assignment operator.
Definition: FlexMap.cpp:158
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
FlexMap operator[](const std::string &key)
Retrieves a flex map at the keyed location.
Definition: FlexMap.cpp:492
This class stores a string within a variable context.
Definition: String.h:31
int set_index(size_t index, knowledge::KnowledgeRecord::Integer value)
Sets an index within an array to a specified value.
Definition: FlexMap.cpp:879
int set(knowledge::KnowledgeRecord::Integer value=knowledge::KnowledgeRecord::MODIFIED)
Sets the location within the map to the specified value.
Definition: FlexMap.cpp:836
std::string to_string(const std::string &default_value="") const
Returns the value at the location as a string.
Definition: FlexMap.cpp:285
This class stores a vector of integers inside of KaRL.
Definition: IntegerVector.h:31
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: FlexMap.h:696
MADARA_LOCK_TYPE mutex_
guard for access and changes
void update_variable(void) const
Updates the variable reference if necessary.
Definition: FlexMap.cpp:686
Optimized reference to a variable within the knowledge base.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: FlexMap.cpp:140
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:31
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
Definition: Map.cpp:457
void erase(const std::string &key, bool delete_subkeys=true)
Erases a variable from the map.
Definition: FlexMap.cpp:609
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
void to_container(BufferVector &target) const
Fills a BufferVector container with all subkeys.
Definition: FlexMap.cpp:309
virtual BaseContainer * clone(void) const
Clones this container.
Definition: FlexMap.cpp:152
static struct madara::knowledge::tags::string_t string
int set_file(const unsigned char *value, size_t size)
Atomically sets the value of an index to an arbitrary string.
Definition: FlexMap.cpp:1234
void modify(void)
Mark the vector as modified.
Definition: Map.cpp:76
bool is_false(void) const
Checks if the value in the record is false (0)
Definition: FlexMap.cpp:77
bool exists(void) const
Checks for the existence of the current location in the context.
Definition: FlexMap.cpp:718
void facade_for(ThreadSafeContext &target)
Change the knowledge base to become a facade for another context.
This class stores a map of strings to KaRL variables.
Definition: Map.h:32
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:415
This class stores an integer within a variable context.
Definition: Integer.h:31
void clear(void)
Clears the map.
Definition: FlexMap.cpp:590
This class stores a vector of doubles inside of KaRL.
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:259
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: FlexMap.cpp:116
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
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:566
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:700
Integer to_integer(void) const
converts the value to an integer.
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: FlexMap.h:706
virtual ~FlexMap()
Destructor.
Definition: FlexMap.cpp:43
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:376
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: FlexMap.cpp:632
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
int set_jpeg(const unsigned char *value, size_t size)
Atomically sets the value of an index to a JPEG image.
Definition: FlexMap.cpp:1277
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
knowledge::KnowledgeRecord::Integer to_integer(KnowledgeRecord::Integer default_value=0) const
Returns the value at the location as an integer.
Definition: FlexMap.cpp:240
int read_file(const std::string &filename)
Read a file into a location in the map.
Definition: FlexMap.cpp:793
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: FlexMap.cpp:146
FlexMap(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: FlexMap.cpp:6
Settings for applying knowledge updates.
knowledge::KnowledgeRecord to_record(const KnowledgeRecord &default_value=KnowledgeRecord(0)) const
Retrieves a copy of the record from the current location.
Definition: FlexMap.cpp:217
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
VariableReference variable_
The current location variable.
Definition: FlexMap.h:701
Settings for applying knowledge updates.
This class stores a vector of doubles inside of KaRL.
Definition: DoubleVector.h:31
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
Provides an interface for external functions into the MADARA KaRL variable settings.
size_t size(bool first_level_keys_only=true) const
Returns the size of the map.
Definition: FlexMap.cpp:534
This class stores a flexible map of strings and ints to KaRL variables FlexMap differs from Map in th...
Definition: FlexMap.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
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:1346
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:1340
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:708
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Double.cpp:146
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.
void modify(void)
Mark the flex map as modified.
Definition: FlexMap.cpp:93
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Map.cpp:92
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: String.cpp:147
void keys(std::vector< std::string > &curkeys, bool first_level_keys_only=true) const
Returns the keys within the map.
Definition: FlexMap.cpp:766
size_t size(void) const
Returns the size of the map.
Definition: Map.cpp:217
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
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:1321