Molybden
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:
46
51
59 template <class T>
60 static JsValue from(T&& value);
61
66 bool isEmpty() const;
67
72 const std::string& asString() const;
73
77 bool isString() const;
78
83 double asNumber() const;
84
88 bool isNumber() const;
89
94 bool asBool() const;
95
99 bool isBool() const;
100
104 std::shared_ptr<JsObject> asJsObject() const;
105
109 std::shared_ptr<JsProxyObject> asJsProxyObject() const;
110
114 explicit operator bool() const;
115
116 JsValue(const JsValue& other);
117 JsValue(JsValue&& other);
118 ~JsValue();
119
120 private:
121 template <typename, typename = void>
122 struct IsFunctor : std::false_type {};
123
124 template <typename T>
125 struct IsFunctor<T, internal::void_t<decltype(&T::operator())>>
126 : std::true_type {};
127
128 template <class T>
129 explicit JsValue(T value,
130 typename std::enable_if<std::is_floating_point<T>::value ||
131 std::is_integral<T>::value,
132 T*>::type v = nullptr);
133 template <class T>
134 explicit JsValue(
135 T value,
136 typename std::enable_if<
137 std::is_function<typename std::remove_pointer<T>::type>::value,
138 T*>::type v = nullptr);
139 template <class T>
140 explicit JsValue(
141 T& value,
142 typename std::enable_if<IsFunctor<T>::value, T*>::type v = nullptr);
143
144 template <class T>
145 explicit JsValue(
146 T&& value,
147 typename std::enable_if<IsFunctor<T>::value, T*>::type v = nullptr);
148 explicit JsValue(const char* value);
149 explicit JsValue(const std::string& value);
150 explicit JsValue(std::string&& value);
151 explicit JsValue(bool value);
152 explicit JsValue(double value);
153 explicit JsValue(std::shared_ptr<JsObject> value);
154 explicit JsValue(std::shared_ptr<JsProxyObject> value);
155
156 std::unique_ptr<JsValueImpl> impl_;
157};
158
159} // namespace molybden
160
161#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.