3ba0307f4c3520477f3c9e2483fb9b00d251b6ea
[profile/ivi/automotive-message-broker.git] / lib / 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 <boost/any.hpp>
26 #include <boost/lexical_cast.hpp>
27
28 class AbstractPropertyType
29 {
30 public:
31         virtual std::string toString() = 0;
32
33         void setValue(boost::any val)
34         {
35                 mValue = val;
36         }
37
38         template <typename T>
39         T value() const
40         {
41                 return boost::any_cast<T>(mValue);
42         }
43
44         boost::any anyValue()
45         {
46                 return mValue;
47         }
48
49 protected:
50
51         boost::any mValue;
52
53 };
54
55 template <typename T>
56 class BasicPropertyType: public AbstractPropertyType
57 {
58 public:
59         BasicPropertyType(BasicPropertyType const &other)
60         {
61                 setValue(other.value<T>());
62         }
63
64         BasicPropertyType & operator = (BasicPropertyType const & other)
65         {
66                 setValue(other.value<T>());
67                 return *this;
68         }
69
70         BasicPropertyType(T val)
71                 :AbstractPropertyType()
72         {
73                 setValue(val);
74         }
75
76         BasicPropertyType(std::string val)
77         {
78                 if(!val.empty() && val != "")
79                         setValue(boost::lexical_cast<T,std::string>(val));
80                 else throw std::runtime_error("value cannot be empty");
81         }
82
83         std::string toString()
84         {
85                 std::stringstream stream;
86                 stream<<value<T>();
87
88                 return stream.str();
89         }
90 };
91
92 class StringPropertyType: public AbstractPropertyType
93 {
94 public:
95         StringPropertyType(std::string val)
96                 :AbstractPropertyType()
97         {
98                 setValue(val);
99         }
100
101         StringPropertyType(StringPropertyType const & other)
102         {
103                 setValue(other.value<std::string>());
104         }
105
106         StringPropertyType & operator = (StringPropertyType const & other)
107         {
108                 setValue(other.value<std::string>());
109                 return *this;
110         }
111
112
113         std::string toString()
114         {
115                 return value<std::string>();
116         }
117
118 };
119
120 #endif