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 {
32 class Converter : private DPL::Noncopyable
35 explicit Converter(JSContextRef context);
39 * Converts JSValueRef to JSObjectRef.
40 * @param arg JSValueRef object to convert.
41 * @return JSObjectRef object.
42 * @throw ConversionException Thrown when conversion fails.
44 JSObjectRef toJSObjectRef(const JSValueRef& arg);
47 * Converts JSValueRef object to boolean value.
48 * @param arg JSValueRef object to convert.
49 * @return Boolean value.
50 * @throw ConversionException Thrown when conversion fails.
52 bool toBool(const JSValueRef& arg);
55 * Converts JSValueRef object to unsigned char value.
56 * @param arg JSValueRef object to convert.
57 * @return Unsigned char value.
58 * @throw ConversionException Thrown when conversion fails.
60 unsigned char toUChar(const JSValueRef& arg);
63 * Converts JSValueRef object to char value.
64 * @param arg JSValueRef object to convert.
66 * @throw ConversionException Thrown when conversion fails.
68 char toChar(const JSValueRef& arg);
71 * Converts JSValueRef to integer value.
72 * @param arg JSValueRef object to convert.
73 * @return Integer value.
74 * @throw ConversionException Thrown when conversion fails.
76 int toInt(const JSValueRef& arg);
79 * Converts string to integer value.
80 * @param arg string to convert.
81 * @return Integer value.
82 * @throw ConversionException Thrown when conversion fails.
84 int toInt(const std::string& arg);
87 * Converts JSvalueRef to long integer value.
88 * @param arg JSValueRef object to convert.
89 * @return Long integer value.
90 * @throw ConversionException Thrown when conversion fails.
92 long toLong(const JSValueRef& arg);
95 * Converts JSvalueRef to long long integer value.
96 * @param arg JSValueRef object to convert.
97 * @return Long long integer value.
98 * @throw ConversionException Thrown when conversion fails.
100 long long toLongLong(const JSValueRef& arg);
102 * Converts JSValueRef to unsigned long value.
103 * @param arg JSValueRef object to convert.
104 * @return Unsigned long value.
105 * @throw ConversionException Thrown when conversion fails.
107 unsigned long toULong(const JSValueRef& arg);
110 * Converts JSValueRef to double value.
111 * @param arg JSValueRef object to convert.
112 * @return Double value.
113 * @throw ConversionException Thrown when conversion fails.
115 double toDouble(const JSValueRef& arg);
118 * Converts JSValueRef to size_t value.
119 * @param arg JSValueRef object to convert.
120 * @return Size_t value.
121 * @throw ConversionException Thrown when conversion fails.
123 std::size_t toSizeT(const JSValueRef& arg);
126 * Converts JSStringRef to size_t value.
127 * @param arg JSStringRef object to convert.
128 * @return Size_t value.
129 * @throw ConversionException Thrown when conversion fails.
131 std::size_t toSizeT(const JSStringRef& arg);
134 * Converts int to STL string.
135 * @param arg int to convert.
136 * @return STL string.
137 * @throw ConversionException Thrown when conversion fails.
139 std::string toString(int arg);
141 std::string toString(unsigned long arg);
143 std::string toString(long arg);
145 std::string toString(std::size_t arg);
148 * Converts JSValueRef to STL string.
149 * @param arg JSValueRef to convert.
150 * @return STL string.
151 * @throw ConversionException Thrown when conversion fails.
152 * @remarks If one wants to convert JS Date object to a string then
153 * toDateString() should be used.
155 std::string toString(const JSValueRef& arg);
158 * Converts JSStringRef to STL string.
159 * @param arg JSStringRef to convert.
160 * @return STL string.
161 * @throw ConversionException Thrown when conversion fails.
163 std::string toString(const JSStringRef& arg);
166 * Converts JSSValueRef to STL string if
167 * arg is one of: String, Number, Bool
168 * @param arg JSValueReg to convert.
169 * @return STL string.
170 * @throw ConversionException Thrown when conversion fails.
172 std::string tryString(const JSValueRef& arg);
175 * Converts JS Date object to STL string.
176 * @param arg JS Date object to convert.
177 * @return STL string.
178 * @throw ConversionException Thrown when conversion fails.
180 std::string toDateString(const JSValueRef& arg);
183 * Converts JSValueRef to time_t value.
184 * @param arg JSValueRef to convert.
185 * @return time_t value.
186 * @throw ConversionException Thrown when conversion fails.
188 time_t toDateTimeT(const JSValueRef& arg);
191 * Converts JSValueRef to tm struct.
192 * @param arg JSValueRef to convert.
194 * @throw ConversionException Thrown when conversion fails.
196 tm toDateTm(const JSValueRef& arg);
199 * Converts JSValueRef to vector<unsigned char>.
200 * @param arg JSValueRef to convert.
201 * @return vector<unsigned char>.
202 * @throw ConversionException Thrown when conversion fails.
204 std::vector<unsigned char> toVectorOfUChars(const JSValueRef& arg);
207 * Converts JSValueRef to vector<char>.
208 * @param arg JSValueRef to convert.
209 * @return vector<char>.
210 * @throw ConversionException Thrown when conversion fails.
212 std::vector<char> toVectorOfChars(const JSValueRef& arg);
215 * Converts JSValueRef to vector<int>.
216 * @param arg JSValueRef to convert.
217 * @return vector<int>.
218 * @throw ConversionException Thrown when conversion fails.
220 std::vector<int> toVectorOfInts(const JSValueRef& arg);
223 * Converts JSValueRef to vector of string.
224 * @param arg JSValueRef to convert.
225 * @return std::vector<std::string>
226 * @throw ConversionException Thrown when conversion fails.
228 std::vector<std::string> toVectorOfStrings(const JSValueRef& arg);
231 * Converts JSValueRef to vector of time_t.
232 * @param arg JSValueRef to convert.
233 * @return std::vector<std::time_t>
234 * @throw ConversionException Thrown when conversion fails.
236 std::vector<std::time_t> toVectorOfTimeT(const JSValueRef& arg);
239 * Converts boolean value to JSValueRef.
240 * @param arg Boolean value to convert.
241 * @return JSValueRef object.
242 * @throw ConversionException Thrown when conversion fails.
244 JSValueRef toJSValueRef(bool arg);
247 * Converts integer value to JSValueRef.
248 * @param arg Integer value to convert.
249 * @return JSValueRef object.
250 * @throw ConversionException Thrown when conversion fails.
252 JSValueRef toJSValueRef(int arg);
255 * Converts unsigned integer value to JSValueRef.
256 * @param arg Unsigned integer value to convert.
257 * @return JSValueRef object.
258 * @throw ConversionException Thrown when conversion fails.
260 JSValueRef toJSValueRef(unsigned int arg);
263 * Converts double value to JSValueRef.
264 * @param arg Double value to convert.
265 * @return JSValueRef object.
266 * @throw ConversionException Thrown when conversion fails.
268 JSValueRef toJSValueRef(double arg);
271 * Converts unsigned long value to JSValueRef.
272 * @param arg unsigned long value to convert.
273 * @return JSValueRef object.
274 * @throw ConversionException Thrown when conversion fails.
276 JSValueRef toJSValueRef(unsigned long arg);
279 * Converts long value to JSValueRef.
280 * @param arg long value to convert.
281 * @return JSValueRef object.
282 * @throw ConversionException Thrown when conversion fails.
284 JSValueRef toJSValueRefLong(const long arg);
287 * Converts unsigned long value to JSValueRef.
288 * @param arg long long int value to convert.
289 * @return JSValueRef object.
290 * @throw ConversionException Thrown when conversion fails.
292 JSValueRef toJSValueRef(long long int arg);
295 * Converts STL string to JSValueRef.
296 * @param arg STL string to convert.
297 * @return JSValueRef object.
298 * @throw ConversionException Thrown when conversion fails.
300 JSValueRef toJSValueRef(const std::string& arg);
303 * Converts char sequence to JSValueRef.
304 * @param arg char sequence to convert.
305 * @return JSValueRef object.
306 * @throw ConversionException Thrown when conversion fails.
308 JSValueRef toJSValueRef(const char* arg);
311 * Converts time_t value to JSValueRef.
312 * @param arg time_t value to convert.
313 * @return JSValueRef object.
314 * @throw ConversionException Thrown when conversion fails.
316 JSValueRef toJSValueRef(const time_t arg);
319 * Converts tm struct to JSValueRef.
320 * @param arg tm struct to convert.
321 * @return JSValueRef object.
322 * @throw ConversionException Thrown when conversion fails.
324 JSValueRef toJSValueRef(const tm& arg);
327 * Converts STL vector of integer to JSValueRef.
328 * @param arg STL vector of integer to convert.
329 * @return JSValueRef object.
330 * @throw ConversionException Thrown when conversion fails.
332 JSValueRef toJSValueRef(const std::vector<int>& arg);
335 * Converts STL vector of time_t to JSValueRef.
336 * @param arg STL vector of time_t to convert.
337 * @return JSValueRef object.
338 * @throw ConversionException Thrown when conversion fails.
340 JSValueRef toJSValueRef(const std::vector<time_t>& arg);
343 * Converts STL vector of string to JSStringRef.
344 * @param arg STL string to convert.
345 * @return JSValueRef object.
346 * @throw ConversionException Thrown when conversion fails.
348 JSValueRef toJSValueRef(const std::vector<std::string>& arg);
351 * Converts JSValueRef to JSValueRef
352 * @param JSValueRef js value which is returned
353 * @return JSValueRef - parameter
354 * @throw ConversionException Thrown when conversion fails.
356 JSValueRef toJSValueRef(JSValueRef arg);
359 * Converts std::vector to JSValueRef
360 * @param arg vecotr of JSValueRefs
361 * @return JSValueRef - js array
362 * @throw ConversionException Thrown when conversion fails.
364 JSValueRef toJSValueRef(const std::vector<JSValueRef>& arg);
367 * Template specialization to catch errors caused
368 * by wrong usage of general template or
369 * implicit conversions from SharedPtr to
372 template<class Class>
373 JSValueRef toJSValueRef(const DPL::SharedPtr<Class>& c)
375 STATIC_ERROR(ERROR_INVALID_IMPLICIT_CONVERSION, c)
376 //above macro causes compilation error
377 return static_cast<JSValueRef>(NULL);
381 * Converts STL string to JSStringRef.
382 * @param arg STL string to convert.
383 * @return JSStringRef object.
384 * @throw ConversionException Thrown when conversion fails.
386 JSStringRef toJSStringRef(const std::string& arg);
389 bool isNan(double value) const;
391 double toNumber_(const JSValueRef& arg);
393 double toNumber_(const JSStringRef& arg);
395 double toNumber_(const std::string& arg);
397 template<typename T, class Derived>
398 std::vector<T> toVectorOfT_(const JSValueRef& arg,
399 T (Derived::*f)(const JSValueRef &),
402 if (JSValueIsNull(m_context,
403 arg) || JSValueIsUndefined(m_context, arg))
405 return std::vector<T>();
408 if (!JSIsArrayValue(m_context, arg)) {
409 ThrowMsg(Commons::ConversionException,
410 "Argument is not an JS array.");
413 std::vector<T> result;
414 JSObjectRef objArg = toJSObjectRef(arg);
415 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
416 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
417 result.push_back((object->*f)(element));
423 std::vector<T> toVectorOfT_(const JSValueRef& arg,
424 T (Converter::*f)(const JSValueRef &))
426 if (JSValueIsNull(m_context,
427 arg) || JSValueIsUndefined(m_context, arg))
429 return std::vector<T>();
432 if (!JSIsArrayValue(m_context, arg)) {
433 ThrowMsg(Commons::ConversionException,
434 "Argument is not an JS array.");
437 std::vector<T> result;
438 JSObjectRef objArg = toJSObjectRef(arg);
439 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
440 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
441 result.push_back((this->*f)(element));
446 template<class T, class Derived>
447 JSValueRef toJSValueRef_(const std::vector<T>& arg,
448 JSValueRef (Derived::*f)(const T &),
451 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
453 if (NULL == jsResult) {
454 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
455 "Could not create js array object");
458 for (std::size_t i = 0; i < arg.size(); ++i) {
459 JSValueRef tmpVal = (object->*f)(arg[i]);
460 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
461 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
462 "Could not insert value into js array");
470 JSValueRef toJSValueRef_(const std::vector<T>& arg)
472 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
474 if (NULL == jsResult) {
475 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
476 "Could not create js array object");
479 for (std::size_t i = 0; i < arg.size(); ++i) {
480 JSValueRef tmpVal = toJSValueRef(arg[i]);
481 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
482 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
483 "Could not insert value into js array");
491 JSValueRef toJSValueRef_(const std::list<T>& arg)
493 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
495 if (NULL == jsResult) {
496 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
497 "Could not create js array object");
500 typename std::list<T>::const_iterator it = arg.begin();
501 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
502 JSValueRef tmpVal = toJSValueRef(*it);
503 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
504 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
505 "Could not insert value into js array");
512 template<class T, class Derived>
513 JSValueRef toJSValueRef_(const std::list<T>& arg,
514 JSValueRef (Derived::*f)(const T &),
517 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
519 if (NULL == jsResult) {
520 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
521 "Could not create js array object");
524 typename std::list<T>::const_iterator it = arg.begin();
525 for (std::size_t i = 0; it != arg.end(); ++i, ++it) {
526 JSValueRef tmpVal = (object->*f)(*it);
527 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
528 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
529 "Could not insert value into js array");
536 std::string toString_(const JSValueRef& arg);
539 std::string toString_(const T& arg)
541 std::stringstream ss;
543 ThrowMsg(WrtDeviceApis::Commons::ConversionException,
544 "Could not convert to string.");
550 JSContextRef m_context;
554 class ConverterFactory : private DPL::Noncopyable
558 * Converter type which deletes itself when gets out of scope.
560 typedef DPL::SharedPtr<C> ConverterType;
564 * Gets converter object.
565 * @param context JavaScript context the conversion will be performed in.
566 * @param[out] exception JavaScript value for storing exception.
567 * @return Converter object.
569 static ConverterType getConverter(JSContextRef context)
571 C* convert = new C(context);
572 return ConverterType(convert);
579 typedef ConverterFactory<Converter> BasicConverterFactory;
581 typedef BasicConverterFactory::ConverterType BasicConverter;
582 } // CommonsJavaScript
585 #endif /* WRTDEVICEAPIS_COMMONSJAVASCRIPT_CONVERTER_H_ */