Beta merge 2
[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
38 #define _PT_UNDEFINED   (0)
39 #define _PT_NULL                (1 << 0)
40 #define _PT_BOOLEAN             (1 << 1)
41 #define _PT_CHAR                (1 << 2)
42 #define _PT_UCHAR               (1 << 3)
43 #define _PT_INT                 (1 << 4)
44 #define _PT_UINT                (1 << 5)
45 #define _PT_LONG                (1 << 6)
46 #define _PT_ULONG               (1 << 7)
47 #define _PT_DOUBLE              (1 << 8)
48 #define _PT_STRING              (1 << 9)
49 #define _PT_TIME                (1 << 10)
50 #define _PT_OBJECT              (1 << 11)
51
52 enum PrimitiveType {
53         PrimitiveType_NoType    = 0,
54         PrimitiveType_Undefined = _PT_UNDEFINED,
55         PrimitiveType_Null              = _PT_NULL,
56         PrimitiveType_Boolean   = _PT_BOOLEAN,
57         PrimitiveType_Char              = _PT_CHAR,
58         PrimitiveType_UChar             = _PT_UCHAR,
59         PrimitiveType_Int               = _PT_INT,
60         PrimitiveType_UInt              = _PT_UINT,
61         PrimitiveType_Long              = _PT_LONG,
62         PrimitiveType_ULong             = _PT_ULONG,
63         PrimitiveType_Double    = _PT_DOUBLE,
64         PrimitiveType_String    = _PT_STRING,
65         PrimitiveType_Time              = _PT_TIME,
66         PrimitiveType_Object    = _PT_OBJECT,
67         PrimitiveType_Number    = (_PT_CHAR | _PT_UCHAR | _PT_INT | _PT_UINT |
68                                                                 _PT_LONG | _PT_ULONG | _PT_DOUBLE),
69         PrimitiveType_Integer   = (_PT_CHAR | _PT_UCHAR | _PT_INT | _PT_UINT |
70                                                                 _PT_LONG | _PT_ULONG),
71         PrimitiveType_Any               = (_PT_BOOLEAN | _PT_CHAR | _PT_UCHAR | _PT_INT |
72                                                                 _PT_UINT | _PT_LONG | _PT_ULONG | _PT_DOUBLE |
73                                                                 _PT_STRING | _PT_TIME | _PT_OBJECT )
74 };
75
76 class Any
77 {
78 private:
79         union AnyTypeUnion
80         {
81                 bool b;
82                 double d;
83                 std::tm t;
84                 std::string* s;
85                 int i;
86         };
87
88         unsigned int m_type;
89
90         AnyTypeUnion m_value;
91
92         std::string m_json;
93
94         void * m_priv;
95
96 public:
97         Any();
98         Any(bool value);
99         Any(double value, std::string json);
100         Any(std::string value);
101         Any(std::tm value, std::string json);
102         Any(void * priv, std::string json);
103         virtual ~Any();
104
105         virtual std::string toString() const;
106         std::string toJSON() const;
107
108         unsigned int getType() const;
109         bool isType(PrimitiveType type) const;
110
111         bool getBool() const;
112         char getChar() const;
113         unsigned char getUChar() const;
114         int getInt() const;
115         unsigned int getUInt() const;
116         long getLong() const;
117         unsigned long getULong() const;
118         double getDouble() const;
119         std::string getString() const;
120         std::tm getDateTm() const;
121         std::time_t getTimeT() const;
122 };
123 typedef DPL::SharedPtr<Any> AnyPtr;
124
125 typedef std::vector<AnyPtr> AnyArray;
126 typedef DPL::SharedPtr<AnyArray> AnyArrayPtr;
127
128 class AnyTypeConverter : public WrtDeviceApis::CommonsJavaScript::Converter
129 {
130 public:
131         explicit AnyTypeConverter(JSContextRef context);
132         virtual ~AnyTypeConverter();
133
134         JSValueRef toJSValueRef(const AnyPtr& arg);
135         AnyPtr toAny(const JSValueRef &value);
136
137         bool toBool(const AnyPtr& arg);
138         char toChar(const AnyPtr& arg);
139         unsigned char toUChar(const AnyPtr& arg);
140         int toInt(const AnyPtr& arg);
141         unsigned int toUInt(const AnyPtr& arg);
142         long toLong(const AnyPtr& arg);
143         unsigned long toULong(const AnyPtr& arg);
144         double toDouble(const AnyPtr& arg);
145         std::string toString(const AnyPtr& arg);
146         tm toDateTm(const AnyPtr& arg);
147         time_t toTimeT(const AnyPtr& arg);
148
149         using WrtDeviceApis::CommonsJavaScript::Converter::toJSValueRef;
150         using WrtDeviceApis::CommonsJavaScript::Converter::toString;
151         using WrtDeviceApis::CommonsJavaScript::Converter::toDateTm;
152
153 protected:
154         bool isDate(const JSValueRef& arg);
155 };
156
157 typedef WrtDeviceApis::CommonsJavaScript::ConverterFactory<AnyTypeConverter> AnyTypeConverterFactory;
158
159 } // Tizen
160 } // Api
161 } // TizenApis
162
163 #endif // _API_ANYTYPE_H_