2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Declaration of the JSFilter class
24 #ifndef _API_ANYTYPE_H_
25 #define _API_ANYTYPE_H_
30 #include <dpl/shared_ptr.h>
31 #include <CommonsJavaScript/Converter.h>
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)
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 )
73 virtual std::string toString() const = 0;
74 virtual PrimitiveType getType() const = 0;
77 typedef DPL::SharedPtr<Any> AnyPtr;
80 class Any_Common : public Any
86 explicit Any_Common(const T& value) { m_value = value; };
87 virtual ~Any_Common() {}
89 virtual T getValue() const { return m_value; }
90 virtual void setValue(const T& value) { m_value = value; }
92 virtual JSValueRef toJSValueRef(JSContextRef context) const
94 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
95 return converter.toJSValueRef(m_value);
100 class Any_Common<T*> : public Any
106 explicit Any_Common(const T* value) { m_value = value; }
107 virtual ~Any_Common() {}
109 T* getValue() const { return m_value; }
110 void setValue(const T* value) { m_value = value; }
112 virtual std::string toString() const = 0;
114 virtual JSValueRef toJSValueRef(JSContextRef context) const
116 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
118 return converter.toJSValueRef(&m_value);
123 class Any_T : public Any_Common<T>
126 explicit Any_T(const T& value) : Any_Common<T>(value) {}
127 virtual PrimitiveType getType() const { return PrimitiveType_Other; }
129 virtual std::string toString() const
136 class Any_T<bool> : public Any_Common<bool>
139 explicit Any_T(const bool& value) : Any_Common<bool>(value) {}
140 virtual PrimitiveType getType() const { return PrimitiveType_Boolean; }
142 virtual std::string toString() const
152 class Any_T<char> : public Any_Common<char>
155 explicit Any_T(const char& value) : Any_Common<char>(value) {}
156 virtual PrimitiveType getType() const { return PrimitiveType_Char; }
158 virtual std::string toString() const
160 std::stringstream oss;
167 class Any_T<unsigned char> : public Any_Common<unsigned char>
170 explicit Any_T(const unsigned char& value) : Any_Common<unsigned char>(value) {}
171 virtual PrimitiveType getType() const { return PrimitiveType_UChar; }
173 virtual std::string toString() const
175 std::stringstream oss;
182 class Any_T<int> : public Any_Common<int>
185 explicit Any_T(const int& value) : Any_Common<int>(value) {}
186 virtual PrimitiveType getType() const { return PrimitiveType_Int; }
188 virtual std::string toString() const
190 std::stringstream oss;
197 class Any_T<unsigned int> : public Any_Common<unsigned int>
200 explicit Any_T(const unsigned int& value) : Any_Common<unsigned int>(value) {}
201 virtual PrimitiveType getType() const { return PrimitiveType_UChar; }
203 virtual std::string toString() const
205 std::stringstream oss;
212 class Any_T<long> : public Any_Common<long>
215 explicit Any_T(const long& value) : Any_Common<long>(value) {}
216 virtual PrimitiveType getType() const { return PrimitiveType_Long; }
218 virtual std::string toString() const
220 std::stringstream oss;
227 class Any_T<unsigned long> : public Any_Common<unsigned long>
230 explicit Any_T(const unsigned long& value) : Any_Common<unsigned long>(value) {}
231 virtual PrimitiveType getType() const { return PrimitiveType_ULong; }
233 virtual std::string toString() const
235 std::stringstream oss;
242 class Any_T<double> : public Any_Common<double>
245 explicit Any_T(const double& value) : Any_Common<double>(value) {}
246 virtual PrimitiveType getType() const { return PrimitiveType_Double; }
248 virtual std::string toString() const
250 std::stringstream oss;
257 class Any_T<std::string> : public Any_Common<std::string>
260 explicit Any_T(const std::string& value) : Any_Common<std::string>(value) {}
261 virtual PrimitiveType getType() const { return PrimitiveType_String; }
263 virtual std::string toString() const
270 class Any_T<tm> : public Any_Common<tm>
273 explicit Any_T(const tm& value) : Any_Common<tm>(value) {}
274 virtual PrimitiveType getType() const { return PrimitiveType_Time; }
276 virtual std::string toString() const
278 std::stringstream oss;
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;
291 class AnyTypeConverter : public WrtDeviceApis::CommonsJavaScript::Converter
294 explicit AnyTypeConverter(JSContextRef context);
295 virtual ~AnyTypeConverter();
297 JSValueRef toJSValueRef(const AnyPtr& arg);
298 AnyPtr toAny(const JSValueRef &value, const PrimitiveType preferredNumberType=PrimitiveType_Double);
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);
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);
322 using WrtDeviceApis::CommonsJavaScript::Converter::toJSValueRef;
323 using WrtDeviceApis::CommonsJavaScript::Converter::toString;
324 using WrtDeviceApis::CommonsJavaScript::Converter::toDateTm;
327 bool isDate(const JSValueRef& arg);
330 typedef WrtDeviceApis::CommonsJavaScript::ConverterFactory<AnyTypeConverter> AnyTypeConverterFactory;
331 // TODO How about processing Any types with JSON???
333 typedef std::vector<AnyPtr> AnyArray;
334 typedef DPL::SharedPtr<AnyArray> AnyArrayPtr;
340 #endif // _API_ANYTYPE_H_