automotive-message-broker  0.13
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Modules 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 #include <superptr.hpp>
38 
39 class Zone {
40 
41 public:
42 
43  typedef int Type;
44 
45  enum {
46  None = 0,
47  Front = 1,
48  Middle = 1 << 1,
49  Right = 1 << 2,
50  Left = 1 << 3,
51  Rear = 1 << 4,
52  Center = 1 << 5,
53  LeftSide = 1 << 6,
54  RightSide = 1 << 7,
55  FrontSide = 1 << 8,
56  BackSide = 1 << 9
57  };
58 
59 static const Zone::Type FrontRight;
60 static const Zone::Type FrontLeft;
61 static const Zone::Type MiddleRight;
62 static const Zone::Type MiddleLeft;
63 static const Zone::Type RearRight;
64 static const Zone::Type RearLeft;
65 
66 typedef std::vector<Zone::Type> ZoneList;
67 
68 };
69 
71 {
72 public:
73 
77  enum Priority
78  {
79  Normal = 0,
80  Low,
81  High,
85  };
86 
87  AbstractPropertyType(std::string property)
88  : name(property), timestamp(amb::currentTime()), sequence(-1), zone(Zone::None), priority(Normal)
89  {
90 
91  }
92 
93  virtual ~AbstractPropertyType()
94  {
95  for(auto i : destroyed)
96  {
97  if(i) i(this);
98  }
99  }
100 
105  virtual std::string toString() const = 0;
106 
110  virtual void fromString(std::string)= 0;
111 
116  virtual GVariant* toVariant() = 0;
117 
122  virtual void fromVariant(GVariant*) = 0;
123 
128  virtual AbstractPropertyType* copy() = 0;
129 
135  virtual void quickCopy(AbstractPropertyType* other)
136  {
137  sequence = other->sequence;
138  mValue = other->anyValue();
139  timestamp = other->timestamp;
140  }
141 
142  bool operator == (AbstractPropertyType &other)
143  {
144  std::string one = toString();
145  std::string two = other.toString();
146  return one == two
147  && zone == other.zone
148  && sourceUuid == other.sourceUuid
149  && name == other.name;
150  }
151 
152  bool operator != (AbstractPropertyType &other)
153  {
154  std::string one = toString();
155  std::string two = other.toString();
156  return one != two;
157  }
158 
162  std::string name;
163 
170  double timestamp;
171 
175  int32_t sequence;
176 
181  std::string sourceUuid;
182 
186  Zone::Type zone;
187 
195 
201  virtual void setValue(boost::any val)
202  {
203  mValue = val;
204  timestamp = amb::currentTime();
205  }
206 
210  template <typename T>
211  T value() const
212  {
213  return boost::any_cast<T>(mValue);
214  }
215 
220  boost::any anyValue()
221  {
222  return mValue;
223  }
224 
229  virtual const string signature()
230  {
231  auto var = amb::make_super(toVariant());
232  if(!var) return "";
233 
234  const string s = g_variant_get_type_string(var.get());
235 
236  DebugOut() << "returning signature: " << s << " for "<< name << endl;
237 
238  return s;
239  }
240 
244  std::vector<std::function<void(AbstractPropertyType*)>> destroyed;
245 
246 protected:
247 
248  boost::any mValue;
249 
250 };
251 
252 namespace amb
253 {
254 
256 {
257  bool operator()(AbstractPropertyType* const & lhs, AbstractPropertyType* & rhs) const
258  {
259  if (lhs->name == rhs->name
260  && lhs->sourceUuid == rhs->sourceUuid
261  && lhs->zone == rhs->zone)
262  {
263  return true;
264  }
265 
266  return false;
267  }
268 
269 };
270 
271 }
272 
273 
274 template <typename T>
275 class GVS;
276 
277 template <>
278 class GVS<int>
279 {
280 public:
281  static const char* signature() { return "i"; }
282 
283  static int value(GVariant* v)
284  {
285  int val = 0;
286  g_variant_get(v, signature(), &val);
287  return val;
288  }
289 
290  static std::string stringize(std::string v)
291  {
292  return v;
293  }
294 };
295 
296 template <>
297 class GVS<double>
298 {
299 public:
300  static const char* signature() { return "d"; }
301 
302  static double value(GVariant* v)
303  {
304  return g_variant_get_double(v);
305  }
306  static std::string stringize(std::string v)
307  {
308  return v;
309  }
310 };
311 
312 template <>
313 class GVS<uint16_t>
314 {
315 public:
316  static const char* signature() { return "q"; }
317 
318  static uint16_t value(GVariant* v)
319  {
320  return g_variant_get_uint16(v);
321  }
322  static std::string stringize(std::string v)
323  {
324  return v;
325  }
326 };
327 
328 template <>
329 class GVS<int16_t>
330 {
331 public:
332  static const char* signature() { return "n"; }
333 
334  static int16_t value(GVariant* v)
335  {
336  return g_variant_get_int16(v);
337  }
338  static std::string stringize(std::string v)
339  {
340  return v;
341  }
342 };
343 
344 template <>
345 class GVS<char>
346 {
347 public:
348  static const char* signature() { return "y"; }
349 
350  static char value(GVariant* v)
351  {
352  return g_variant_get_byte(v);
353  }
354  static std::string stringize(std::string v)
355  {
356  return v;
357  }
358 };
359 
360 template <>
361 class GVS<uint32_t>
362 {
363 public:
364  static const char* signature() { return "u"; }
365 
366  static uint32_t value(GVariant* v)
367  {
368  return g_variant_get_uint32(v);
369  }
370  static std::string stringize(std::string v)
371  {
372  return v;
373  }
374 };
375 
376 template <>
377 class GVS<int64_t>
378 {
379 public:
380  static const char* signature() { return "x"; }
381 
382  static int64_t value(GVariant* v)
383  {
384  return g_variant_get_int64(v);
385  }
386  static std::string stringize(std::string v)
387  {
388  return v;
389  }
390 };
391 
392 template <>
393 class GVS<uint64_t>
394 {
395 public:
396  static const char* signature() { return "t"; }
397 
398  static uint64_t value(GVariant* v)
399  {
400  return g_variant_get_uint64(v);
401  }
402  static std::string stringize(std::string v)
403  {
404  return v;
405  }
406 };
407 
408 template <>
409 class GVS<bool>
410 {
411 public:
412  static const char* signature() { return "b"; }
413 
414  static bool value(GVariant *v)
415  {
416  return g_variant_get_boolean(v);
417  }
418  static std::string stringize(std::string v)
419  {
420  if(v == "0" || v == "1")
421  return v;
422 
423  boost::algorithm::to_lower(v);
424  return v == "true" ? "1":"0";
425  }
426 };
427 
435 template <typename T>
437 {
438 public:
440  {
441  mValue = T();
442  }
443 
444  BasicPropertyType(BasicPropertyType const & other)
445  :AbstractPropertyType(other.name)
446  {
447  setValue(other.value<T>());
448  timestamp = other.timestamp;
449  sequence = other.sequence;
450  sourceUuid = other.sourceUuid;
451  name = other.name;
452  zone = other.zone;
453 
454  }
455 
456  BasicPropertyType & operator = (BasicPropertyType const & other)
457  {
458  setValue(other.value<T>());
459  timestamp = other.timestamp;
460  sequence = other.sequence;
461  sourceUuid = other.sourceUuid;
462  name = other.name;
463  zone = other.zone;
464 
465  return *this;
466  }
467 
468  BasicPropertyType & operator = (T const & other)
469  {
470  setValue(other);
471  return *this;
472  }
473 
474  BasicPropertyType & operator ++ ()
475  {
476  setValue(basicValue() + 1);
477  }
478 
479  BasicPropertyType & operator -- ()
480  {
481  setValue(basicValue() - 1);
482  }
483 
484  bool operator < (const BasicPropertyType<T>& other) const
485  {
486  return value<T>() < other.value<T>();
487  }
488 
489  bool operator > (const BasicPropertyType<T>& other) const
490  {
491  return value<T>() > other.value<T>();
492  }
493 
494  BasicPropertyType( T val)
496  {
497  setValue(val);
498  }
499 
500  BasicPropertyType( std::string propertyName, T val)
501  :AbstractPropertyType(propertyName)
502  {
503  setValue(val);
504  }
505 
506  BasicPropertyType( std::string propertyName, std::string val)
507  :AbstractPropertyType(propertyName)
508  {
509  if(!val.empty() && val != "")
510  {
511  serialize<T>(val);
512  }
513  else setValue(T());
514  }
515 
516  BasicPropertyType(std::string propertyName)
517  :AbstractPropertyType(propertyName)
518  {
519  mValue = T();
520  }
521 
523  {
524  return new BasicPropertyType<T>(*this);
525  }
526 
527  void fromString(std::string val)
528  {
529  if(!val.empty() && val != "")
530  {
531  serialize<T>(val);
532  }
533  }
534 
535  std::string toString() const
536  {
537  std::stringstream stream;
538  stream.precision(10);
539  stream<<value<T>();
540 
541  return stream.str();
542  }
543 
544  GVariant* toVariant()
545  {
546  return serializeVariant<T>(value<T>());
547  }
548 
549  void fromVariant(GVariant *v)
550  {
551  setValue(deserializeVariant<T>(v));
552  }
553 
560  {
561  return value<T>();
562  }
563 
564  void setValue(T val)
565  {
567  }
568 
569  void setValue(boost::any val)
570  {
572  }
573 
574 private:
575 
576  //GVariant* mVariant;
577 
578  template <class N>
579  void serialize(std::string val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
580  {
581  int someTemp;
582 
583  std::stringstream stream(val);
584 
585  stream>>someTemp;
586  setValue((N)someTemp);
587  }
588 
589  template <class N>
590  void serialize(std::string val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
591  {
592  std::stringstream stream(GVS<T>::stringize(val));
593  N someTemp;
594  stream>>someTemp;
595  setValue(someTemp);
596  }
597 
598  template <class N>
599  GVariant* serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
600  {
601  //mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
602 
603  return (g_variant_new("i",(int)val));
604  }
605 
606  template <class N>
607  GVariant* serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
608  {
609  //mVariant = Glib::Variant<T>::create(val);
610  //mVariant = g_variant_ref(g_variant_new(GVS<T>::signature(),val));
611  return g_variant_new(GVS<T>::signature(),val);
612  }
613 
614  template <class N>
615  T deserializeVariant(GVariant* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
616  {
617 // return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
618 
619  return (T)GVS<int>::value(v);
620  }
621 
622  template <class N>
623  T deserializeVariant(GVariant* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
624  {
625  // return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
626  return GVS<T>::value(v);
627  }
628 };
629 
631 {
632 public:
633 
634 
637  {
638  setValue(std::string());
639  }
640 
641  StringPropertyType(std::string propertyName)
642  :AbstractPropertyType(propertyName)
643  {
644  setValue(std::string());
645  }
646 
647  StringPropertyType(std::string propertyName, std::string val)
648  :AbstractPropertyType(propertyName)
649  {
650  setValue(val);
651  }
652 
654  :AbstractPropertyType(other.name)
655  {
656  setValue(other.value<std::string>());
657  timestamp = other.timestamp;
658  sequence = other.sequence;
659  sourceUuid = other.sourceUuid;
660  name = other.name;
661  zone = other.zone;
662  }
663 
664  StringPropertyType & operator = (StringPropertyType const & other)
665  {
666  setValue(other.value<std::string>());
667  timestamp = other.timestamp;
668  sequence = other.sequence;
669  sourceUuid = other.sourceUuid;
670  name = other.name;
671  zone = other.zone;
672 
673  return *this;
674  }
675 
676  StringPropertyType & operator = (std::string const & other)
677  {
678  setValue(std::string(other));
679  return *this;
680  }
681 
682  bool operator < (const StringPropertyType& other) const
683  {
684  return value<std::string>() < other.value<std::string>();
685  }
686 
687 
688  void fromString(std::string val)
689  {
690  setValue(val);
691  }
692 
694  {
695  return new StringPropertyType(*this);
696  }
697 
698  std::string toString() const
699  {
700  return value<std::string>();
701  }
702 
703  GVariant* toVariant()
704  {
705  //mVariant = Glib::Variant<std::string>::create(toString());
706 
707  return g_variant_new_string(toString().c_str());
708 
709  }
710 
711  void fromVariant(GVariant *v)
712  {
713  setValue(std::string(g_variant_get_string(v,NULL)));
714  }
715 };
716 
720 template <class T = AbstractPropertyType>
722 {
723 public:
724 
725  ListPropertyType(std::string propertyName)
726  : AbstractPropertyType(propertyName), initialized(false)
727  {
728 
729  }
730 
731  ListPropertyType(std::string propertyName, T value)
732  : AbstractPropertyType(propertyName), initialized(false)
733  {
734  appendPriv(value);
735  }
736 
738  :AbstractPropertyType(other.name),initialized(false)
739  {
740  std::vector<T> l = other.list();
741  for(auto i : l)
742  {
743  append(i);
744  }
745 
746  timestamp = other.timestamp;
747  sequence = other.sequence;
748  sourceUuid = other.sourceUuid;
749  name = other.name;
750  zone = other.zone;
751  }
752 
754  {
755  clear();
756  }
757 
761  void append(T property)
762  {
763  if(!initialized)
764  {
765  mList.clear();
766  initialized = true;
767  }
768 
769  appendPriv(property);
770  }
771 
772  uint count()
773  {
774  return mList.size();
775  }
776 
778  {
779  return new ListPropertyType(*this);
780  }
781 
783  {
785  ListPropertyType<T>* v = static_cast<ListPropertyType<T>*>(other);
786  if(!v)
787  {
788  DebugOut(DebugOut::Error) << "ListPropertyType Quick Copy failed" << endl;
789  return;
790  }
791  mList = v->list();
792  }
793 
794  std::string toString() const
795  {
796  std::string str = "[";
797 
798  for(auto itr = mList.begin(); itr != mList.end(); itr++)
799  {
800  if(str != "[")
801  str += ",";
802 
803  T t = *itr;
804 
805  str += t.toString();
806  }
807 
808  str += "]";
809 
810  return str;
811  }
812 
813 
814  void fromString(std::string str )
815  {
816  clear();
817 
818  if(!str.length())
819  return;
820 
821  if(str[0] == '[' && str[str.length()-1] == ']')
822  {
823  str = str.substr(1,str.length() - 2);
824  }
825 
826  std::vector<std::string> elements;
827 
828  std::istringstream f(str);
829 
830  std::string element;
831  while(std::getline(f,element,','))
832  {
833  T foo("", element);
834  append (foo);
835  }
836  timestamp = amb::currentTime();
837  }
838 
839 
840  GVariant* toVariant()
841  {
842 
843  GVariantBuilder params;
844  g_variant_builder_init(&params, ((const GVariantType *) "av"));
845 
846  for(auto itr = mList.begin(); itr != mList.end(); itr++)
847  {
848  T t = *itr;
849  auto var = t.toVariant();
850  GVariant *newvar = g_variant_new("v", var);
851  g_variant_builder_add_value(&params, newvar);
852  }
853 
854  GVariant* var = g_variant_builder_end(&params);
855  g_assert(var);
856  return var;
857 
858  }
859 
860  void fromVariant(GVariant* v)
861  {
862  clear();
863 
865  gsize dictsize = g_variant_n_children(v);
866  for (int i=0;i<dictsize;i++)
867  {
868  GVariant *childvariant = g_variant_get_child_value(v,i);
869  GVariant *innervariant = g_variant_get_variant(childvariant);
870  T t;
871  t.fromVariant(innervariant);
872  appendPriv(t);
873  }
874  }
875 
876  std::vector<T> list() { return mList; }
877 
878 private:
879 
880  void clear()
881  {
882  mList.clear();
883  }
884 
885  void appendPriv(T i)
886  {
887  mList.push_back(i);
888  }
889 
890  bool initialized;
891 
892  std::vector<T> mList;
893 };
894 
895 #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:175
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:711
void setValue(boost::any val)
setValue
Definition: abstractpropertytype.h:569
Definition: abstractpropertytype.h:80
Definition: abstractpropertytype.h:70
void fromString(std::string val)
fromString converts from string value
Definition: abstractpropertytype.h:527
virtual const string signature()
signature
Definition: abstractpropertytype.h:229
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:522
T basicValue()
basicValue
Definition: abstractpropertytype.h:559
ListPropertyType is a AbstractPropertyType for arrays of AbstractPropertyTypes.
Definition: abstractpropertytype.h:721
Definition: abstractpropertytype.h:255
static const int Error
Error use when essential functionality is blocked.
Definition: debugout.h:64
Definition: abstractpropertytype.h:275
virtual AbstractPropertyType * copy()=0
copy
std::string toString() const
toString
Definition: abstractpropertytype.h:698
std::string toString() const
toString
Definition: abstractpropertytype.h:794
Priority priority
priority is used to tell the routing engine how to prioritize routing the value to plugins...
Definition: abstractpropertytype.h:194
void fromString(std::string str)
fromString converts from string value
Definition: abstractpropertytype.h:814
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:840
Definition: abstractpropertytype.h:39
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:170
virtual std::string toString() const =0
toString
std::string toString() const
toString
Definition: abstractpropertytype.h:535
Definition: abstractpropertytype.h:252
Zone::Type zone
zone that the property is situated in.
Definition: abstractpropertytype.h:186
Definition: abstractpropertytype.h:82
Priority
The Priority enum describes prority of the property type.
Definition: abstractpropertytype.h:77
std::string name
name Property name.
Definition: abstractpropertytype.h:162
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:703
void append(T property)
append - appends a property to the list
Definition: abstractpropertytype.h:761
void fromString(std::string val)
fromString converts from string value
Definition: abstractpropertytype.h:688
std::string sourceUuid
sourceUuid uuid of the source that produced this property. This is set by the routingengine if left u...
Definition: abstractpropertytype.h:181
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:693
Definition: abstractpropertytype.h:436
virtual void fromVariant(GVariant *)=0
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:81
GVariant * toVariant()
toVariant
Definition: abstractpropertytype.h:544
Definition: abstractpropertytype.h:630
std::vector< std::function< void(AbstractPropertyType *)> > destroyed
destroyed is called if this property is destroyed.
Definition: abstractpropertytype.h:244
Definition: abstractpropertytype.h:79
virtual void setValue(boost::any val)
setValue
Definition: abstractpropertytype.h:201
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:135
The DebugOut class represents a class used for outputing debug information The specified debug level ...
Definition: debugout.h:57
T value() const
value() native value. Does not use type coercion. Will throw if types do not match.
Definition: abstractpropertytype.h:211
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:860
boost::any anyValue()
anyValue
Definition: abstractpropertytype.h:220
void fromVariant(GVariant *v)
fromVariant converts GVariant value into compatible native value. Caller owns GVariant argument...
Definition: abstractpropertytype.h:549
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:782
AbstractPropertyType * copy()
copy
Definition: abstractpropertytype.h:777