Molybden API
Loading...
Searching...
No Matches
js_value_converter.hpp
1// Copyright (c) 2000-2024 TeamDev. 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 <>
91class JsValueConverter<JsValue> {
92 public:
93 static JsValue convert(const JsValue& value);
94 static JsValue convert(JsValue&& value);
95 static constexpr bool isConvertible(const JsValue& value) {return true;}
96 static std::string convertErrorTypeInfo();
97};
98
99template <class T>
100class JsValueConverter<std::shared_ptr<T>, EnableIfJsObject<T>> {
101 public:
106 static std::shared_ptr<T> convert(const JsValue& value);
107 static bool isConvertible(const JsValue& value);
108 static std::string convertErrorTypeInfo();
109};
110
111template <class T>
112class JsValueConverter<std::shared_ptr<T>, EnableIfJsProxyObject<T>> {
113 public:
118 static std::shared_ptr<T> convert(const JsValue& value);
119 static bool isConvertible(const JsValue& value);
120 static std::string convertErrorTypeInfo();
121};
122
129template <class... T>
130class IndexedJsValueConverter {
131 public:
132 explicit IndexedJsValueConverter(const std::vector<JsValue>& values);
133
137 template <std::size_t I>
138 using TypeOfArg = typename std::remove_reference<decltype(std::get<I>(
139 std::tuple<T...>()))>::type;
140
149 template <std::size_t I>
150 TypeOfArg<I> convert();
151
159 template <std::size_t I>
160 bool isConvertible();
161
170 template <std::size_t I>
171 std::string convertErrorTypeInfo();
172
173 private:
174 const std::vector<JsValue>& values_;
175};
176
177} // namespace internal
178} // namespace molybden
179
180#endif // MOLYBDEN_JS_VALUE_CONVERTER_HPP