automotive-message-broker  0.12
 All Classes Functions Variables Typedefs Enumerations Enumerator Pages
abstractpropertytype.h
1 /*
2  Copyright (C) 2012 Intel Corporation
3 
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.
8 
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.
13 
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
17 */
18 
19 #ifndef _ABSTRACTPROPERTYTYPE_H_
20 #define _ABSTRACTPROPERTYTYPE_H_
21 
22 #include <string>
23 #include <sstream>
24 #include <stdexcept>
25 #include <vector>
26 #include <iostream>
27 #include <memory>
28 #include <boost/any.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <boost/utility.hpp>
31 #include <type_traits>
32 #include <glib.h>
33 #include <list>
34 #include "timestamp.h"
35 #include <debugout.h>
36 #include <boost/algorithm/string.hpp>
37 
38 class Zone {
39 
40 public:
41 
42  typedef int Type;
43 
44  enum {
45  None = 0,
46  Front = 1,
47  Middle = 1 << 1,
48  Right = 1 << 2,
49  Left = 1 << 3,
50  Rear = 1 << 4,
51  Center = 1 << 5,
52  LeftSide = 1 << 6,
53  RightSide = 1 << 7,
54  FrontSide = 1 << 8,
55  BackSide = 1 << 9
56  };
57 
58 static const Zone::Type FrontRight;
59 static const Zone::Type FrontLeft;
60 static const Zone::Type MiddleRight;
61 static const Zone::Type MiddleLeft;
62 static const Zone::Type RearRight;
63 static const Zone::Type RearLeft;
64 
65 typedef std::list<Zone::Type> ZoneList;
66 
67 };
68 
70 {
71 public:
72 
76  enum Priority
77  {
78  Normal = 0,
79  Low,
80  High,
84  };
85 
86  AbstractPropertyType(std::string property)
87  : name(property), timestamp(amb::currentTime()), sequence(-1), zone(Zone::None), priority(Normal)
88  {
89  void*(name);
90  }
91 
92  virtual ~AbstractPropertyType()
93  {
94  for(auto i : destroyed)
95  {
96  if(i) i(this);
97  }
98  }
99 
104  virtual std::string toString() const = 0;
105 
109  virtual void fromString(std::string)= 0;
110 
115  virtual GVariant* toVariant() = 0;
116 
121  virtual void fromVariant(GVariant*) = 0;
122 
127  virtual AbstractPropertyType* copy() = 0;
128 
134  virtual void quickCopy(AbstractPropertyType* other)
135  {
136  sequence = other->sequence;
137  mValue = other->anyValue();
138  timestamp = other->timestamp;
139  }
140 
141  bool operator == (AbstractPropertyType &other)
142  {
143  std::string one = toString();
144  std::string two = other.toString();
145  return one == two
146  && zone == other.zone
147  && sourceUuid == other.sourceUuid
148  && name == other.name;
149  }
150 
151  bool operator != (AbstractPropertyType &other)
152  {
153  std::string one = toString();
154  std::string two = other.toString();
155  return one != two;
156  }
157 
161  std::string name;
162 
169  double timestamp;
170 
174  int32_t sequence;
175 
180  std::string sourceUuid;
181 
185  Zone::Type zone;
186 
194 
200  virtual void setValue(boost::any val)
201  {
202  mValue = val;
203  timestamp = amb::currentTime();
204  }
205 
209  template <typename T>
210  T value() const
211  {
212  return boost::any_cast<T>(mValue);
213  }
214 
219  boost::any anyValue()
220  {
221  return mValue;
222  }
223 
227  std::vector<std::function<void(AbstractPropertyType*)>> destroyed;
228 
229 protected:
230 
231  boost::any mValue;
232 
233 };
234 
235 template <typename T>
236 class GVS;
237 
238 template <>
239 class GVS<int>
240 {
241 public:
242  static const char* signature() { return "i"; }
243 
244  static int value(GVariant* v)
245  {
246  return g_variant_get_int32(v);
247  }
248 
249  static std::string stringize(std::string v)
250  {
251  return v;
252  }
253 };
254 
255 template <>
256 class GVS<double>
257 {
258 public:
259  static const char* signature() { return "d"; }
260 
261  static double value(GVariant* v)
262  {
263  return g_variant_get_double(v);
264  }
265  static std::string stringize(std::string v)
266  {
267  return v;
268  }
269 };
270 
271 template <>
272 class GVS<uint16_t>
273 {
274 public:
275  static const char* signature() { return "q"; }
276 
277  static uint16_t value(GVariant* v)
278  {
279  return g_variant_get_uint16(v);
280  }
281  static std::string stringize(std::string v)
282  {
283  return v;
284  }
285 };
286 
287 template <>
288 class GVS<int16_t>
289 {
290 public:
291  static const char* signature() { return "n"; }
292 
293  static int16_t value(GVariant* v)
294  {
295  return g_variant_get_int16(v);
296  }
297  static std::string stringize(std::string v)
298  {
299  return v;
300  }
301 };
302 
303 template <>
304 class GVS<char>
305 {
306 public:
307  static const char* signature() { return "y"; }
308 
309  static char value(GVariant* v)
310  {
311  return g_variant_get_byte(v);
312  }
313  static std::string stringize(std::string v)
314  {
315  return v;
316  }
317 };
318 
319 template <>
320 class GVS<uint32_t>
321 {
322 public:
323  static const char* signature() { return "u"; }
324 
325  static uint32_t value(GVariant* v)
326  {
327  return g_variant_get_uint32(v);
328  }
329  static std::string stringize(std::string v)
330  {
331  return v;
332  }
333 };
334 
335 template <>
336 class GVS<int64_t>
337 {
338 public:
339  static const char* signature() { return "x"; }
340 
341  static int64_t value(GVariant* v)
342  {
343  return g_variant_get_int64(v);
344  }
345  static std::string stringize(std::string v)
346  {
347  return v;
348  }
349 };
350 
351 template <>
352 class GVS<uint64_t>
353 {
354 public:
355  static const char* signature() { return "t"; }
356 
357  static uint64_t value(GVariant* v)
358  {
359  return g_variant_get_uint64(v);
360  }
361  static std::string stringize(std::string v)
362  {
363  return v;
364  }
365 };
366 
367 template <>
368 class GVS<bool>
369 {
370 public:
371  static const char* signature() { return "b"; }
372 
373  static bool value(GVariant *v)
374  {
375  return g_variant_get_boolean(v);
376  }
377  static std::string stringize(std::string v)
378  {
379  if(v == "0" || v == "1")
380  return v;
381 
382  boost::algorithm::to_lower(v);
383  return v == "true" ? "1":"0";
384  }
385 };
386 
394 template <typename T>
396 {
397 public:
399  {
400  mValue = T();
401  }
403  :AbstractPropertyType(other.name)
404  {
405  setValue(other.value<T>());
406  timestamp = other.timestamp;
407  sequence = other.sequence;
408  sourceUuid = other.sourceUuid;
409  name = other.name;
410  zone = other.zone;
411 
412  }
413 
414  BasicPropertyType & operator = (BasicPropertyType const & other)
415  {
416  setValue(other.value<T>());
417  timestamp = other.timestamp;
418  sequence = other.sequence;
419  sourceUuid = other.sourceUuid;
420  name = other.name;
421  zone = other.zone;
422 
423  return *this;
424  }
425 
426  BasicPropertyType & operator = (T const & other)
427  {
428  setValue(other);
429  return *this;
430  }
431 
432  BasicPropertyType & operator ++ ()
433  {
434  setValue(basicValue() + 1);
435  }
436 
437  BasicPropertyType & operator -- ()
438  {
439  setValue(basicValue() - 1);
440  }
441 
442  bool operator < (const BasicPropertyType<T>& other) const
443  {
444  return value<T>() < other.value<T>();
445  }
446 
447  bool operator > (const BasicPropertyType<T>& other) const
448  {
449  return value<T>() > other.value<T>();
450  }
451 
452  BasicPropertyType( T val)
454  {
455  setValue(val);
456  }
457 
458  BasicPropertyType( std::string propertyName, T val)
459  :AbstractPropertyType(propertyName)
460  {
461  setValue(val);
462  }
463 
464  BasicPropertyType( std::string propertyName, std::string val)
465  :AbstractPropertyType(propertyName)
466  {
467  if(!val.empty() && val != "")
468  {
469  serialize<T>(val);
470  }
471  else setValue(T());
472  }
473 
474  BasicPropertyType(std::string propertyName)
475  :AbstractPropertyType(propertyName)
476  {
477  mValue = T();
478  }
479 
481  {
482  return new BasicPropertyType<T>(*this);
483  }
484 
485  void fromString(std::string val)
486  {
487  if(!val.empty() && val != "")
488  {
489  serialize<T>(val);
490  }
491  }
492 
493  std::string toString() const
494  {
495  std::stringstream stream;
496  stream.precision(10);
497  stream<<value<T>();
498 
499  return stream.str();
500  }
501 
502  GVariant* toVariant()
503  {
504  return serializeVariant<T>(value<T>());
505  }
506 
507  void fromVariant(GVariant *v)
508  {
509  setValue(deserializeVariant<T>(v));
510  }
511 
518  {
519  return value<T>();
520  }
521 
522  void setValue(T val)
523  {
525  }
526 
527  void setValue(boost::any val)
528  {
530  }
531 
532 private:
533 
534  //GVariant* mVariant;
535 
536  template <class N>
537  void serialize(std::string val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
538  {
539  int someTemp;
540 
541  std::stringstream stream(val);
542 
543  stream>>someTemp;
544  setValue((N)someTemp);
545  }
546 
547  template <class N>
548  void serialize(std::string val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
549  {
550  std::stringstream stream(GVS<T>::stringize(val));
551  N someTemp;
552  stream>>someTemp;
553  setValue(someTemp);
554  }
555 
556  template <class N>
557  GVariant* serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
558  {
559  //mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
560 
561  return (g_variant_new("i",(int)val));
562  }
563 
564  template <class N>
565  GVariant* serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
566  {
567  //mVariant = Glib::Variant<T>::create(val);
568  //mVariant = g_variant_ref(g_variant_new(GVS<T>::signature(),val));
569  return g_variant_new(GVS<T>::signature(),val);
570  }
571 
572  template <class N>
573  T deserializeVariant(GVariant* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
574  {
575 // return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
576 
577  return (T)GVS<int>::value(v);
578  }
579 
580  template <class N>
581  T deserializeVariant(GVariant* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
582  {
583  // return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
584  return GVS<T>::value(v);
585  }
586 };
587 
589 {
590 public:
591  StringPropertyType(std::string propertyName)
592  :AbstractPropertyType(propertyName)
593  {
594  setValue(std::string());
595  }
596 
597  StringPropertyType(std::string propertyName, std::string val)
598  :AbstractPropertyType(propertyName)
599  {
600  setValue(val);
601  }
602 
604  :AbstractPropertyType(other.name)
605  {
606  setValue(other.value<std::string>());
607  timestamp = other.timestamp;
608  sequence = other.sequence;
609  sourceUuid = other.sourceUuid;
610  name = other.name;
611  zone = other.zone;
612  }
613 
614  StringPropertyType & operator = (StringPropertyType const & other)
615  {
616  setValue(other.value<std::string>());
617  timestamp = other.timestamp;
618  sequence = other.sequence;
619  sourceUuid = other.sourceUuid;
620  name = other.name;
621  zone = other.zone;
622 
623  return *this;
624  }
625 
626  StringPropertyType & operator = (std::string const & other)
627  {
628  setValue(std::string(other));
629  return *this;
630  }
631 
632  void fromString(std::string val)
633  {
634  setValue(val);
635  }
636 
638  {
639  return new StringPropertyType(*this);
640  }
641 
642  std::string toString() const
643  {
644  return value<std::string>();
645  }
646 
647  GVariant* toVariant()
648  {
649  //mVariant = Glib::Variant<std::string>::create(toString());
650 
651  return g_variant_new_string(toString().c_str());
652 
653  }
654 
655  void fromVariant(GVariant *v)
656  {
657  setValue(std::string(g_variant_get_string(v,NULL)));
658  }
659 };
660 
661 template <class T>
663 {
664 public:
665 
666  ListPropertyType(std::string propertyName)
667  : AbstractPropertyType(propertyName), initialized(false)
668  {
669 
670  }
671 
672  ListPropertyType(std::string propertyName, AbstractPropertyType *value)
673  : AbstractPropertyType(propertyName), initialized(false)
674  {
675  appendPriv(value->copy());
676  }
677 
679  :AbstractPropertyType(other.name),initialized(false)
680  {
681  std::list<AbstractPropertyType*> l = other.list();
682  for(auto itr = l.begin(); itr != l.end(); itr++)
683  {
684  append(*itr);
685  }
686 
687  timestamp = other.timestamp;
688  sequence = other.sequence;
689  sourceUuid = other.sourceUuid;
690  name = other.name;
691  zone = other.zone;
692  }
693 
695  {
696  clear();
697  }
698 
703  void append(AbstractPropertyType* property)
704  {
705  if(!initialized)
706  {
707  for(auto itr = mList.begin(); itr != mList.end(); itr++)
708  {
709  AbstractPropertyType *p = *itr;
710  delete p;
711  }
712  mList.clear();
713  initialized = true;
714  }
715 
716  appendPriv(property->copy());
717  }
718 
719  uint count()
720  {
721  return mList.size();
722  }
723 
725  {
726  return new ListPropertyType(*this);
727  }
728 
729  std::string toString() const
730  {
731  std::string str = "[";
732 
733  for(auto itr = mList.begin(); itr != mList.end(); itr++)
734  {
735  if(str != "[")
736  str += ",";
737 
738  AbstractPropertyType* t = *itr;
739 
740  str += t->toString();
741  }
742 
743  str += "]";
744 
745  return str;
746  }
747 
748 
749  void fromString(std::string str )
750  {
751  clear();
752 
753  if(!str.length())
754  return;
755 
756  if(str[0] != '[' && str[str.length()-1] != ']')
757  {
758  return;
759  }
760 
761  str = str.substr(1,str.length() - 2);
762 
763  std::vector<std::string> elements;
764 
765  std::istringstream f(str);
766 
767  std::string element;
768  while(std::getline(f,element,','))
769  {
770  T foo("", element);
771  append (&foo);
772  }
773  }
774 
775 
776  GVariant* toVariant()
777  {
778 
779  GVariantBuilder params;
780  g_variant_builder_init(&params, ((const GVariantType *) "av"));
781 
782  for(auto itr = mList.begin(); itr != mList.end(); itr++)
783  {
784  AbstractPropertyType* t = *itr;
785  GVariant *var = t->toVariant();
786  GVariant *newvar = g_variant_new("v",var);
787  g_variant_builder_add_value(&params, newvar);
788 
789  }
790 
791  GVariant* var = g_variant_builder_end(&params);
792  g_assert(var);
793  return var;
794 
795  }
796 
797  void fromVariant(GVariant* v)
798  {
799  clear();
800 
802  gsize dictsize = g_variant_n_children(v);
803  for (int i=0;i<dictsize;i++)
804  {
805  GVariant *childvariant = g_variant_get_child_value(v,i);
806  GVariant *innervariant = g_variant_get_variant(childvariant);
807  T *t = new T();
808  t->fromVariant(innervariant);
809  appendPriv(t);
810  }
811  }
812 
813  std::list<AbstractPropertyType*> list() { return mList; }
814 
815 private:
816 
817  void clear()
818  {
819  for(auto itr = mList.begin(); itr != mList.end(); itr++)
820  {
821  delete *itr;
822  }
823  mList.clear();
824  }
825 
826  void appendPriv(AbstractPropertyType* i)
827  {
828  mList.push_back(i);
829  }
830 
831  bool initialized;
832 
833  std::list<AbstractPropertyType*> mList;
834 };
835 
836 #endif
virtual GVariant * toVariant()=0
toVariant
int32_t sequence
sequence internal counter. Useful as a unique indentifier. values is -1 if not used (default)...
Definition: abstractpropertytype.h:174
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:655
void setValue(boost::any val)
setValue
Definition: abstractpropertytype.h:527
Definition: abstractpropertytype.h:79
Definition: abstractpropertytype.h:69
void fromString(std::string val)
fromString converts from string value
Definition: abstractpropertytype.h:485
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:480
T basicValue()
basicValue
Definition: abstractpropertytype.h:517
Definition: abstractpropertytype.h:662
virtual AbstractPropertyType * copy()=0
copy
std::string toString() const
toString
Definition: abstractpropertytype.h:642
std::string toString() const
toString
Definition: abstractpropertytype.h:729
Priority priority
priority is used to tell the routing engine how to prioritize routing the value to plugins...
Definition: abstractpropertytype.h:193
void fromString(std::string str)
fromString converts from string value
Definition: abstractpropertytype.h:749
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:776
Definition: abstractpropertytype.h:38
virtual void fromString(std::string)=0
fromString converts from string value
double timestamp
timestamp. Timestamp when the value was last updated by the system. This is updated automatically any...
Definition: abstractpropertytype.h:169
virtual std::string toString() const =0
toString
std::string toString() const
toString
Definition: abstractpropertytype.h:493
Zone::Type zone
zone that the property is situated in.
Definition: abstractpropertytype.h:185
Definition: abstractpropertytype.h:81
Priority
The Priority enum describes prority of the property type.
Definition: abstractpropertytype.h:76
std::string name
name Property name.
Definition: abstractpropertytype.h:161
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:647
void fromString(std::string val)
fromString converts from string value
Definition: abstractpropertytype.h:632
std::string sourceUuid
sourceUuid uuid of the source that produced this property. This is set by the routingengine if left u...
Definition: abstractpropertytype.h:180
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:637
Definition: abstractpropertytype.h:395
Definition: abstractpropertytype.h:236
virtual void fromVariant(GVariant *)=0
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:80
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:502
Definition: abstractpropertytype.h:588
std::vector< std::function< void(AbstractPropertyType *)> > destroyed
destroyed is called if this property is destroyed.
Definition: abstractpropertytype.h:227
Definition: abstractpropertytype.h:78
virtual void setValue(boost::any val)
setValue
Definition: abstractpropertytype.h:200
virtual void quickCopy(AbstractPropertyType *other)
quickCopy is intended as a way to quickly copy the often changing bits from one abstract property to ...
Definition: abstractpropertytype.h:134
T value() const
value() native value. Does not use type coercion. Will throw if types do not match.
Definition: abstractpropertytype.h:210
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:797
boost::any anyValue()
anyValue
Definition: abstractpropertytype.h:219
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:507
void append(AbstractPropertyType *property)
Definition: abstractpropertytype.h:703
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:724