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.
16 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_
24 #include <JavaScriptCore/JavaScript.h>
25 #include <dpl/noncopyable.h>
26 #include <dpl/shared_ptr.h>
27 #include <Commons/Exception.h>
28 #include <Commons/StaticAssert.h>
30 namespace WrtDeviceApis {
31 namespace CommonsJavaScript {
33 class Converter : private DPL::Noncopyable
36 explicit Converter(JSContextRef context);
40 * Converts JSValueRef to JSObjectRef.
41 * @param arg JSValueRef object to convert.
42 * @return JSObjectRef object.
43 * @throw ConversionException Thrown when conversion fails.
45 JSObjectRef toJSObjectRef(const JSValueRef& arg);
48 * Converts JSValueRef object to boolean value.
49 * @param arg JSValueRef object to convert.
50 * @return Boolean value.
51 * @throw ConversionException Thrown when conversion fails.
53 bool toBool(const JSValueRef& arg);
56 * Converts JSValueRef object to unsigned char value.
57 * @param arg JSValueRef object to convert.
58 * @return Unsigned char value.
59 * @throw ConversionException Thrown when conversion fails.
61 unsigned char toUChar(const JSValueRef& arg);
64 * Converts JSValueRef object to char value.
65 * @param arg JSValueRef object to convert.
67 * @throw ConversionException Thrown when conversion fails.
69 char toChar(const JSValueRef& arg);
72 * Converts JSValueRef to integer value.
73 * @param arg JSValueRef object to convert.
74 * @return Integer value.
75 * @throw ConversionException Thrown when conversion fails.
77 int toInt(const JSValueRef& arg);
80 * Converts string to integer value.
81 * @param arg string to convert.
82 * @return Integer value.
83 * @throw ConversionException Thrown when conversion fails.
85 int toInt(const std::string& arg);
88 * Converts JSvalueRef to long integer value.
89 * @param arg JSValueRef object to convert.
90 * @return Long integer value.
91 * @throw ConversionException Thrown when conversion fails.
93 long toLong(const JSValueRef& arg);
96 * Converts JSvalueRef to long long integer value.
97 * @param arg JSValueRef object to convert.
98 * @return Long long integer value.
99 * @throw ConversionException Thrown when conversion fails.
101 long long toLongLong(const JSValueRef& arg);
103 * Converts JSValueRef to unsigned long value.
104 * @param arg JSValueRef object to convert.
105 * @return Unsigned long value.
106 * @throw ConversionException Thrown when conversion fails.
108 unsigned long toULong(const JSValueRef& arg);
111 * Converts JSValueRef to double value.
112 * @param arg JSValueRef object to convert.
113 * @return Double value.
114 * @throw ConversionException Thrown when conversion fails.
116 double toDouble(const JSValueRef& arg);
119 * Converts JSValueRef to size_t value.
120 * @param arg JSValueRef object to convert.
121 * @return Size_t value.
122 * @throw ConversionException Thrown when conversion fails.
124 std::size_t toSizeT(const JSValueRef& arg);
127 * Converts JSStringRef to size_t value.
128 * @param arg JSStringRef object to convert.
129 * @return Size_t value.
130 * @throw ConversionException Thrown when conversion fails.
132 std::size_t toSizeT(const JSStringRef& arg);
135 * Converts int to STL string.
136 * @param arg int to convert.
137 * @return STL string.
138 * @throw ConversionException Thrown when conversion fails.
140 std::string toString(int arg);
142 std::string toString(unsigned long arg);
144 std::string toString(long arg);
146 std::string toString(std::size_t arg);
149 * Converts JSValueRef to STL string.
150 * @param arg JSValueRef to convert.
151 * @return STL string.
152 * @throw ConversionException Thrown when conversion fails.
153 * @remarks If one wants to convert JS Date object to a string then
154 * toDateString() should be used.
156 std::string toString(const JSValueRef& arg);
159 * Converts JSStringRef to STL string.
160 * @param arg JSStringRef to convert.
161 * @return STL string.
162 * @throw ConversionException Thrown when conversion fails.
164 std::string toString(const JSStringRef& arg);
167 * Converts JSSValueRef to STL string if
168 * arg is one of: String, Number, Bool
169 * @param arg JSValueReg to convert.
170 * @return STL string.
171 * @throw ConversionException Thrown when conversion fails.
173 std::string tryString(const JSValueRef& arg);
176 * Converts JS Date object to STL string.
177 * @param arg JS Date object to convert.
178 * @return STL string.
179 * @throw ConversionException Thrown when conversion fails.
181 std::string toDateString(const JSValueRef& arg);
184 * Converts JSValueRef to time_t value.
185 * @param arg JSValueRef to convert.
186 * @return time_t value.
187 * @throw ConversionException Thrown when conversion fails.
189 time_t toDateTimeT(const JSValueRef& arg);
192 * Converts JSValueRef to tm struct.
193 * @param arg JSValueRef to convert.
195 * @throw ConversionException Thrown when conversion fails.
197 tm toDateTm(const JSValueRef& arg);
200 * Converts JSValueRef to vector<unsigned char>.
201 * @param arg JSValueRef to convert.
202 * @return vector<unsigned char>.
203 * @throw ConversionException Thrown when conversion fails.
205 std::vector<unsigned char> toVectorOfUChars(const JSValueRef& arg);
208 * Converts JSValueRef to vector<char>.
209 * @param arg JSValueRef to convert.
210 * @return vector<char>.
211 * @throw ConversionException Thrown when conversion fails.
213 std::vector<char> toVectorOfChars(const JSValueRef& arg);
216 * Converts JSValueRef to vector<int>.
217 * @param arg JSValueRef to convert.
218 * @return vector<int>.
219 * @throw ConversionException Thrown when conversion fails.
221 std::vector<int> toVectorOfInts(const JSValueRef& arg);
224 * Converts JSValueRef to vector of string.
225 * @param arg JSValueRef to convert.
226 * @return std::vector<std::string>
227 * @throw ConversionException Thrown when conversion fails.
229 std::vector<std::string> toVectorOfStrings(const JSValueRef& arg);
232 * Converts JSValueRef to vector of time_t.
233 * @param arg JSValueRef to convert.
234 * @return std::vector<std::time_t>
235 * @throw ConversionException Thrown when conversion fails.
237 std::vector<std::time_t> toVectorOfTimeT(const JSValueRef& arg);
240 * Converts boolean value to JSValueRef.
241 * @param arg Boolean value to convert.
242 * @return JSValueRef object.
243 * @throw ConversionException Thrown when conversion fails.
245 JSValueRef toJSValueRef(bool arg);
248 * Converts integer value to JSValueRef.
249 * @param arg Integer value to convert.
250 * @return JSValueRef object.
251 * @throw ConversionException Thrown when conversion fails.
253 JSValueRef toJSValueRef(int arg);
256 * Converts unsigned integer value to JSValueRef.
257 * @param arg Unsigned integer value to convert.
258 * @return JSValueRef object.
259 * @throw ConversionException Thrown when conversion fails.
261 JSValueRef toJSValueRef(unsigned int arg);
264 * Converts double value to JSValueRef.
265 * @param arg Double value to convert.
266 * @return JSValueRef object.
267 * @throw ConversionException Thrown when conversion fails.
269 JSValueRef toJSValueRef(double arg);
272 * Converts unsigned long value to JSValueRef.
273 * @param arg unsigned long value to convert.
274 * @return JSValueRef object.
275 * @throw ConversionException Thrown when conversion fails.
277 JSValueRef toJSValueRef(unsigned long arg);
280 * Converts long value to JSValueRef.
281 * @param arg long value to convert.
282 * @return JSValueRef object.
283 * @throw ConversionException Thrown when conversion fails.
285 JSValueRef toJSValueRefLong(const long arg);
288 * Converts unsigned long value to JSValueRef.
289 * @param arg long long int value to convert.
290 * @return JSValueRef object.
291 * @throw ConversionException Thrown when conversion fails.
293 JSValueRef toJSValueRef(long long int arg);
296 * Converts STL string to JSValueRef.
297 * @param arg STL string to convert.
298 * @return JSValueRef object.
299 * @throw ConversionException Thrown when conversion fails.
301 JSValueRef toJSValueRef(const std::string& arg);
304 * Converts char sequence to JSValueRef.
305 * @param arg char sequence to convert.
306 * @return JSValueRef object.
307 * @throw ConversionException Thrown when conversion fails.
309 JSValueRef toJSValueRef(const char* arg);
312 * Converts time_t value to JSValueRef.
313 * @param arg time_t value to convert.
314 * @return JSValueRef object.
315 * @throw ConversionException Thrown when conversion fails.
317 JSValueRef toJSValueRef(const time_t arg);
320 * Converts tm struct to JSValueRef.
321 * @param arg tm struct to convert.
322 * @return JSValueRef object.
323 * @throw ConversionException Thrown when conversion fails.
325 JSValueRef toJSValueRef(const tm& arg);
328 * Converts STL vector of integer to JSValueRef.
329 * @param arg STL vector of integer to convert.
330 * @return JSValueRef object.
331 * @throw ConversionException Thrown when conversion fails.
333 JSValueRef toJSValueRef(const std::vector<int>& arg);
336 * Converts STL vector of time_t to JSValueRef.
337 * @param arg STL vector of time_t to convert.
338 * @return JSValueRef object.
339 * @throw ConversionException Thrown when conversion fails.
341 JSValueRef toJSValueRef(const std::vector<time_t>& arg);
344 * Converts STL vector of string to JSStringRef.
345 * @param arg STL string to convert.
346 * @return JSValueRef object.
347 * @throw ConversionException Thrown when conversion fails.
349 JSValueRef toJSValueRef(const std::vector<std::string>& arg);
352 * Converts JSValueRef to JSValueRef
353 * @param JSValueRef js value which is returned
354 * @return JSValueRef - parameter
355 * @throw ConversionException Thrown when conversion fails.
357 JSValueRef toJSValueRef(JSValueRef arg);
360 * Converts std::vector to JSValueRef
361 * @param arg vecotr of JSValueRefs
362 * @return JSValueRef - js array
363 * @throw ConversionException Thrown when conversion fails.
365 JSValueRef toJSValueRef(const std::vector<JSValueRef>& arg);
368 * Template specialization to catch errors caused
369 * by wrong usage of general template or
370 * implicit conversions from SharedPtr to
373 template<class Class>
374 JSValueRef toJSValueRef(const DPL::SharedPtr<Class>& c)
376 STATIC_ERROR(ERROR_INVALID_IMPLICIT_CONVERSION, c)
377 //above macro causes compilation error
378 return static_cast<JSValueRef>(NULL);
382 * Converts STL string to JSStringRef.
383 * @param arg STL string to convert.
384 * @return JSStringRef object.
385 * @throw ConversionException Thrown when conversion fails.
387 JSStringRef toJSStringRef(const std::string& arg);
390 bool isNan(double value) const;
392 double toNumber_(const JSValueRef& arg);
394 double toNumber_(const JSStringRef& arg);
396 double toNumber_(const std::string& arg);
398 template<typename T, class Derived>
399 std::vector<T> toVectorOfT_(const JSValueRef& arg,
400 T (Derived::*f)(const JSValueRef &),
403 if (JSValueIsNull(m_context,
404 arg) || JSValueIsUndefined(m_context, arg)) {
405 return std::vector<T>();
408 if (!JSIsArrayValue(m_context, arg)) {
409 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
412 std::vector<T> result;
413 JSObjectRef objArg = toJSObjectRef(arg);
414 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
415 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
416 result.push_back((object->*f)(element));
422 std::vector<T> toVectorOfT_(const JSValueRef& arg,
423 T (Converter::*f)(const JSValueRef &))
425 if (JSValueIsNull(m_context,
426 arg) || JSValueIsUndefined(m_context, arg)) {
427 return std::vector<T>();
430 if (!JSIsArrayValue(m_context, arg)) {
431 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
434 std::vector<T> result;
435 JSObjectRef objArg = toJSObjectRef(arg);
436 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
437 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
438 result.push_back((this->*f)(element));
443 template<class T, class Derived>
444 JSValueRef toJSValueRef_(const std::vector<T>& arg,
445 JSValueRef (Derived::*f)(const T &),
448 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
450 if (NULL == jsResult) {
451 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Could not create js array object");
454 for (std::size_t i = 0; i < arg.size(); ++i) {
455 JSValueRef tmpVal = (object->*f)(arg[i]);
456 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
457 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
458 "Could not insert value into js array");
466 JSValueRef toJSValueRef_(const std::vector<T>& arg)
468 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
470 if (NULL == jsResult) {
471 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
472 "Could not create js array object");
475 for (std::size_t i = 0; i < arg.size(); ++i) {
476 JSValueRef tmpVal = toJSValueRef(arg[i]);
477 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
478 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
479 "Could not insert value into js array");
487 JSValueRef toJSValueRef_(const std::list<T>& arg)
489 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
491 if (NULL == jsResult) {
492 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Could not create js array object");
495 typename std::list<T>::const_iterator it = arg.begin();
496 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
497 JSValueRef tmpVal = toJSValueRef(*it);
498 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
499 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
500 "Could not insert value into js array");
507 template<class T, class Derived>
508 JSValueRef toJSValueRef_(const std::list<T>& arg,
509 JSValueRef (Derived::*f)(const T &),
512 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
514 if (NULL == jsResult) {
515 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Could not create js array object");
518 typename std::list<T>::const_iterator it = arg.begin();
519 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
520 JSValueRef tmpVal = (object->*f)(*it);
521 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
522 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
523 "Could not insert value into js array");
530 std::string toString_(const JSValueRef& arg);
533 std::string toString_(const T& arg)
535 std::stringstream ss;
537 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Could not convert to string.");
543 JSContextRef m_context;
547 class ConverterFactory : private DPL::Noncopyable
551 * Converter type which deletes itself when gets out of scope.
553 typedef DPL::SharedPtr<C> ConverterType;
557 * Gets converter object.
558 * @param context JavaScript context the conversion will be performed in.
559 * @param[out] exception JavaScript value for storing exception.
560 * @return Converter object.
562 static ConverterType getConverter(JSContextRef context)
564 C* convert = new C(context);
565 return ConverterType(convert);
572 typedef ConverterFactory<Converter> BasicConverterFactory;
574 typedef BasicConverterFactory::ConverterType BasicConverter;
576 } // CommonsJavaScript
579 #endif /* WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_ */