IncludeJS  0.0.1
Build your own JavaScript runtime
engine_value.h
1 #ifndef INCLUDEJS_ENGINE_VALUE_H_
2 #define INCLUDEJS_ENGINE_VALUE_H_
3 
4 #include "engine_export.h"
5 
6 #include <functional> // std::function
7 #include <map> // std::map
8 #include <memory> // std::unique_ptr
9 #include <optional> // std::optional
10 #include <string> // std::string
11 #include <vector> // std::vector
12 
13 namespace includejs {
14 
15 enum class ValueType {
16  Number,
17  String,
18  Error,
19  Object,
20  Boolean,
21  Undefined,
22  Null,
23  Array,
24  Function,
25  Unknown
26 };
27 
28 // Inspired by https://github.com/sourcemeta/jsontoolkit
30 class INCLUDEJS_ENGINE_EXPORT Value {
31 public:
32  // Consumers are not meant to create this class directly
33 #ifndef DOXYGEN
34  Value(const void *context, const void *value);
35  ~Value();
36 #endif
37 
38  Value(Value &&other) noexcept;
39  Value(const Value &other) = delete;
40 
41  using Function = std::function<Value(std::vector<Value> arguments)>;
42  using FunctionStorage = std::map<void *, Function>;
43 
44  auto is_number() const -> bool;
45  auto is_string() const -> bool;
46  auto is_error() const -> bool;
47  auto is_object() const -> bool;
48  auto is_boolean() const -> bool;
49  auto is_undefined() const -> bool;
50  auto is_null() const -> bool;
51  auto is_array() const -> bool;
52  auto is_function() const -> bool;
53  auto to_number() const -> double;
54  auto to_string() const -> std::string;
55  auto to_boolean() const -> bool;
56  auto to_function() const -> Function;
57  auto type() const -> ValueType;
58  auto at(const std::string &property) const -> std::optional<Value>;
59  auto at(const unsigned int &position) const -> std::optional<Value>;
60  auto set(const std::string &property, Value value) -> void;
61  auto set(const std::string &property, Function function) -> void;
62  auto private_data() -> void *;
63  auto private_data(void *data, std::function<void(void *)> deleter) -> void;
64  auto push(Value value) -> void;
65  auto to_map() const -> std::map<std::string, Value>;
66  auto to_vector() const -> std::vector<Value>;
67  auto to_json_string() const -> std::string;
68 
69  // For internal use only
70 #ifndef DOXYGEN
71  auto native() const -> const void *;
72 #endif
73 
74 private:
75  struct Internal;
76  std::unique_ptr<Internal> internal;
77 };
78 
79 } // namespace includejs
80 
81 #endif
Definition: engine_value.h:30