Molybden API
Loading...
Searching...
No Matches
js_convertible_types_detail.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_COMPATIBLE_OBJECTS_DETAIL_HPP
6#define MOLYBDEN_JS_COMPATIBLE_OBJECTS_DETAIL_HPP
7
8#include <memory>
9
10#include "molybden/base/internal/is_base_and_derived.hpp"
11
12namespace molybden {
13
14class JsObject;
15class JsProxyObject;
16
17namespace internal {
18
19template <class T>
20struct IsJsObject {
21 static constexpr bool Value();
22};
23
24template <class T>
25constexpr bool IsJsObject<T>::Value() {
26 return IsBaseAndDerived<JsObject, T>::Value();
27}
28
29template <class T>
30struct IsJsProxyObject {
31 static constexpr bool Value();
32};
33
34template <class T>
35constexpr bool IsJsProxyObject<T>::Value() {
36 return IsBaseAndDerived<JsProxyObject, T>::Value();
37}
38
39template <class T, class V = void>
40struct IsConvertibleFromJsDirectlyInternal {
41 static constexpr bool value() { return false; }
42};
43
44template <class T>
45struct IsConvertibleFromJsDirectlyInternal<
46 T,
47 typename std::enable_if<std::is_integral<T>::value ||
48 std::is_floating_point<T>::value>::type> {
49 static constexpr bool value() { return true; }
50};
51
52template <class T>
53struct IsConvertibleFromJsDirectlyInternal<std::shared_ptr<T>> {
54 static constexpr bool value() {
55 return IsJsObject<T>::Value() || IsJsProxyObject<T>::Value();
56 }
57};
58
59template <>
60struct IsConvertibleFromJsDirectlyInternal<std::string> {
61 static constexpr bool value() { return true; }
62};
63
64template <>
65struct IsConvertibleFromJsDirectlyInternal<const char*> {
66 static constexpr bool value() { return true; }
67};
68
69template <>
70struct IsConvertibleFromJsDirectlyInternal<bool> {
71 static constexpr bool value() { return true; }
72};
73
74template <>
75struct IsConvertibleFromJsDirectlyInternal<JsValue> {
76 static constexpr bool value() { return true; }
77};
78
79template <>
80struct IsConvertibleFromJsDirectlyInternal<void> {
81 static constexpr bool value() { return true; }
82};
83
84template <class T>
85constexpr bool IsConvertibleFromJsDirectly<T>::value() {
86 return IsConvertibleFromJsDirectlyInternal<
87 typename std::remove_const<T>::type>::value();
88}
89
90template <class... T>
91struct AreAllConvertibleFromJsDirectly;
92
93template <class T0, class... T>
94void AreAllConvertibleFromJsDirectly<T0, T...>::check() {
95 static_assert(
96 IsConvertibleFromJsDirectly<T0>::value(),
97 "Invalid type of the JS-accessible field or the method signature. "
98 "Supported types: "
99 " 1. double;"
100 " 2. bool;"
101 " 3. std::string;"
102 " 4. std::shared_ptr<SomeJsObject>;"
103 " 5. std::shared_ptr<SomeJsProxyObject>."
104 " Accessible methods also can return void.");
105 AreAllConvertibleFromJsDirectly<T...>::check();
106}
107
108} // namespace internal
109} // namespace molybden
110
111#endif // MOLYBDEN_JS_COMPATIBLE_OBJECTS_DETAIL_HPP