1 #ifndef _MAPPROPERTYTYPE_H_
2 #define _MAPPROPERTYTYPE_H_
5 #include "abstractpropertytype.h"
11 template <class T, class N>
12 class MapPropertyType: public AbstractPropertyType
15 MapPropertyType(std::string propertyName):AbstractPropertyType(propertyName){}
17 void append(T key, N value)
19 appendPriv(key,value);
22 AbstractPropertyType* copy()
24 MapPropertyType<T,N> *t = new MapPropertyType<T,N>(name);
27 t->timestamp = timestamp;
28 t->sequence = sequence;
29 t->sourceUuid = sourceUuid;
36 std::string toString() const
38 std::stringstream str;
42 for(auto itr = mMap.begin(); itr != mMap.end(); itr++)
49 str <<"'"<< t.first.toString() <<"':'"<<t.second.toString()<<"'";
57 void fromString(std::string str)
59 json_object *rootobject;
60 json_tokener *tokener = json_tokener_new();
61 enum json_tokener_error err;
64 rootobject = json_tokener_parse_ex(tokener, str.c_str(),str.length());
65 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
66 if (err != json_tokener_success)
68 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err));
69 // Handle errors, as appropriate for your application.
71 if (tokener->char_offset < str.length()) // XXX shouldn't access internal fields
73 // Handle extra characters after parsed object as desired.
74 // e.g. issue an error, parse another object from that point, etc...
78 json_object_object_foreach(rootobject, key, val)
81 N two(json_object_get_string(val));
85 json_object_put(rootobject);
91 GVariantBuilder params;
92 g_variant_builder_init(¶ms, G_VARIANT_TYPE_DICTIONARY);
93 for(auto itr = mMap.begin(); itr != mMap.end(); itr++)
95 auto &foo = (*itr).first;
96 g_variant_builder_add(¶ms,"{?*}",const_cast<T&>(foo).toVariant(),(*itr).second.toVariant());
99 GVariant* var = g_variant_builder_end(¶ms);
104 void fromVariant(GVariant* variant)
106 /// TODO: fill this in
107 gsize dictsize = g_variant_n_children(variant);
108 for (int i=0;i<dictsize;i++)
110 GVariant *childvariant = g_variant_get_child_value(variant,i);
111 gsize dictvalsize = g_variant_n_children(childvariant);
112 if (dictvalsize == 2)
114 //It is a dictionary entry
115 GVariant *keyvariant = g_variant_get_child_value(childvariant,0);
116 GVariant *valvariant = g_variant_get_child_value(childvariant,1);
118 t.fromVariant(keyvariant);
120 n.fromVariant(valvariant);
127 void setMap(std::map<T, N> m)
134 void appendPriv(T key, N value)