2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef _ABSTRACTPROPERTYTYPE_H_
20 #define _ABSTRACTPROPERTYTYPE_H_
27 #include <boost/any.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/utility.hpp>
30 #include <type_traits>
33 #include "timestamp.h"
35 #include <boost/algorithm/string.hpp>
49 class AbstractPropertyType
52 //AbstractPropertyType(): timestamp(-1), sequence(-1), zone(Zone::None), index(0) {}
54 AbstractPropertyType(std::string property): name(property), timestamp(-1), sequence(-1), zone(Zone::None), index(0) {}
56 virtual std::string toString() const = 0;
58 virtual void fromString(std::string)= 0;
60 virtual GVariant* toVariant() = 0;
62 virtual void fromVariant(GVariant*) = 0;
64 virtual AbstractPropertyType* copy() = 0;
66 bool operator == (AbstractPropertyType &other)
68 std::string one = toString();
69 std::string two = other.toString();
73 bool operator != (AbstractPropertyType &other)
75 std::string one = toString();
76 std::string two = other.toString();
86 std::string sourceUuid;
92 void setValue(boost::any val)
95 timestamp = amb::currentTime();
101 return boost::any_cast<T>(mValue);
104 boost::any anyValue()
115 template <typename T>
122 static const char* signature() { return "i"; }
124 static char value(GVariant* v)
126 return g_variant_get_int32(v);
129 static std::string stringize(std::string v)
139 static const char* signature() { return "d"; }
141 static double value(GVariant* v)
143 return g_variant_get_double(v);
145 static std::string stringize(std::string v)
155 static const char* signature() { return "q"; }
157 static uint16_t value(GVariant* v)
159 return g_variant_get_uint16(v);
161 static std::string stringize(std::string v)
171 static const char* signature() { return "n"; }
173 static int16_t value(GVariant* v)
175 return g_variant_get_int16(v);
177 static std::string stringize(std::string v)
187 static const char* signature() { return "y"; }
189 static char value(GVariant* v)
191 return g_variant_get_byte(v);
193 static std::string stringize(std::string v)
203 static const char* signature() { return "u"; }
205 static uint32_t value(GVariant* v)
207 return g_variant_get_uint32(v);
209 static std::string stringize(std::string v)
219 static const char* signature() { return "x"; }
221 static int64_t value(GVariant* v)
223 return g_variant_get_int64(v);
225 static std::string stringize(std::string v)
235 static const char* signature() { return "t"; }
237 static uint64_t value(GVariant* v)
239 g_variant_get_uint64(v);
241 static std::string stringize(std::string v)
251 static const char* signature() { return "b"; }
253 static bool value(GVariant *v)
255 return g_variant_get_boolean(v);
257 static std::string stringize(std::string v)
259 boost::algorithm::to_lower(v);
260 return v == "true" ? "1":"0";
265 template <typename T>
266 class BasicPropertyType: public AbstractPropertyType
269 BasicPropertyType(): AbstractPropertyType("")
273 BasicPropertyType(BasicPropertyType const &other)
274 :AbstractPropertyType(other.name)
276 setValue(other.value<T>());
277 timestamp = other.timestamp;
278 sequence = other.sequence;
279 sourceUuid = other.sourceUuid;
284 BasicPropertyType & operator = (BasicPropertyType const & other)
286 setValue(other.value<T>());
287 timestamp = other.timestamp;
288 sequence = other.sequence;
289 sourceUuid = other.sourceUuid;
296 bool operator < (const BasicPropertyType<T>& other) const
298 return value<T>() < other.value<T>();
301 bool operator > (const BasicPropertyType<T>& other) const
303 return value<T>() > other.value<T>();
306 BasicPropertyType( T val)
307 :AbstractPropertyType("")
312 BasicPropertyType( std::string propertyName, T val)
313 :AbstractPropertyType(propertyName)
318 BasicPropertyType( std::string propertyName, std::string val)
319 :AbstractPropertyType(propertyName)
321 if(!val.empty() && val != "")
328 BasicPropertyType(std::string val)
329 :AbstractPropertyType("")
331 if(!val.empty() && val != "")
338 AbstractPropertyType* copy()
340 return new BasicPropertyType<T>(*this);
343 void fromString(std::string val)
345 if(!val.empty() && val != "")
351 std::string toString() const
353 std::stringstream stream;
354 stream.precision(10);
360 GVariant* toVariant()
362 return serializeVariant<T>(value<T>());
365 void fromVariant(GVariant *v)
367 setValue(deserializeVariant<T>(v));
372 //GVariant* mVariant;
375 void serialize(std::string val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
379 std::stringstream stream(val);
382 setValue((N)someTemp);
386 void serialize(std::string val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
388 std::stringstream stream(GVS<T>::stringize(val));
395 GVariant* serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
397 //mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
399 return (g_variant_new("i",(int)val));
403 GVariant* serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
405 //mVariant = Glib::Variant<T>::create(val);
406 //mVariant = g_variant_ref(g_variant_new(GVS<T>::signature(),val));
407 return g_variant_new(GVS<T>::signature(),val);
411 T deserializeVariant(GVariant* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
413 // return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
415 return (T)GVS<int>::value(v);
419 T deserializeVariant(GVariant* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
421 // return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
422 return GVS<T>::value(v);
426 class StringPropertyType: public AbstractPropertyType
429 StringPropertyType(std::string propertyName)
430 :AbstractPropertyType(propertyName)
435 StringPropertyType(std::string propertyName, std::string val)
436 :AbstractPropertyType(propertyName)
441 StringPropertyType(StringPropertyType const & other)
442 :AbstractPropertyType(other.name)
444 setValue(other.value<std::string>());
445 timestamp = other.timestamp;
446 sequence = other.sequence;
447 sourceUuid = other.sourceUuid;
452 StringPropertyType & operator = (StringPropertyType const & other)
454 setValue(other.value<std::string>());
455 timestamp = other.timestamp;
456 sequence = other.sequence;
457 sourceUuid = other.sourceUuid;
464 void fromString(std::string val)
469 AbstractPropertyType* copy()
471 return new StringPropertyType(*this);
474 std::string toString() const
476 return value<std::string>();
479 GVariant* toVariant()
481 //mVariant = Glib::Variant<std::string>::create(toString());
483 return g_variant_new_string(toString().c_str());
487 void fromVariant(GVariant *v)
489 setValue(std::string(g_variant_get_string(v,NULL)));
494 class ListPropertyType: public AbstractPropertyType
498 ListPropertyType(std::string propertyName)
499 : AbstractPropertyType(propertyName), initialized(false)
504 ListPropertyType(std::string propertyName, AbstractPropertyType *value)
505 : AbstractPropertyType(propertyName), initialized(false)
507 appendPriv(value->copy());
510 ListPropertyType(ListPropertyType & other)
511 :AbstractPropertyType(other.name),initialized(false)
513 std::list<AbstractPropertyType*> l = other.list();
514 for(auto itr = l.begin(); itr != l.end(); itr++)
519 timestamp = other.timestamp;
520 sequence = other.sequence;
521 sourceUuid = other.sourceUuid;
528 for(auto itr = mList.begin(); itr != mList.end(); itr++)
534 /** append - appends a property to the list
535 * @arg property - property to be appended. Property will be copied and owned by ListPropertyType.
536 * You are responsible for freeing property after append is called.
538 void append(AbstractPropertyType* property)
542 for(auto itr = mList.begin(); itr != mList.end(); itr++)
544 AbstractPropertyType *p = *itr;
551 appendPriv(property->copy());
559 AbstractPropertyType* copy()
561 return new ListPropertyType(*this);
564 std::string toString() const
566 std::string str = "[";
568 for(auto itr = mList.begin(); itr != mList.end(); itr++)
573 AbstractPropertyType* t = *itr;
575 str += t->toString();
584 void fromString(std::string str )
589 if(str[0] != '[' && str[str.length()-1] != ']')
594 str = str.substr(1,str.length() - 2);
596 std::vector<std::string> elements;
598 std::istringstream f(str);
601 while(std::getline(f,element,','))
603 T *foo = new T(element);
611 GVariant* toVariant()
614 GVariantBuilder params;
615 g_variant_builder_init(¶ms, ((const GVariantType *) "av"));
617 for(auto itr = mList.begin(); itr != mList.end(); itr++)
619 AbstractPropertyType* t = *itr;
620 GVariant *var = t->toVariant();
621 GVariant *newvar = g_variant_new("v",var);
622 g_variant_builder_add_value(¶ms, newvar);
626 GVariant* var = g_variant_builder_end(¶ms);
632 void fromVariant(GVariant* v)
634 /// TODO: fill this in
635 gsize dictsize = g_variant_n_children(v);
636 for (int i=0;i<dictsize;i++)
638 GVariant *childvariant = g_variant_get_child_value(v,i);
639 GVariant *innervariant = g_variant_get_variant(childvariant);
641 t->fromVariant(innervariant);
646 std::list<AbstractPropertyType*> list() { return mList; }
649 void appendPriv(AbstractPropertyType* i)
656 std::list<AbstractPropertyType*> mList;