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 Zone::Type FrontRight = Zone::Type(Front | Right);
50 Zone::Type FrontLeft = Zone::Type(Front | Left);
51 Zone::Type MiddleRight = Zone::Type(Middle | Right);
52 Zone::Type MiddleLeft = Zone::Type(Middle | Left);
54 typedef std::list<Zone::Type> ZoneList;
58 class AbstractPropertyType
61 AbstractPropertyType(std::string property): name(property), timestamp(-1), sequence(-1), zone(Zone::None) {}
63 virtual ~AbstractPropertyType() { }
65 virtual std::string toString() const = 0;
67 virtual void fromString(std::string)= 0;
69 virtual GVariant* toVariant() = 0;
71 virtual void fromVariant(GVariant*) = 0;
73 virtual AbstractPropertyType* copy() = 0;
75 bool operator == (AbstractPropertyType &other)
77 std::string one = toString();
78 std::string two = other.toString();
82 bool operator != (AbstractPropertyType &other)
84 std::string one = toString();
85 std::string two = other.toString();
95 std::string sourceUuid;
99 void setValue(boost::any val)
102 timestamp = amb::currentTime();
105 template <typename T>
108 return boost::any_cast<T>(mValue);
111 boost::any anyValue()
122 template <typename T>
129 static const char* signature() { return "i"; }
131 static char value(GVariant* v)
133 return g_variant_get_int32(v);
136 static std::string stringize(std::string v)
146 static const char* signature() { return "d"; }
148 static double value(GVariant* v)
150 return g_variant_get_double(v);
152 static std::string stringize(std::string v)
162 static const char* signature() { return "q"; }
164 static uint16_t value(GVariant* v)
166 return g_variant_get_uint16(v);
168 static std::string stringize(std::string v)
178 static const char* signature() { return "n"; }
180 static int16_t value(GVariant* v)
182 return g_variant_get_int16(v);
184 static std::string stringize(std::string v)
194 static const char* signature() { return "y"; }
196 static char value(GVariant* v)
198 return g_variant_get_byte(v);
200 static std::string stringize(std::string v)
210 static const char* signature() { return "u"; }
212 static uint32_t value(GVariant* v)
214 return g_variant_get_uint32(v);
216 static std::string stringize(std::string v)
226 static const char* signature() { return "x"; }
228 static int64_t value(GVariant* v)
230 return g_variant_get_int64(v);
232 static std::string stringize(std::string v)
242 static const char* signature() { return "t"; }
244 static uint64_t value(GVariant* v)
246 return g_variant_get_uint64(v);
248 static std::string stringize(std::string v)
258 static const char* signature() { return "b"; }
260 static bool value(GVariant *v)
262 return g_variant_get_boolean(v);
264 static std::string stringize(std::string v)
266 boost::algorithm::to_lower(v);
267 return v == "true" ? "1":"0";
272 template <typename T>
273 class BasicPropertyType: public AbstractPropertyType
276 BasicPropertyType(): AbstractPropertyType("")
280 BasicPropertyType(BasicPropertyType const &other)
281 :AbstractPropertyType(other.name)
283 setValue(other.value<T>());
284 timestamp = other.timestamp;
285 sequence = other.sequence;
286 sourceUuid = other.sourceUuid;
292 BasicPropertyType & operator = (BasicPropertyType const & other)
294 setValue(other.value<T>());
295 timestamp = other.timestamp;
296 sequence = other.sequence;
297 sourceUuid = other.sourceUuid;
304 bool operator < (const BasicPropertyType<T>& other) const
306 return value<T>() < other.value<T>();
309 bool operator > (const BasicPropertyType<T>& other) const
311 return value<T>() > other.value<T>();
314 BasicPropertyType( T val)
315 :AbstractPropertyType("")
320 BasicPropertyType( std::string propertyName, T val)
321 :AbstractPropertyType(propertyName)
326 BasicPropertyType( std::string propertyName, std::string val)
327 :AbstractPropertyType(propertyName)
329 if(!val.empty() && val != "")
336 BasicPropertyType(std::string val)
337 :AbstractPropertyType("")
339 if(!val.empty() && val != "")
346 AbstractPropertyType* copy()
348 return new BasicPropertyType<T>(*this);
351 void fromString(std::string val)
353 if(!val.empty() && val != "")
359 std::string toString() const
361 std::stringstream stream;
362 stream.precision(10);
368 GVariant* toVariant()
370 return serializeVariant<T>(value<T>());
373 void fromVariant(GVariant *v)
375 setValue(deserializeVariant<T>(v));
380 //GVariant* mVariant;
383 void serialize(std::string val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
387 std::stringstream stream(val);
390 setValue((N)someTemp);
394 void serialize(std::string val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
396 std::stringstream stream(GVS<T>::stringize(val));
403 GVariant* serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
405 //mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
407 return (g_variant_new("i",(int)val));
411 GVariant* serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
413 //mVariant = Glib::Variant<T>::create(val);
414 //mVariant = g_variant_ref(g_variant_new(GVS<T>::signature(),val));
415 return g_variant_new(GVS<T>::signature(),val);
419 T deserializeVariant(GVariant* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
421 // return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
423 return (T)GVS<int>::value(v);
427 T deserializeVariant(GVariant* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
429 // return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
430 return GVS<T>::value(v);
434 class StringPropertyType: public AbstractPropertyType
437 StringPropertyType(std::string propertyName)
438 :AbstractPropertyType(propertyName)
443 StringPropertyType(std::string propertyName, std::string val)
444 :AbstractPropertyType(propertyName)
449 StringPropertyType(StringPropertyType const & other)
450 :AbstractPropertyType(other.name)
452 setValue(other.value<std::string>());
453 timestamp = other.timestamp;
454 sequence = other.sequence;
455 sourceUuid = other.sourceUuid;
460 StringPropertyType & operator = (StringPropertyType const & other)
462 setValue(other.value<std::string>());
463 timestamp = other.timestamp;
464 sequence = other.sequence;
465 sourceUuid = other.sourceUuid;
472 void fromString(std::string val)
477 AbstractPropertyType* copy()
479 return new StringPropertyType(*this);
482 std::string toString() const
484 return value<std::string>();
487 GVariant* toVariant()
489 //mVariant = Glib::Variant<std::string>::create(toString());
491 return g_variant_new_string(toString().c_str());
495 void fromVariant(GVariant *v)
497 setValue(std::string(g_variant_get_string(v,NULL)));
502 class ListPropertyType: public AbstractPropertyType
506 ListPropertyType(std::string propertyName)
507 : AbstractPropertyType(propertyName), initialized(false)
512 ListPropertyType(std::string propertyName, AbstractPropertyType *value)
513 : AbstractPropertyType(propertyName), initialized(false)
515 appendPriv(value->copy());
518 ListPropertyType(ListPropertyType & other)
519 :AbstractPropertyType(other.name),initialized(false)
521 std::list<AbstractPropertyType*> l = other.list();
522 for(auto itr = l.begin(); itr != l.end(); itr++)
527 timestamp = other.timestamp;
528 sequence = other.sequence;
529 sourceUuid = other.sourceUuid;
536 for(auto itr = mList.begin(); itr != mList.end(); itr++)
542 /** append - appends a property to the list
543 * @arg property - property to be appended. Property will be copied and owned by ListPropertyType.
544 * You are responsible for freeing property after append is called.
546 void append(AbstractPropertyType* property)
550 for(auto itr = mList.begin(); itr != mList.end(); itr++)
552 AbstractPropertyType *p = *itr;
559 appendPriv(property->copy());
567 AbstractPropertyType* copy()
569 return new ListPropertyType(*this);
572 std::string toString() const
574 std::string str = "[";
576 for(auto itr = mList.begin(); itr != mList.end(); itr++)
581 AbstractPropertyType* t = *itr;
583 str += t->toString();
592 void fromString(std::string str )
597 if(str[0] != '[' && str[str.length()-1] != ']')
602 str = str.substr(1,str.length() - 2);
604 std::vector<std::string> elements;
606 std::istringstream f(str);
609 while(std::getline(f,element,','))
611 T *foo = new T(element);
619 GVariant* toVariant()
622 GVariantBuilder params;
623 g_variant_builder_init(¶ms, ((const GVariantType *) "av"));
625 for(auto itr = mList.begin(); itr != mList.end(); itr++)
627 AbstractPropertyType* t = *itr;
628 GVariant *var = t->toVariant();
629 GVariant *newvar = g_variant_new("v",var);
630 g_variant_builder_add_value(¶ms, newvar);
634 GVariant* var = g_variant_builder_end(¶ms);
640 void fromVariant(GVariant* v)
642 /// TODO: fill this in
643 gsize dictsize = g_variant_n_children(v);
644 for (int i=0;i<dictsize;i++)
646 GVariant *childvariant = g_variant_get_child_value(v,i);
647 GVariant *innervariant = g_variant_get_variant(childvariant);
649 t->fromVariant(innervariant);
654 std::list<AbstractPropertyType*> list() { return mList; }
657 void appendPriv(AbstractPropertyType* i)
664 std::list<AbstractPropertyType*> mList;