96699340f99c1e1beccaa42c4f6c3eb5d735d9fa
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Filter / AnyType.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * @file        AnyType.h
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Declaration of the JSFilter class
22  */
23
24 #ifndef _API_ANYTYPE_H_
25 #define _API_ANYTYPE_H_
26
27 #include <ctime>
28 #include <iomanip>
29 #include <string>
30 #include <dpl/shared_ptr.h>
31 #include <CommonsJavaScript/Converter.h>
32
33 namespace TizenApis {
34 namespace Api {
35 namespace Tizen {
36
37 #define _PT_NOTYPE      (0)
38 #define _PT_BOOLEAN     (1 << 0)
39 #define _PT_CHAR        (1 << 1)
40 #define _PT_UCHAR       (1 << 2)
41 #define _PT_INT         (1 << 3)
42 #define _PT_UINT        (1 << 4)
43 #define _PT_LONG        (1 << 5)
44 #define _PT_ULONG       (1 << 6)
45 #define _PT_DOUBLE      (1 << 7)
46 #define _PT_STRING      (1 << 8)
47 #define _PT_TIME        (1 << 9)
48 #define _PT_OTHER       (1 << 10)
49
50 enum PrimitiveType {
51         PrimitiveType_Notype    = _PT_NOTYPE,
52         PrimitiveType_Boolean   = _PT_BOOLEAN,
53         PrimitiveType_Char              = _PT_CHAR,
54         PrimitiveType_UChar             = _PT_UCHAR,
55         PrimitiveType_Int               = _PT_INT,
56         PrimitiveType_UInt              = _PT_UINT,
57         PrimitiveType_Long              = _PT_LONG,
58         PrimitiveType_ULong             = _PT_ULONG,
59         PrimitiveType_Double    = _PT_DOUBLE,
60         PrimitiveType_String    = _PT_STRING,
61         PrimitiveType_Time              = _PT_TIME,
62         PrimitiveType_Other             = _PT_OTHER,
63         PrimitiveType_Number    = (_PT_CHAR | _PT_UCHAR | _PT_INT | _PT_UINT |
64                                                                 _PT_LONG | _PT_ULONG | _PT_DOUBLE),
65         PrimitiveType_Any               = (_PT_BOOLEAN | _PT_CHAR | _PT_UCHAR | _PT_INT |
66                                                                 _PT_UINT | _PT_LONG | _PT_ULONG | _PT_DOUBLE |
67                                                                 _PT_STRING | _PT_TIME | _PT_OTHER )
68 };
69
70 class Any
71 {
72 public:
73         virtual std::string toString() const = 0;
74         virtual PrimitiveType getType() const = 0;
75 };
76
77 typedef DPL::SharedPtr<Any> AnyPtr;
78
79 template<typename T>
80 class Any_Common : public Any
81 {
82 protected:
83         T       m_value;
84
85 public:
86         explicit Any_Common(const T& value) { m_value = value; };
87         virtual ~Any_Common() {}
88
89         virtual T getValue() const { return m_value; }
90         virtual void setValue(const T& value) { m_value = value; }
91
92         virtual JSValueRef toJSValueRef(JSContextRef context) const
93         {
94                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
95                 return converter.toJSValueRef(m_value);
96         }
97 };
98
99 template<typename T>
100 class Any_Common<T*> : public Any
101 {
102 protected:
103         T*      m_value;
104
105 public:
106         explicit Any_Common(const T* value) { m_value = value; }
107         virtual ~Any_Common() {}
108
109         T* getValue() const { return m_value; }
110         void setValue(const T* value) { m_value = value; }
111
112         virtual std::string toString() const = 0;
113
114         virtual JSValueRef toJSValueRef(JSContextRef context) const
115         {
116                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
117
118                 return converter.toJSValueRef(&m_value);
119         }
120 };
121
122 template<typename T>
123 class Any_T : public Any_Common<T>
124 {
125 public:
126         explicit Any_T(const T& value) : Any_Common<T>(value) {}
127         virtual PrimitiveType getType() const { return PrimitiveType_Other; }
128
129         virtual std::string toString() const
130         {
131                 return "";
132         }
133 };
134
135 template<>
136 class Any_T<bool> : public Any_Common<bool>
137 {
138 public:
139         explicit Any_T(const bool& value) : Any_Common<bool>(value) {}
140         virtual PrimitiveType getType() const { return PrimitiveType_Boolean; }
141
142         virtual std::string toString() const
143         {
144                 if(m_value)
145                         return "true";
146                 else
147                         return "false";
148         }
149 };
150
151 template<>
152 class Any_T<char> : public Any_Common<char>
153 {
154 public:
155         explicit Any_T(const char& value) : Any_Common<char>(value) {}
156         virtual PrimitiveType getType() const { return PrimitiveType_Char; }
157
158         virtual std::string toString() const
159         {
160                 std::stringstream oss;
161                 oss << m_value;
162                 return oss.str();
163         }
164 };
165
166 template<>
167 class Any_T<unsigned char> : public Any_Common<unsigned char>
168 {
169 public:
170         explicit Any_T(const unsigned char& value) : Any_Common<unsigned char>(value) {}
171         virtual PrimitiveType getType() const { return PrimitiveType_UChar; }
172
173         virtual std::string toString() const
174         {
175                 std::stringstream oss;
176                 oss << m_value;
177                 return oss.str();
178         }
179 };
180
181 template<>
182 class Any_T<int> : public Any_Common<int>
183 {
184 public:
185         explicit Any_T(const int& value) : Any_Common<int>(value) {}
186         virtual PrimitiveType getType() const { return PrimitiveType_Int; }
187
188         virtual std::string toString() const
189         {
190                 std::stringstream oss;
191                 oss << m_value;
192                 return oss.str();
193         }
194 };
195
196 template<>
197 class Any_T<unsigned int> : public Any_Common<unsigned int>
198 {
199 public:
200         explicit Any_T(const unsigned int& value) : Any_Common<unsigned int>(value) {}
201         virtual PrimitiveType getType() const { return PrimitiveType_UChar; }
202
203         virtual std::string toString() const
204         {
205                 std::stringstream oss;
206                 oss << m_value;
207                 return oss.str();
208         }
209 };
210
211 template<>
212 class Any_T<long> : public Any_Common<long>
213 {
214 public:
215         explicit Any_T(const long& value) : Any_Common<long>(value) {}
216         virtual PrimitiveType getType() const { return PrimitiveType_Long; }
217
218         virtual std::string toString() const
219         {
220                 std::stringstream oss;
221                 oss << m_value;
222                 return oss.str();
223         }
224 };
225
226 template<>
227 class Any_T<unsigned long> : public Any_Common<unsigned long>
228 {
229 public:
230         explicit Any_T(const unsigned long& value) : Any_Common<unsigned long>(value) {}
231         virtual PrimitiveType getType() const { return PrimitiveType_ULong; }
232
233         virtual std::string toString() const
234         {
235                 std::stringstream oss;
236                 oss << m_value;
237                 return oss.str();
238         }
239 };
240
241 template<>
242 class Any_T<double> : public Any_Common<double>
243 {
244 public:
245         explicit Any_T(const double& value) : Any_Common<double>(value) {}
246         virtual PrimitiveType getType() const { return PrimitiveType_Double; }
247
248         virtual std::string toString() const
249         {
250                 std::stringstream oss;
251                 oss << m_value;
252                 return oss.str();
253         }
254 };
255
256 template<>
257 class Any_T<std::string> : public Any_Common<std::string>
258 {
259 public:
260         explicit Any_T(const std::string& value) : Any_Common<std::string>(value) {}
261         virtual PrimitiveType getType() const { return PrimitiveType_String; }
262
263         virtual std::string toString() const
264         {
265                 return m_value;
266         }
267 };
268
269 template<>
270 class Any_T<tm> : public Any_Common<tm>
271 {
272 public:
273         explicit Any_T(const tm& value) : Any_Common<tm>(value) {}
274         virtual PrimitiveType getType() const { return PrimitiveType_Time; }
275
276         virtual std::string toString() const
277         {
278                 std::stringstream oss;
279
280                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(4) << (m_value.tm_year + 1900);
281                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << (m_value.tm_mon + 1);
282                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.tm_mday;
283                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.tm_hour;
284                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.tm_min;
285                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.tm_sec;
286
287                 return oss.str();
288         }
289 };
290
291 class AnyTypeConverter : public WrtDeviceApis::CommonsJavaScript::Converter
292 {
293 public:
294         explicit AnyTypeConverter(JSContextRef context);
295         virtual ~AnyTypeConverter();
296
297         JSValueRef toJSValueRef(const AnyPtr& arg);
298         AnyPtr toAny(const JSValueRef &value, const PrimitiveType preferredNumberType=PrimitiveType_Double);
299
300         AnyPtr toAny(const bool &value);
301         AnyPtr toAny(const char &value);
302         AnyPtr toAny(const unsigned char &value);
303         AnyPtr toAny(const int &value);
304         AnyPtr toAny(const unsigned int &value);
305         AnyPtr toAny(const long &value);
306         AnyPtr toAny(const unsigned long &value);
307         AnyPtr toAny(const double &value);
308         AnyPtr toAny(const std::string &value);
309         AnyPtr toAny(const tm &value);
310
311         bool toBool(const AnyPtr& arg);
312         char toChar(const AnyPtr& arg);
313         unsigned char toUChar(const AnyPtr& arg);
314         int toInt(const AnyPtr& arg);
315         unsigned int toUInt(const AnyPtr& arg);
316         long toLong(const AnyPtr& arg);
317         unsigned long toULong(const AnyPtr& arg);
318         double toDouble(const AnyPtr& arg);
319         std::string toString(const AnyPtr& arg);
320         tm toDateTm(const AnyPtr& arg);
321
322         using WrtDeviceApis::CommonsJavaScript::Converter::toJSValueRef;
323         using WrtDeviceApis::CommonsJavaScript::Converter::toString;
324         using WrtDeviceApis::CommonsJavaScript::Converter::toDateTm;
325
326 protected:
327         bool isDate(const JSValueRef& arg);
328 };
329
330 typedef WrtDeviceApis::CommonsJavaScript::ConverterFactory<AnyTypeConverter> AnyTypeConverterFactory;
331 // TODO How about processing Any types with JSON???
332
333 typedef std::vector<AnyPtr> AnyArray;
334 typedef DPL::SharedPtr<AnyArray> AnyArrayPtr;
335
336 } // Tizen
337 } // Api
338 } // TizenApis
339
340 #endif // _API_ANYTYPE_H_