Molybden API
Loading...
Searching...
No Matches
js_value.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_HPP
6#define MOLYBDEN_JS_VALUE_HPP
7
8#include <functional>
9#include <memory>
10#include <string>
11
12#include "molybden/base/internal/void_t.hpp"
13
14namespace molybden {
15
16class JsObject;
17class JsProxyObject;
18
19class JsValueImpl;
20
44class JsValue {
45 public:
50
58 template <class T>
59 static JsValue from(T&& value);
60
65 bool isEmpty() const;
66
71 const std::string& asString() const;
72
76 bool isString() const;
77
82 double asNumber() const;
83
87 bool isNumber() const;
88
93 bool asBool() const;
94
98 bool isBool() const;
99
103 std::shared_ptr<JsObject> asJsObject() const;
104
108 std::shared_ptr<JsProxyObject> asJsProxyObject() const;
109
113 explicit operator bool() const;
114
115 JsValue& operator=(const JsValue& other);
116 JsValue& operator=(JsValue&& other);
117
118 JsValue(const JsValue& other);
119 JsValue(JsValue&& other);
120 ~JsValue();
121
122 private:
123 template <typename, typename = void>
124 struct IsFunctor : std::false_type {};
125
126 template <typename T>
127 struct IsFunctor<T, internal::void_t<decltype(&T::operator())>>
128 : std::true_type {};
129
130 template <class T>
131 using EnableIfNumber = typename std::enable_if<
132 std::is_floating_point<T>::value || std::is_integral<T>::value,
133 T*>::type;
134 template <class T>
135 using EnableIfFunction = typename std::enable_if<
136 std::is_function<typename std::remove_pointer<T>::type>::value,
137 T*>::type;
138 template <class T>
139 using EnableIfFunctor = typename std::enable_if<IsFunctor<T>::value, T*>::type;
140
141 template <class T>
142 explicit JsValue(T value, EnableIfNumber<T> = nullptr);
143 template <class T>
144 explicit JsValue(T value, EnableIfFunction<T> = nullptr);
145 template <class T>
146 explicit JsValue(T& value, EnableIfFunctor<T> = nullptr);
147 template <class T>
148 explicit JsValue(T&& value, EnableIfFunctor<T> = nullptr);
149 explicit JsValue(const char* value);
150 explicit JsValue(const std::string& value);
151 explicit JsValue(std::string&& value);
152 explicit JsValue(bool value);
153 explicit JsValue(double value);
154 explicit JsValue(std::shared_ptr<JsObject> value);
155 explicit JsValue(std::shared_ptr<JsProxyObject> value);
156
157 std::unique_ptr<JsValueImpl> impl_;
158};
159
160} // namespace molybden
161
162#endif // MOLYBDEN_JS_VALUE_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
double asNumber() const
Returns the number that the value represents if it does, otherwise it should not be called.
std::shared_ptr< JsProxyObject > asJsProxyObject() const
Returns a JsProxyObject if the value represents a boolean value.
bool isNumber() const
Indicates if the given value represents a number.
JsValue()
Constructs an empty JsValue.
bool isString() const
Indicates if the given value represents a string.
bool isBool() const
Indicates if the given value represents a boolean.
std::shared_ptr< JsObject > asJsObject() const
Returns a JsObject if the value represents a boolean value.
bool isEmpty() const
Returns true if this JsValue does not hold a value (or, equally, represents null or undefined).
bool asBool() const
Returns the boolean that the value represents if it does, otherwise it should not be called.
const std::string & asString() const
Returns the string that the value represents if it does, otherwise it should not be called.