Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Filter / AnyType.cpp
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.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Declaration of the JSFilter class
22  */
23
24 #include <limits>
25 #include <sstream>
26 #include <Commons/Exception.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
28 #include "AnyType.h"
29
30 namespace TizenApis {
31 namespace Api {
32 namespace Tizen {
33
34 using namespace WrtDeviceApis::Commons;
35 using namespace WrtDeviceApis::CommonsJavaScript;
36
37
38 Any::Any()
39 {
40         m_type = PrimitiveType_Null;
41         m_value.i = 0;
42         m_json = "null";
43         m_priv = NULL;
44 }
45
46 Any::Any(bool value)
47 {
48         m_type = PrimitiveType_Boolean;
49         m_value.b = value;
50         m_json = value?"true":"false";
51         m_priv = NULL;
52 }
53
54 Any::Any(double value, std::string json)
55 {
56         m_type = PrimitiveType_Double;
57         m_value.d = value;
58         m_json = json;
59         m_priv = NULL;
60
61         if(value == (double)((char)value))
62                 m_type |= PrimitiveType_Char;
63         if(value == (double)((unsigned char)value))
64                 m_type |= PrimitiveType_UChar;
65         if(value == (double)((int)value))
66                 m_type |= PrimitiveType_Int;
67         if(value == (double)((unsigned int)value))
68                 m_type |= PrimitiveType_UInt;
69         if(value == (double)((long)value))
70                 m_type |= PrimitiveType_Long;
71         if(value == (double)((unsigned long)value))
72                 m_type |= PrimitiveType_ULong;
73 }
74
75 Any::Any(std::string value)
76 {
77         m_type = PrimitiveType_String;
78         m_value.s = new std::string(value);
79         m_json = "\"" + value + "\"";
80         m_priv = NULL;
81 }
82
83 Any::Any(std::tm value, std::string json)
84 {
85         m_type = PrimitiveType_Time;
86         m_value.t = value;
87         m_json = json;
88         m_priv = NULL;
89 }
90
91 Any::Any(void * priv, std::string json)
92 {
93         m_type = PrimitiveType_Object;
94         m_value.i = 0;
95         m_json = json;
96         m_priv = priv;
97 }
98
99 Any::~Any()
100 {
101         if(m_type == PrimitiveType_String)
102                 delete m_value.s;
103 }
104
105 std::string Any::toString() const
106 {
107         std::stringstream oss;
108
109         if(m_type == PrimitiveType_Undefined || m_type == PrimitiveType_Null)
110                 return "";
111         else if(m_type == PrimitiveType_Boolean)
112                 return m_json;
113         else if(m_type == PrimitiveType_String)
114                 return *m_value.s;
115         else if(m_type == PrimitiveType_Object)
116                 return m_json;
117         else if(m_type | PrimitiveType_Number)
118         {
119                 if(m_type | PrimitiveType_ULong)
120                         oss << (unsigned long)m_value.d;
121                 else if(m_type | PrimitiveType_Long)
122                         oss << (long)m_value.d;
123                 else
124                 {
125                         oss << std::setprecision(std::numeric_limits<double>::digits10);
126                         oss << std::setiosflags(std::ios::fixed);
127                         oss << m_value.d;
128                 }
129
130                 return oss.str();
131         }
132         else if(m_type == PrimitiveType_Time)
133         {
134                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(4) << (m_value.t.tm_year + 1900);
135                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << (m_value.t.tm_mon + 1);
136                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_mday;
137                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_hour;
138                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_min;
139                 oss << std::setfill('0') << std::setiosflags(std::ios::right) << std::setw(2) << m_value.t.tm_sec;
140
141                 return oss.str();
142         }
143         else
144                 return m_json;
145 }
146
147 std::string Any::toJSON() const
148 {
149         return m_json;
150 }
151
152 unsigned int Any::getType() const
153 {
154         return m_type;
155 }
156
157 bool Any::isType(PrimitiveType type) const
158 {
159         return (bool)(m_type & type);
160 }
161
162 bool Any::getBool() const
163 {
164         if(m_type != PrimitiveType_Boolean)
165                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
166
167         return m_value.b;
168 }
169
170 char Any::getChar() const
171 {
172         if(!(m_type & PrimitiveType_Char))
173                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
174
175         return (char)m_value.d;
176 }
177
178 unsigned char Any::getUChar() const
179 {
180         if(!(m_type & PrimitiveType_UChar))
181                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
182
183         return (unsigned char)m_value.d;
184 }
185
186 int Any::getInt() const
187 {
188         if(!(m_type & PrimitiveType_Int))
189                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
190
191         return (int)m_value.d;
192 }
193
194 unsigned int Any::getUInt() const
195 {
196         if(!(m_type & PrimitiveType_UInt))
197                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
198
199         return (unsigned int)m_value.d;
200 }
201
202 long Any::getLong() const
203 {
204         if(!(m_type & PrimitiveType_Long))
205                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
206
207         return (long)m_value.d;
208 }
209
210 unsigned long Any::getULong() const
211 {
212         if(!(m_type & PrimitiveType_ULong))
213                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
214
215         return (unsigned long)m_value.d;
216 }
217
218 double Any::getDouble() const
219 {
220         if(!(m_type & PrimitiveType_Double))
221                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
222
223         return m_value.d;
224 }
225
226 std::string Any::getString() const
227 {
228         if(m_type != PrimitiveType_String)
229                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
230
231         return *m_value.s;
232 }
233
234 std::tm Any::getDateTm() const
235 {
236         if(m_type != PrimitiveType_Time)
237                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
238
239         return m_value.t;
240 }
241
242 std::time_t Any::getTimeT() const
243 {
244         if(m_type != PrimitiveType_Time)
245                 ThrowMsg(InvalidArgumentException, "Type mismatch.");
246
247         std::tm *time = const_cast<std::tm*>(&(m_value.t));
248         return std::mktime(time);
249 }
250
251 AnyTypeConverter::AnyTypeConverter(JSContextRef context) :
252                 Converter(context)
253 {
254 }
255
256 AnyTypeConverter::~AnyTypeConverter()
257 {
258 }
259
260 JSValueRef AnyTypeConverter::toJSValueRef(const AnyPtr& arg)
261 {
262         if(arg->isType(PrimitiveType_Undefined))
263                 return JSValueMakeUndefined(m_context);
264         else if(arg->isType(PrimitiveType_Null))
265                 return JSValueMakeNull(m_context);
266         else if(arg->isType(PrimitiveType_Boolean))
267                 return toJSValueRef(arg->getBool());
268         else if(arg->isType(PrimitiveType_Number))
269                 return toJSValueRef(arg->getDouble());
270         else if(arg->isType(PrimitiveType_String))
271                 return toJSValueRef(arg->getString());
272         else if(arg->isType(PrimitiveType_Time))
273                 return toJSValueRef(arg->getDateTm());
274         else if(arg->isType(PrimitiveType_Object))
275         {
276                 ScopedJSStringRef json(JSStringCreateWithUTF8CString(arg->toJSON().c_str()));
277                 return JSValueMakeFromJSONString(m_context, json.get());
278         }
279
280         return JSValueMakeUndefined(m_context);
281 }
282
283 AnyPtr AnyTypeConverter::toAny(const JSValueRef &value)
284 {
285         JSType jstype = JSValueGetType(m_context, value);
286         ScopedJSStringRef json(JSValueCreateJSONString(m_context, value, 0, NULL));
287
288         if(jstype == kJSTypeBoolean)
289                 return AnyPtr(new Any(Converter::toBool(value)));
290         else if(jstype == kJSTypeNumber)
291                 return AnyPtr(new Any(Converter::toDouble(value), toString(json.get())));
292         else if(jstype == kJSTypeString)
293                 return AnyPtr(new Any(Converter::toString(value)));
294         else if(jstype == kJSTypeObject)
295         {
296                 if(isDate(value))
297                         return AnyPtr(new Any(Converter::toDateTm(value), toString(json.get())));
298                 else
299                 {
300                         void *priv = NULL;
301                         JSObjectRef object = JSValueToObject(m_context, value, NULL);
302                         if (object)
303                                 priv = JSObjectGetPrivate(object);
304
305                         return AnyPtr(new Any(priv, toString(json.get())));
306                 }
307         }
308         else
309                 return AnyPtr(new Any());
310 }
311
312 bool AnyTypeConverter::toBool(const AnyPtr& arg)
313 {
314         return arg->getBool();
315 }
316
317 char AnyTypeConverter::toChar(const AnyPtr& arg)
318 {
319         return arg->getChar();
320 }
321
322 unsigned char AnyTypeConverter::toUChar(const AnyPtr& arg)
323 {
324         return arg->getUChar();
325 }
326
327 int AnyTypeConverter::toInt(const AnyPtr& arg)
328 {
329         return arg->getInt();
330 }
331
332 unsigned int AnyTypeConverter::toUInt(const AnyPtr& arg)
333 {
334         return arg->getUInt();
335 }
336
337 long AnyTypeConverter::toLong(const AnyPtr& arg)
338 {
339         return arg->getLong();
340 }
341
342 unsigned long AnyTypeConverter::toULong(const AnyPtr& arg)
343 {
344         return arg->getULong();
345 }
346
347 double AnyTypeConverter::toDouble(const AnyPtr& arg)
348 {
349         return arg->getDouble();
350 }
351
352 std::string AnyTypeConverter::toString(const AnyPtr& arg)
353 {
354         return arg->getString();
355 }
356
357 tm AnyTypeConverter::toDateTm(const AnyPtr& arg)
358 {
359         return arg->getDateTm();
360 }
361
362 time_t AnyTypeConverter::toTimeT(const AnyPtr& arg)
363 {
364         return arg->getTimeT();
365 }
366
367 bool AnyTypeConverter::isDate(const JSValueRef& arg)
368 {
369         // this method originated from TizenApis::Commons::Validator::isDate()
370         if ( JSValueIsNull( m_context, arg ) || JSValueIsUndefined( m_context, arg ) || !JSValueIsObject( m_context, arg ) )
371                 return false;
372
373         Try {
374                 toDateTm( arg );
375         } Catch(Exception) {
376                 return false;
377         }
378         return true;
379 }
380
381 } // Tizen
382 } // Api
383 } // TizenApis