Molybden API
Loading...
Searching...
No Matches
js_value_converter.hpp
1// Copyright (c) 2000-2023 TeamDev Ltd. All rights reserved.
2// TeamDev PROPRIETARY and CONFIDENTIAL.
3// Use is subject to license terms.
4
5#ifndef MOLYBDEN_JS_VALUE_CONVERTER_HPP
6#define MOLYBDEN_JS_VALUE_CONVERTER_HPP
7
8#include <vector>
9
10#include "molybden/js/js_array.hpp"
11#include "molybden/js/js_value.hpp"
12
13namespace molybden {
14namespace internal {
15
16template <class T>
17struct IsJsObject;
18
19template <class T>
20struct IsJsProxyObject;
21
22template <class T>
23using EnableIfJsObject = typename std::enable_if<IsJsObject<T>::Value()>::type;
24
25template <class T>
26using EnableIfJsProxyObject =
27 typename std::enable_if<IsJsProxyObject<T>::Value()>::type;
28
47template <class T, class V = void>
48class JsValueConverter;
49
50template <>
51class JsValueConverter<double> {
52 public:
53 static double convert(const JsValue& value);
54 static bool isConvertible(const JsValue& value);
55 static std::string convertErrorTypeInfo();
56};
57
58template <>
59class JsValueConverter<float> {
60 public:
61 static float convert(const JsValue& value);
62 static bool isConvertible(const JsValue& value);
63 static std::string convertErrorTypeInfo();
64};
65
66template <class T>
67class JsValueConverter<T, typename std::enable_if<std::is_integral<T>::value>::type> {
68 public:
69 static T convert(const JsValue& value);
70 static bool isConvertible(const JsValue& value);
71 static std::string convertErrorTypeInfo();
72};
73
74template <>
75class JsValueConverter<std::string> {
76 public:
77 static std::string convert(const JsValue& value);
78 static bool isConvertible(const JsValue& value);
79 static std::string convertErrorTypeInfo();
80};
81
82template <>
83class JsValueConverter<bool> {
84 public:
85 static bool convert(const JsValue& value);
86 static bool isConvertible(const JsValue& value);
87 static std::string convertErrorTypeInfo();
88};
89
90template <class T>
91class JsValueConverter<std::shared_ptr<T>, EnableIfJsObject<T>> {
92 public:
97 static std::shared_ptr<T> convert(const JsValue& value);
98 static bool isConvertible(const JsValue& value);
99 static std::string convertErrorTypeInfo();
100};
101
102template <class T>
103class JsValueConverter<std::shared_ptr<T>, EnableIfJsProxyObject<T>> {
104 public:
109 static std::shared_ptr<T> convert(const JsValue& value);
110 static bool isConvertible(const JsValue& value);
111 static std::string convertErrorTypeInfo();
112};
113
120template <class... T>
121class IndexedJsValueConverter {
122 public:
123 explicit IndexedJsValueConverter(const std::vector<JsValue>& values);
124
128 template <std::size_t I>
129 using TypeOfArg = typename std::remove_reference<decltype(std::get<I>(
130 std::tuple<T...>()))>::type;
131
140 template <std::size_t I>
141 TypeOfArg<I> convert();
142
150 template <std::size_t I>
151 bool isConvertible();
152
161 template <std::size_t I>
162 std::string convertErrorTypeInfo();
163
164 private:
165 const std::vector<JsValue>& values_;
166};
167
168} // namespace internal
169} // namespace molybden
170
171#endif // MOLYBDEN_JS_VALUE_CONVERTER_HPP