Molybden API
Loading...
Searching...
No Matches
js_value_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_DETAIL_HPP
6#define MOLYBDEN_JS_VALUE_DETAIL_HPP
7
8#include "molybden/js/internal/js_proxy_function_object.hpp"
9
10namespace molybden {
11
12namespace internal {
13template <class T>
14struct IsCharArray {
15 static constexpr bool value = false;
16};
17
18template <size_t N>
19struct IsCharArray<char[N]> {
20 static constexpr bool value = true;
21};
22
23template <size_t N>
24struct IsCharArray<const char[N]> {
25 static constexpr bool value = true;
26};
27
28template <class R, class... T>
29std::shared_ptr<JsProxyObject> getFunctionProxy(R (*function)(T...)) {
30 return std::make_shared<internal::JsProxyFunctionObject<R, T...>>(
31 std::function<R(T...)>(function));
32}
33
34template <class F, class R, class... T>
35std::shared_ptr<JsProxyObject> getFunctionProxy(F&& function,
36 R (F::*)(T...) const) {
37 return std::make_shared<internal::JsProxyFunctionObject<R, T...>>(
38 std::function<R(T...)>(function));
39}
40
41template <class F, class R, class... T>
42std::shared_ptr<JsProxyObject> getFunctionProxy(F& function,
43 R (F::*)(T...) const) {
44 return std::make_shared<internal::JsProxyFunctionObject<R, T...>>(
45 std::function<R(T...)>(function));
46}
47
48} // namespace internal
49
50template <class T>
52 using TNoRef = typename std::remove_reference<T>::type;
53
54 static_assert(
55 internal::IsConvertibleFromJsDirectly<TNoRef>::value() ||
56 internal::IsCharArray<TNoRef>::value ||
57 std::is_same<TNoRef, char*>::value ||
58 std::is_same<TNoRef, const char*>::value ||
59 IsFunctor<TNoRef>::value ||
60 std::is_function<typename std::remove_pointer<TNoRef>::type>::value,
61 "Unable to construct JsValue from the given type.");
62 return JsValue(std::forward<T>(value));
63}
64
65template <class T>
66JsValue::JsValue(T value, EnableIfNumber<T>)
67 : JsValue(static_cast<double>(value)) {}
68
69template <class T>
70JsValue::JsValue(T value, EnableIfFunction<T>)
71 : JsValue(internal::getFunctionProxy(value)) {}
72
73template <class T>
74JsValue::JsValue(T& value, EnableIfFunctor<T>)
75 : JsValue(
76 internal::getFunctionProxy(std::forward<T>(value), &T::operator())) {}
77
78template <class T>
79JsValue::JsValue(T&& value, EnableIfFunctor<T>)
80 : JsValue(
81 internal::getFunctionProxy(std::forward<T>(value), &T::operator())) {}
82
83} // namespace molybden
84
85#endif // MOLYBDEN_JS_VALUE_DETAIL_HPP
A JavaScript value.
Definition js_value.hpp:44
static JsValue from(T &&value)
Constructs a JavaScript value from the convertible C++ one.
Definition js_value_detail.hpp:51
JsValue()
Constructs an empty JsValue.