MADARA  3.1.8
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 {
220 
221  if (context_)
222  {
223  ContextGuard context_guard (*context_);
224  MADARA_GUARD_TYPE guard (mutex_);
225 
226  KnowledgeUpdateSettings keep_local (true);
227 
228  if (!variable_.is_valid ())
229  {
230  this->update_variable ();
231  }
232 
233  result = context_->get (variable_, settings_);
234  }
235 
236  return result;
237 }
238 
241 {
242  return to_record ().to_integer ();
243 }
244 
245 double
247 {
248  return to_record ().to_double ();
249 }
250 
253 {
254  return to_record ().to_string ();
255 }
256 
257 
258 void
260  BufferVector & target) const
261 {
262  if (context_)
263  {
264  ContextGuard context_guard (*context_);
265  MADARA_GUARD_TYPE guard (mutex_);
266 
267  // get all children
269  knowledge.facade_for (*context_);
270 
271  target.set_delimiter (delimiter_);
272  target.set_name (name_, knowledge);
273  }
274 }
275 
276 
277 void
279  DoubleVector & target) const
280 {
281  if (context_)
282  {
283  ContextGuard context_guard (*context_);
284  MADARA_GUARD_TYPE guard (mutex_);
285 
286  // get all children
288  knowledge.facade_for (*context_);
289 
290  target.set_delimiter (delimiter_);
291  target.set_name (name_, knowledge);
292  }
293 }
294 
295 
296 void
298  IntegerVector & 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 
314 void
316  NativeIntegerVector & target) const
317 {
318  if (context_)
319  {
320  ContextGuard context_guard (*context_);
321  MADARA_GUARD_TYPE guard (mutex_);
322 
323  // get all children
325  knowledge.facade_for (*context_);
326 
327  target.set_name (name_, knowledge);
328  }
329 }
330 
331 
332 void
334  StringVector & target) const
335 {
336  if (context_)
337  {
338  ContextGuard context_guard (*context_);
339  MADARA_GUARD_TYPE guard (mutex_);
340 
341  // get all children
343  knowledge.facade_for (*context_);
344 
345  target.set_delimiter (delimiter_);
346  target.set_name (name_, knowledge);
347  }
348 }
349 
350 
351 void
353  Double & target) const
354 {
355  if (context_)
356  {
357  ContextGuard context_guard (*context_);
358  MADARA_GUARD_TYPE guard (mutex_);
359 
360  // get all children
362  knowledge.facade_for (*context_);
363 
364  target.set_name (name_, knowledge);
365  }
366 }
367 
368 void
370  NativeDoubleVector & target) const
371 {
372  if (context_)
373  {
374  ContextGuard context_guard (*context_);
375  MADARA_GUARD_TYPE guard (mutex_);
376 
377  // get all children
379  knowledge.facade_for (*context_);
380 
381  target.set_name (name_, knowledge);
382  }
383 }
384 
385 
386 void
388  Integer & target) const
389 {
390  if (context_)
391  {
392  ContextGuard context_guard (*context_);
393  MADARA_GUARD_TYPE guard (mutex_);
394 
395  // get all children
397  knowledge.facade_for (*context_);
398 
399  target.set_name (name_, knowledge);
400  }
401 }
402 
403 
404 void
406  String & target) const
407 {
408  if (context_)
409  {
410  ContextGuard context_guard (*context_);
411  MADARA_GUARD_TYPE guard (mutex_);
412 
413  // get all children
415  knowledge.facade_for (*context_);
416 
417  target.set_name (name_, knowledge);
418  }
419 }
420 
421 
422 void
424  Map & target) const
425 {
426  if (context_)
427  {
428  ContextGuard context_guard (*context_);
429  MADARA_GUARD_TYPE guard (mutex_);
430 
431  // get all children
433  knowledge.facade_for (*context_);
434 
435  target.set_delimiter (delimiter_);
436  target.set_name (name_, knowledge);
437  }
438 }
439 
440 
443  const std::string & key)
444 {
446 
447  if (key != "" && context_)
448  {
449  ContextGuard context_guard (*context_);
450  MADARA_GUARD_TYPE guard (mutex_);
451 
453  knowledge.facade_for (*context_);
454  map.set_name (name_ + delimiter_ + key, knowledge);
455  }
456 
457  return map;
458 }
459 
462  size_t index)
463 {
465 
466  if (context_)
467  {
468  ContextGuard context_guard (*context_);
469  MADARA_GUARD_TYPE guard (mutex_);
470 
471  std::stringstream buffer;
473  knowledge.facade_for (*context_);
474 
475  buffer << name_ << delimiter_ << index;
476 
477  map.set_name (buffer.str (), knowledge);
478  }
479 
480  return map;
481 }
482 
483 size_t
485  bool first_level_keys_only) const
486 {
487  ContextGuard context_guard (*context_);
488  MADARA_GUARD_TYPE guard (mutex_);
489 
490  size_t result (0);
491 
492  if (!first_level_keys_only)
493  {
494  // get all children
496  knowledge.facade_for (*context_);
497  Map map (name_, knowledge, settings_, delimiter_);
498 
499  result = map.size ();
500  }
501  else
502  {
503  // use the context's specialized mapping feature
504  std::vector<std::string> next_keys;
505  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
507  next_keys, all_record_vars, true);
508 
509  result = next_keys.size ();
510  }
511 
512  return result;
513 }
514 
515 void
517  FlexMap & other, bool refresh_keys, bool delete_keys)
518 {
519  if (context_ && other.context_)
520  {
521  ContextGuard context_guard (*context_);
522  ContextGuard other_context_guard (*other.context_);
523  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
524 
525  // use the Map container, which already has exchange implemented
527  knowledge.facade_for (*context_);
528  Map this_map (name_, knowledge, settings_, delimiter_);
529  Map other_map (other.name_, knowledge, other.settings_, delimiter_);
530 
531  this_map.exchange (other_map, refresh_keys, delete_keys);
532  }
533 }
534 
535 void
537 {
538  if (context_)
539  {
540  ContextGuard context_guard (*context_);
541  MADARA_GUARD_TYPE guard (mutex_);
542 
543  // use the Map container, which already has clear implemented
545  knowledge.facade_for (*context_);
546  Map map (name_, knowledge, settings_, delimiter_);
547 
548  map.clear ();
549 
551  }
552 }
553 
554 void
556  const std::string & key, bool delete_subkeys)
557 {
558  if (context_)
559  {
560  ContextGuard context_guard (*context_);
561  MADARA_GUARD_TYPE guard (mutex_);
562 
564 
565  if (delete_subkeys)
566  {
567  // use the Map container, which already has clear implemented
569  knowledge.facade_for (*context_);
570  Map map (name_ + delimiter_ + key, knowledge, settings_, delimiter_);
571 
572  map.clear ();
573  }
574  }
575 }
576 
577 void
579  const std::string & var_name,
581 {
582  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
583  {
584  context_ = &(knowledge.get_context ());
585 
586  ContextGuard context_guard (*context_);
587  MADARA_GUARD_TYPE guard (mutex_);
588 
589  name_ = var_name;
590 
591  if (context_->exists (var_name, settings_))
592  {
593  KnowledgeUpdateSettings keep_local (true);
594 
595  variable_ = context_->get_ref (var_name, keep_local);
596  }
597  else
598  {
599  // reset variable reference
601  }
602  }
603 }
604 
605 void
607  const std::string & var_name,
609 {
610  if (context_ != knowledge.get_context () || name_ != var_name)
611  {
612  context_ = knowledge.get_context ();
613 
614  ContextGuard context_guard (*context_);
615  MADARA_GUARD_TYPE guard (mutex_);
616 
617  name_ = var_name;
618 
619  if (context_->exists (var_name, settings_))
620  {
621  update_variable ();
622  }
623  else
624  {
625  // reset variable reference
627  }
628  }
629 }
630 
631 void
633 {
634  if (context_ && name_ != "")
635  {
636  ContextGuard context_guard (*context_);
637  MADARA_GUARD_TYPE guard (mutex_);
638 
639  KnowledgeUpdateSettings keep_local (true);
640 
641  variable_ = context_->get_ref (name_, keep_local);
642  }
643 }
644 
645 void
647 const std::string & delimiter)
648 {
649  delimiter_ = delimiter;
650 }
651 
652 
655 {
656  return delimiter_;
657 }
658 
663 bool
665 {
666  ContextGuard context_guard (*context_);
667  bool result (true);
668 
669  KnowledgeUpdateSettings keep_local (true);
670 
671  if (!variable_.is_valid ())
672  {
673  result = context_->exists (variable_, keep_local);
674  }
675 
676  return result;
677 }
678 
679 bool
681  const std::string & key, bool first_level_key) const
682 {
683  bool result (false);
684 
685  if (context_)
686  {
687  ContextGuard context_guard (*context_);
688  MADARA_GUARD_TYPE guard (mutex_);
689 
690  // check if the key exists
691  std::string exact_key = name_ + delimiter_ + key;
692  result = context_->exists (exact_key);
693 
694  if (!result && first_level_key)
695  {
696  std::vector <std::string> curkeys;
697  keys (curkeys);
698 
699  std::vector <std::string>::iterator found;
700 
701  // look for the key in the sub keys
702  found = std::find (curkeys.begin (), curkeys.end (), key);
703 
704  result = found != curkeys.end ();
705  }
706  }
707 
708  return result;
709 }
710 
711 void
713  std::vector <std::string> & curkeys,
714  bool first_level_keys_only) const
715 {
716  ContextGuard context_guard (*context_);
717  MADARA_GUARD_TYPE guard (mutex_);
718 
719  if (!first_level_keys_only)
720  {
721  // use the Map container, which already has clear implemented
723  knowledge.facade_for (*context_);
724  Map map (name_, knowledge, settings_, delimiter_);
725 
726  map.keys (curkeys);
727  }
728  else
729  {
730  // use the context's specialized mapping feature
731  std::vector<std::string> next_keys;
732  std::map<std::string, madara::knowledge::KnowledgeRecord> all_record_vars;
734  curkeys, all_record_vars, true);
735  }
736 }
737 
738 int
740  const std::string & filename)
741 {
742  int result = -1;
743 
744  if (context_ && name_ != "")
745  {
746  ContextGuard context_guard (*context_);
747  MADARA_GUARD_TYPE guard (mutex_);
748 
749  if (!variable_.is_valid ())
750  {
751  update_variable ();
752  }
753 
754  result = context_->read_file (variable_, filename, settings_);
755  }
756  return result;
757 }
758 
759 int
761 
762  const std::string & filename,
763  const KnowledgeUpdateSettings & settings)
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_->read_file (variable_, filename, settings);
778  }
779  return result;
780 }
781 
784 {
785  int result = -1;
786 
787  if (context_ && name_ != "")
788  {
789  ContextGuard context_guard (*context_);
790  MADARA_GUARD_TYPE guard (mutex_);
791 
792  if (!variable_.is_valid ())
793  {
794  update_variable ();
795  }
796 
797  result = context_->set (variable_, value, settings_);
798  }
799 
800  return result;
801 }
802 
805  const KnowledgeUpdateSettings & settings)
806 {
807  int result = -1;
808 
809  if (context_ && name_ != "")
810  {
811  ContextGuard context_guard (*context_);
812  MADARA_GUARD_TYPE guard (mutex_);
813 
814  if (!variable_.is_valid ())
815  {
816  update_variable ();
817  }
818 
819  result = context_->set (variable_, value, settings);
820  }
821 
822  return result;
823 }
824 
826 
827  size_t index,
829 {
830  int result = -1;
831 
832  if (context_ && name_ != "")
833  {
834  ContextGuard context_guard (*context_);
835  MADARA_GUARD_TYPE guard (mutex_);
836 
837  if (!variable_.is_valid ())
838  {
839  update_variable ();
840  }
841 
842  result = context_->set_index (variable_, index, value, settings_);
843  }
844 
845  return result;
846 }
847 
849 
850  size_t index,
852  const KnowledgeUpdateSettings & settings)
853 {
854  int result = -1;
855 
856  if (context_ && name_ != "")
857  {
858  ContextGuard context_guard (*context_);
859  MADARA_GUARD_TYPE guard (mutex_);
860 
861  if (!variable_.is_valid ())
862  {
863  update_variable ();
864  }
865 
866  result = context_->set_index (variable_, index, value, settings);
867  }
868 
869  return result;
870 }
871 
872 
875  uint32_t size)
876 {
877  int result = -1;
878 
879  if (context_ && name_ != "")
880  {
881  ContextGuard context_guard (*context_);
882  MADARA_GUARD_TYPE guard (mutex_);
883 
884  if (!variable_.is_valid ())
885  {
886  update_variable ();
887  }
888 
889  result = context_->set (variable_, value, size, settings_);
890  }
891 
892  return result;
893 }
894 
897  uint32_t size,
898  const KnowledgeUpdateSettings & settings)
899 {
900  int result = -1;
901 
902  if (context_ && name_ != "")
903  {
904  ContextGuard context_guard (*context_);
905  MADARA_GUARD_TYPE guard (mutex_);
906 
907  if (!variable_.is_valid ())
908  {
909  update_variable ();
910  }
911 
912  result = context_->set (variable_, value, size, settings);
913  }
914 
915  return result;
916 }
917 
919  const std::vector <KnowledgeRecord::Integer> & value)
920 {
921  int result = -1;
922 
923  if (context_ && name_ != "")
924  {
925  ContextGuard context_guard (*context_);
926  MADARA_GUARD_TYPE guard (mutex_);
927 
928  if (!variable_.is_valid ())
929  {
930  update_variable ();
931  }
932 
933  result = context_->set (variable_, value, settings_);
934  }
935 
936  return result;
937 }
938 
940  const std::vector <KnowledgeRecord::Integer> & value,
941  const KnowledgeUpdateSettings & settings)
942 {
943  int result = -1;
944 
945  if (context_ && name_ != "")
946  {
947  ContextGuard context_guard (*context_);
948  MADARA_GUARD_TYPE guard (mutex_);
949 
950  if (!variable_.is_valid ())
951  {
952  update_variable ();
953  }
954 
955  result = context_->set (variable_, value, settings);
956  }
957 
958  return result;
959 }
960 
962  double value)
963 {
964  int result = -1;
965 
966  if (context_ && name_ != "")
967  {
968  ContextGuard context_guard (*context_);
969  MADARA_GUARD_TYPE guard (mutex_);
970 
971  if (!variable_.is_valid ())
972  {
973  update_variable ();
974  }
975 
976  result = context_->set (variable_, value, settings_);
977  }
978 
979  return result;
980 }
981 
983  double value,
984  const KnowledgeUpdateSettings & settings)
985 {
986  int result = -1;
987 
988  if (context_ && name_ != "")
989  {
990  ContextGuard context_guard (*context_);
991  MADARA_GUARD_TYPE guard (mutex_);
992 
993  if (!variable_.is_valid ())
994  {
995  update_variable ();
996  }
997 
998  result = context_->set (variable_, value, settings);
999  }
1000 
1001  return result;
1002 }
1003 
1005  size_t index,
1006  double value)
1007 {
1008  int result = -1;
1009 
1010  if (context_ && name_ != "")
1011  {
1012  ContextGuard context_guard (*context_);
1013  MADARA_GUARD_TYPE guard (mutex_);
1014 
1015  if (!variable_.is_valid ())
1016  {
1017  update_variable ();
1018  }
1019 
1020  result = context_->set_index (variable_, index, value, settings_);
1021  }
1022 
1023  return result;
1024 }
1025 
1027  size_t index,
1028  double value,
1029  const KnowledgeUpdateSettings & settings)
1030 {
1031  int result = -1;
1032 
1033  if (context_ && name_ != "")
1034  {
1035  ContextGuard context_guard (*context_);
1036  MADARA_GUARD_TYPE guard (mutex_);
1037 
1038  if (!variable_.is_valid ())
1039  {
1040  update_variable ();
1041  }
1042 
1043  result = context_->set_index (variable_, index, value, settings);
1044  }
1045 
1046  return result;
1047 }
1048 
1050  const double * value,
1051  uint32_t size)
1052 {
1053  int result = -1;
1054 
1055  if (context_ && name_ != "")
1056  {
1057  ContextGuard context_guard (*context_);
1058  MADARA_GUARD_TYPE guard (mutex_);
1059 
1060  if (!variable_.is_valid ())
1061  {
1062  update_variable ();
1063  }
1064 
1065  result = context_->set (variable_, value, size, settings_);
1066  }
1067 
1068  return result;
1069 }
1070 
1072  const double * value,
1073  uint32_t size,
1074  const KnowledgeUpdateSettings & settings)
1075 {
1076  int result = -1;
1077 
1078  if (context_ && name_ != "")
1079  {
1080  ContextGuard context_guard (*context_);
1081  MADARA_GUARD_TYPE guard (mutex_);
1082 
1083  if (!variable_.is_valid ())
1084  {
1085  update_variable ();
1086  }
1087 
1088  result = context_->set (variable_, value, size, settings);
1089  }
1090 
1091  return result;
1092 }
1093 
1095  const std::vector <double> & value)
1096 {
1097  int result = -1;
1098 
1099  if (context_ && name_ != "")
1100  {
1101  ContextGuard context_guard (*context_);
1102  MADARA_GUARD_TYPE guard (mutex_);
1103 
1104  if (!variable_.is_valid ())
1105  {
1106  update_variable ();
1107  }
1108 
1109  result = context_->set (variable_, value, settings_);
1110  }
1111 
1112  return result;
1113 }
1114 
1116  const std::vector <double> & value,
1117  const KnowledgeUpdateSettings & settings)
1118 {
1119  int result = -1;
1120 
1121  if (context_ && name_ != "")
1122  {
1123  ContextGuard context_guard (*context_);
1124  MADARA_GUARD_TYPE guard (mutex_);
1125 
1126  if (!variable_.is_valid ())
1127  {
1128  update_variable ();
1129  }
1130 
1131  result = context_->set (variable_, value, settings);
1132  }
1133 
1134  return result;
1135 }
1136 
1138  const std::string & value)
1139 {
1140  int result = -1;
1141 
1142  if (context_ && name_ != "")
1143  {
1144  ContextGuard context_guard (*context_);
1145  MADARA_GUARD_TYPE guard (mutex_);
1146 
1147  if (!variable_.is_valid ())
1148  {
1149  update_variable ();
1150  }
1151 
1152  result = context_->set (variable_, value, settings_);
1153  }
1154 
1155  return result;
1156 }
1157 
1159  const std::string & value,
1160  const KnowledgeUpdateSettings & settings)
1161 {
1162  int result = -1;
1163 
1164  if (context_ && name_ != "")
1165  {
1166  ContextGuard context_guard (*context_);
1167  MADARA_GUARD_TYPE guard (mutex_);
1168 
1169  if (!variable_.is_valid ())
1170  {
1171  update_variable ();
1172  }
1173 
1174  result = context_->set (variable_, value, settings);
1175  }
1176 
1177  return result;
1178 }
1179 
1181  const unsigned char * value, size_t size)
1182 {
1183  int result = -1;
1184 
1185  if (context_ && name_ != "")
1186  {
1187  ContextGuard context_guard (*context_);
1188  MADARA_GUARD_TYPE guard (mutex_);
1189 
1190  if (!variable_.is_valid ())
1191  {
1192  update_variable ();
1193  }
1194 
1195  result = context_->set_file (variable_, value, size, settings_);
1196  }
1197 
1198  return result;
1199 }
1200 
1202  const unsigned char * value, size_t size,
1203  const KnowledgeUpdateSettings & settings)
1204 {
1205  int result = -1;
1206 
1207  if (context_ && name_ != "")
1208  {
1209  ContextGuard context_guard (*context_);
1210  MADARA_GUARD_TYPE guard (mutex_);
1211 
1212  if (!variable_.is_valid ())
1213  {
1214  update_variable ();
1215  }
1216 
1217  result = context_->set_file (variable_, value, size, settings);
1218  }
1219 
1220  return result;
1221 }
1222 
1224  const unsigned char * value, size_t size)
1225 {
1226  int result = -1;
1227 
1228  if (context_ && name_ != "")
1229  {
1230  ContextGuard context_guard (*context_);
1231  MADARA_GUARD_TYPE guard (mutex_);
1232 
1233  if (!variable_.is_valid ())
1234  {
1235  update_variable ();
1236  }
1237 
1238  result = context_->set_jpeg (variable_, value, size, settings_);
1239  }
1240 
1241  return result;
1242 }
1243 
1245  const unsigned char * value, size_t size,
1246  const KnowledgeUpdateSettings & settings)
1247 {
1248  int result = -1;
1249 
1250  if (context_ && name_ != "")
1251  {
1252  ContextGuard context_guard (*context_);
1253  MADARA_GUARD_TYPE guard (mutex_);
1254 
1255  if (!variable_.is_valid ())
1256  {
1257  update_variable ();
1258  }
1259 
1260  result = context_->set_jpeg (variable_, value, size, settings);
1261  }
1262 
1263  return result;
1264 }
1265 
1266 void
1268  uint32_t quality,
1269  const KnowledgeReferenceSettings & settings)
1270 {
1271  if (context_)
1272  {
1273  ContextGuard context_guard (*context_);
1274  MADARA_GUARD_TYPE guard (mutex_);
1275 
1276  if (!variable_.is_valid ())
1277  {
1278  update_variable ();
1279  }
1280 
1281  context_->set_quality (name_, quality, true, settings);
1282  }
1283 }
1284 
1285 bool
1287 {
1288  return is_true ();
1289 }
1290 
1291 bool
1293 {
1294  return is_false ();
1295 }
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
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.
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)
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:512
std::string to_string(void) const
Returns the value at the location as a string.
Definition: FlexMap.cpp:252
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:442
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:825
int set(knowledge::KnowledgeRecord::Integer value=knowledge::KnowledgeRecord::MODIFIED)
Sets the location within the map to the specified value.
Definition: FlexMap.cpp:782
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:686
MADARA_LOCK_TYPE mutex_
guard for access and changes
void update_variable(void) const
Updates the variable reference if necessary.
Definition: FlexMap.cpp:632
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:453
void erase(const std::string &key, bool delete_subkeys=true)
Erases a variable from the map.
Definition: FlexMap.cpp:555
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:259
virtual BaseContainer * clone(void) const
Clones this container.
Definition: FlexMap.cpp:152
int set_file(const unsigned char *value, size_t size)
Atomically sets the value of an index to an arbitrary string.
Definition: FlexMap.cpp:1180
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:664
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:44
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
This class stores an integer within a variable context.
Definition: Integer.h:31
void clear(void)
Clears the map.
Definition: FlexMap.cpp:536
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
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:516
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:646
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: FlexMap.h:696
virtual ~FlexMap()
Destructor.
Definition: FlexMap.cpp:43
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:372
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: FlexMap.cpp:578
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value at the location as an integer.
Definition: FlexMap.cpp:240
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:1223
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.
int read_file(const std::string &filename)
Read a file into a location in the map.
Definition: FlexMap.cpp:739
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
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
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.
knowledge::KnowledgeRecord to_record(void) const
Retrieves a copy of the record from the current location.
Definition: FlexMap.cpp:217
VariableReference variable_
The current location variable.
Definition: FlexMap.h:691
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:484
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:1292
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:1286
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
Definition: FlexMap.cpp:654
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:143
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
double to_double(void) const
Returns the value at the location as a double.
Definition: FlexMap.cpp:246
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:143
void keys(std::vector< std::string > &curkeys, bool first_level_keys_only=true) const
Returns the keys within the map.
Definition: FlexMap.cpp:712
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:1267