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>
57 static const Zone::Type FrontRight;
58 static const Zone::Type FrontLeft;
59 static const Zone::Type MiddleRight;
60 static const Zone::Type MiddleLeft;
61 static const Zone::Type RearRight;
62 static const Zone::Type RearLeft;
64 typedef std::list<Zone::Type> ZoneList;
68 class AbstractPropertyType
73 * \brief The Priority enum describes prority of the property type.
83 AbstractPropertyType(std::string property): name(property), timestamp(amb::currentTime()), sequence(-1), zone(Zone::None), priority(Normal)
88 virtual ~AbstractPropertyType() { }
90 virtual std::string toString() const = 0;
92 virtual void fromString(std::string)= 0;
94 virtual GVariant* toVariant() = 0;
96 virtual void fromVariant(GVariant*) = 0;
98 virtual AbstractPropertyType* copy() = 0;
101 * @brief quickCopy is intended as a way to quickly copy the often changing bits from one abstract property to another
102 * It assumes that the properties are almost identical in name, source, and zone.
103 * @param other the property to copy from
105 virtual void quickCopy(AbstractPropertyType* other)
107 sequence = other->sequence;
108 mValue = other->anyValue();
109 timestamp = other->timestamp;
112 bool operator == (AbstractPropertyType &other)
114 std::string one = toString();
115 std::string two = other.toString();
117 && zone == other.zone
118 && sourceUuid == other.sourceUuid
119 && name == other.name;
122 bool operator != (AbstractPropertyType &other)
124 std::string one = toString();
125 std::string two = other.toString();
135 std::string sourceUuid;
140 * \brief priority is used to tell the routing engine how to prioritize routing the value to plugins.
141 * setting this value to AbstractPropertyType::Instant will tell the routing engine to immedietly
142 * route the value without any reliance on the mainloop. Instant priority is NOT thread safe.
143 * Default priority is AbstractPropertyType::Normal.
147 void setValue(boost::any val)
150 timestamp = amb::currentTime();
153 template <typename T>
156 return boost::any_cast<T>(mValue);
159 boost::any anyValue()
170 template <typename T>
177 static const char* signature() { return "i"; }
179 static int value(GVariant* v)
181 return g_variant_get_int32(v);
184 static std::string stringize(std::string v)
194 static const char* signature() { return "d"; }
196 static double value(GVariant* v)
198 return g_variant_get_double(v);
200 static std::string stringize(std::string v)
210 static const char* signature() { return "q"; }
212 static uint16_t value(GVariant* v)
214 return g_variant_get_uint16(v);
216 static std::string stringize(std::string v)
226 static const char* signature() { return "n"; }
228 static int16_t value(GVariant* v)
230 return g_variant_get_int16(v);
232 static std::string stringize(std::string v)
242 static const char* signature() { return "y"; }
244 static char value(GVariant* v)
246 return g_variant_get_byte(v);
248 static std::string stringize(std::string v)
258 static const char* signature() { return "u"; }
260 static uint32_t value(GVariant* v)
262 return g_variant_get_uint32(v);
264 static std::string stringize(std::string v)
274 static const char* signature() { return "x"; }
276 static int64_t value(GVariant* v)
278 return g_variant_get_int64(v);
280 static std::string stringize(std::string v)
290 static const char* signature() { return "t"; }
292 static uint64_t value(GVariant* v)
294 return g_variant_get_uint64(v);
296 static std::string stringize(std::string v)
306 static const char* signature() { return "b"; }
308 static bool value(GVariant *v)
310 return g_variant_get_boolean(v);
312 static std::string stringize(std::string v)
314 if(v == "0" || v == "1")
317 boost::algorithm::to_lower(v);
318 return v == "true" ? "1":"0";
323 template <typename T>
324 class BasicPropertyType: public AbstractPropertyType
327 BasicPropertyType(): AbstractPropertyType("")
331 BasicPropertyType(BasicPropertyType const &other)
332 :AbstractPropertyType(other.name)
334 setValue(other.value<T>());
335 timestamp = other.timestamp;
336 sequence = other.sequence;
337 sourceUuid = other.sourceUuid;
343 BasicPropertyType & operator = (BasicPropertyType const & other)
345 setValue(other.value<T>());
346 timestamp = other.timestamp;
347 sequence = other.sequence;
348 sourceUuid = other.sourceUuid;
355 bool operator < (const BasicPropertyType<T>& other) const
357 return value<T>() < other.value<T>();
360 bool operator > (const BasicPropertyType<T>& other) const
362 return value<T>() > other.value<T>();
365 BasicPropertyType( T val)
366 :AbstractPropertyType("")
371 BasicPropertyType( std::string propertyName, T val)
372 :AbstractPropertyType(propertyName)
377 BasicPropertyType( std::string propertyName, std::string val)
378 :AbstractPropertyType(propertyName)
380 if(!val.empty() && val != "")
387 BasicPropertyType(std::string propertyName)
388 :AbstractPropertyType(propertyName)
393 /*BasicPropertyType(std::string val)
394 :AbstractPropertyType("")
396 if(!val.empty() && val != "")
403 AbstractPropertyType* copy()
405 return new BasicPropertyType<T>(*this);
408 void fromString(std::string val)
410 if(!val.empty() && val != "")
416 std::string toString() const
418 std::stringstream stream;
419 stream.precision(10);
425 GVariant* toVariant()
427 return serializeVariant<T>(value<T>());
430 void fromVariant(GVariant *v)
432 setValue(deserializeVariant<T>(v));
437 //GVariant* mVariant;
440 void serialize(std::string val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
444 std::stringstream stream(val);
447 setValue((N)someTemp);
451 void serialize(std::string val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
453 std::stringstream stream(GVS<T>::stringize(val));
460 GVariant* serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
462 //mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
464 return (g_variant_new("i",(int)val));
468 GVariant* serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
470 //mVariant = Glib::Variant<T>::create(val);
471 //mVariant = g_variant_ref(g_variant_new(GVS<T>::signature(),val));
472 return g_variant_new(GVS<T>::signature(),val);
476 T deserializeVariant(GVariant* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
478 // return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
480 return (T)GVS<int>::value(v);
484 T deserializeVariant(GVariant* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
486 // return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
487 return GVS<T>::value(v);
491 class StringPropertyType: public AbstractPropertyType
494 StringPropertyType(std::string propertyName)
495 :AbstractPropertyType(propertyName)
497 setValue(std::string());
500 StringPropertyType(std::string propertyName, std::string val)
501 :AbstractPropertyType(propertyName)
506 StringPropertyType(StringPropertyType const & other)
507 :AbstractPropertyType(other.name)
509 setValue(other.value<std::string>());
510 timestamp = other.timestamp;
511 sequence = other.sequence;
512 sourceUuid = other.sourceUuid;
517 StringPropertyType & operator = (StringPropertyType const & other)
519 setValue(other.value<std::string>());
520 timestamp = other.timestamp;
521 sequence = other.sequence;
522 sourceUuid = other.sourceUuid;
529 void fromString(std::string val)
534 AbstractPropertyType* copy()
536 return new StringPropertyType(*this);
539 std::string toString() const
541 return value<std::string>();
544 GVariant* toVariant()
546 //mVariant = Glib::Variant<std::string>::create(toString());
548 return g_variant_new_string(toString().c_str());
552 void fromVariant(GVariant *v)
554 setValue(std::string(g_variant_get_string(v,NULL)));
559 class ListPropertyType: public AbstractPropertyType
563 ListPropertyType(std::string propertyName)
564 : AbstractPropertyType(propertyName), initialized(false)
569 ListPropertyType(std::string propertyName, AbstractPropertyType *value)
570 : AbstractPropertyType(propertyName), initialized(false)
572 appendPriv(value->copy());
575 ListPropertyType(ListPropertyType & other)
576 :AbstractPropertyType(other.name),initialized(false)
578 std::list<AbstractPropertyType*> l = other.list();
579 for(auto itr = l.begin(); itr != l.end(); itr++)
584 timestamp = other.timestamp;
585 sequence = other.sequence;
586 sourceUuid = other.sourceUuid;
596 /** append - appends a property to the list
597 * @arg property - property to be appended. Property will be copied and owned by ListPropertyType.
598 * You are responsible for freeing property after append is called.
600 void append(AbstractPropertyType* property)
604 for(auto itr = mList.begin(); itr != mList.end(); itr++)
606 AbstractPropertyType *p = *itr;
613 appendPriv(property->copy());
621 AbstractPropertyType* copy()
623 return new ListPropertyType(*this);
626 std::string toString() const
628 std::string str = "[";
630 for(auto itr = mList.begin(); itr != mList.end(); itr++)
635 AbstractPropertyType* t = *itr;
637 str += t->toString();
646 void fromString(std::string str )
653 if(str[0] != '[' && str[str.length()-1] != ']')
658 str = str.substr(1,str.length() - 2);
660 std::vector<std::string> elements;
662 std::istringstream f(str);
665 while(std::getline(f,element,','))
673 GVariant* toVariant()
676 GVariantBuilder params;
677 g_variant_builder_init(¶ms, ((const GVariantType *) "av"));
679 for(auto itr = mList.begin(); itr != mList.end(); itr++)
681 AbstractPropertyType* t = *itr;
682 GVariant *var = t->toVariant();
683 GVariant *newvar = g_variant_new("v",var);
684 g_variant_builder_add_value(¶ms, newvar);
688 GVariant* var = g_variant_builder_end(¶ms);
694 void fromVariant(GVariant* v)
698 /// TODO: fill this in
699 gsize dictsize = g_variant_n_children(v);
700 for (int i=0;i<dictsize;i++)
702 GVariant *childvariant = g_variant_get_child_value(v,i);
703 GVariant *innervariant = g_variant_get_variant(childvariant);
705 t->fromVariant(innervariant);
710 std::list<AbstractPropertyType*> list() { return mList; }
716 for(auto itr = mList.begin(); itr != mList.end(); itr++)
723 void appendPriv(AbstractPropertyType* i)
730 std::list<AbstractPropertyType*> mList;