Molybden API
Loading...
Searching...
No Matches
js_value_converter_detail.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_DETAIL_HPP
6#define MOLYBDEN_JS_VALUE_CONVERTER_DETAIL_HPP
7
8#include "molybden/js/internal/js_value_converter.hpp"
9
10#include <limits>
11#include <cmath>
12
13namespace molybden {
14namespace internal {
15
16template <class T, class V = void>
17struct TypeName {};
18
19template <class T>
20struct TypeName<T, EnableIfJsObject<T>> {
21 static std::string toString() { return "JsObject"; }
22};
23
24template <>
25struct TypeName<JsArray> {
26 static std::string toString();
27};
28
29template <class T>
30T JsValueConverter<T,
31 typename std::enable_if<std::is_integral<T>::value>::type>::
32 convert(const JsValue& value) {
33 return static_cast<T>(value.asNumber());
34}
35
36inline bool isInteger(double value) {
37 return std::floor(value) == value;
38}
39
40template <class T>
41bool JsValueConverter<T,
42 typename std::enable_if<std::is_integral<T>::value>::
43 type>::isConvertible(const JsValue& value) {
44 if (value.isNumber() && isInteger(value.asNumber())) {
45 return value.asNumber() >= (std::numeric_limits<T>::min)() &&
46 value.asNumber() <= (std::numeric_limits<T>::max)();
47 }
48 return false;
49}
50
51template <class T>
52std::string
53JsValueConverter<T, typename std::enable_if<std::is_integral<T>::value>::type>::
54 convertErrorTypeInfo() {
55 std::string result;
56 if (std::is_unsigned<T>::value) {
57 result += "u";
58 }
59 result += "int";
60 result += std::to_string(sizeof(T) * 8);
61 result += "_t";
62 return result;
63}
64
65template <class T>
66std::shared_ptr<T>
67JsValueConverter<std::shared_ptr<T>, EnableIfJsObject<T>>::convert(
68 const JsValue& value) {
69 if (auto object = value.asJsObject()) {
70 return object->as<T>();
71 }
72 return nullptr;
73}
74
75template <class T>
76bool JsValueConverter<std::shared_ptr<T>, EnableIfJsObject<T>>::isConvertible(
77 const JsValue& value) {
78 return !!convert(value);
79}
80
81template <class T>
82std::string JsValueConverter<std::shared_ptr<T>,
83 EnableIfJsObject<T>>::convertErrorTypeInfo() {
84 return TypeName<T>::toString();
85}
86
87template <class T>
88std::shared_ptr<T>
89JsValueConverter<std::shared_ptr<T>, EnableIfJsProxyObject<T>>::convert(
90 const JsValue& value) {
91 if (auto object = value.asJsProxyObject()) {
92 return object->as<T>();
93 }
94 return nullptr;
95}
96
97template <class T>
98bool JsValueConverter<std::shared_ptr<T>, EnableIfJsProxyObject<T>>::
99 isConvertible(const JsValue& value) {
100 return !!convert(value);
101}
102
103template <class T>
104std::string JsValueConverter<std::shared_ptr<T>,
105 EnableIfJsProxyObject<T>>::convertErrorTypeInfo() {
106 return "the proxy object of the appropriate class";
107}
108
109template <class... T>
110IndexedJsValueConverter<T...>::IndexedJsValueConverter(
111 const std::vector<JsValue>& values)
112 : values_(values) {}
113
114template <class... T>
115template <std::size_t I>
116typename IndexedJsValueConverter<T...>::template TypeOfArg<I>
117IndexedJsValueConverter<T...>::convert() {
118 return JsValueConverter<TypeOfArg<I>>::convert(values_.at(I));
119}
120
121template <class... T>
122template <std::size_t I>
123bool IndexedJsValueConverter<T...>::isConvertible() {
124 return JsValueConverter<TypeOfArg<I>>::isConvertible(values_.at(I));
125}
126
127template <class... T>
128template <std::size_t I>
129std::string IndexedJsValueConverter<T...>::convertErrorTypeInfo() {
130 return JsValueConverter<TypeOfArg<I>>::convertErrorTypeInfo();
131}
132
133} // namespace internal
134} // namespace molybden
135
136#endif // MOLYBDEN_JS_VALUE_CONVERTER_DETAIL_HPP