GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
dynamic_object.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9
10
11#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
12#define CHAISCRIPT_DYNAMIC_OBJECT_HPP_
13
14#include <map>
15#include <string>
16#include <utility>
17
18#include "boxed_value.hpp"
19
20namespace chaiscript {
22namespace dispatch {
24} // namespace dispatch
25} // namespace chaiscript
26
27namespace chaiscript
28{
29 namespace dispatch
30 {
31 struct option_explicit_set : std::runtime_error {
32 explicit option_explicit_set(const std::string &t_param_name)
33 : std::runtime_error("option explicit set and parameter '" + t_param_name + "' does not exist")
34 {
35
36 }
37
39
40 ~option_explicit_set() noexcept override = default;
41 };
42
44 {
45 public:
46 explicit Dynamic_Object(std::string t_type_name)
47 : m_type_name(std::move(t_type_name)), m_option_explicit(false)
48 {
49 }
50
51 Dynamic_Object() = default;
52
53 bool is_explicit() const
54 {
55 return m_option_explicit;
56 }
57
58 void set_explicit(const bool t_explicit)
59 {
60 m_option_explicit = t_explicit;
61 }
62
63 std::string get_type_name() const
64 {
65 return m_type_name;
66 }
67
68 const Boxed_Value &operator[](const std::string &t_attr_name) const
69 {
70 return get_attr(t_attr_name);
71 }
72
73 Boxed_Value &operator[](const std::string &t_attr_name)
74 {
75 return get_attr(t_attr_name);
76 }
77
78 const Boxed_Value &get_attr(const std::string &t_attr_name) const
79 {
80 auto a = m_attrs.find(t_attr_name);
81
82 if (a != m_attrs.end()) {
83 return a->second;
84 } else {
85 throw std::range_error("Attr not found '" + t_attr_name + "' and cannot be added to const obj");
86 }
87 }
88
89 bool has_attr(const std::string &t_attr_name) const {
90 return m_attrs.find(t_attr_name) != m_attrs.end();
91 }
92
93 Boxed_Value &get_attr(const std::string &t_attr_name)
94 {
95 return m_attrs[t_attr_name];
96 }
97
98 Boxed_Value &method_missing(const std::string &t_method_name)
99 {
100 if (m_option_explicit && m_attrs.find(t_method_name) == m_attrs.end()) {
101 throw option_explicit_set(t_method_name);
102 }
103
104 return get_attr(t_method_name);
105 }
106
107 const Boxed_Value &method_missing(const std::string &t_method_name) const
108 {
109 if (m_option_explicit && m_attrs.find(t_method_name) == m_attrs.end()) {
110 throw option_explicit_set(t_method_name);
111 }
112
113 return get_attr(t_method_name);
114 }
115
116 std::map<std::string, Boxed_Value> get_attrs() const
117 {
118 return m_attrs;
119 }
120
121 private:
122 const std::string m_type_name = "";
123 bool m_option_explicit = false;
124
125 std::map<std::string, Boxed_Value> m_attrs;
126 };
127
128 }
129}
130#endif
131
A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects.
Definition boxed_value.hpp:28
Definition type_conversions.hpp:321
Dynamic_Object(std::string t_type_name)
Definition dynamic_object.hpp:46
Boxed_Value & method_missing(const std::string &t_method_name)
Definition dynamic_object.hpp:98
std::string get_type_name() const
Definition dynamic_object.hpp:63
bool has_attr(const std::string &t_attr_name) const
Definition dynamic_object.hpp:89
Boxed_Value & get_attr(const std::string &t_attr_name)
Definition dynamic_object.hpp:93
std::map< std::string, Boxed_Value > get_attrs() const
Definition dynamic_object.hpp:116
void set_explicit(const bool t_explicit)
Definition dynamic_object.hpp:58
const Boxed_Value & get_attr(const std::string &t_attr_name) const
Definition dynamic_object.hpp:78
Boxed_Value & operator[](const std::string &t_attr_name)
Definition dynamic_object.hpp:73
const Boxed_Value & operator[](const std::string &t_attr_name) const
Definition dynamic_object.hpp:68
const Boxed_Value & method_missing(const std::string &t_method_name) const
Definition dynamic_object.hpp:107
bool is_explicit() const
Definition dynamic_object.hpp:53
Definition proxy_functions.hpp:196
Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use t...
Definition callable_traits.hpp:13
Namespace chaiscript contains every API call that the average user will be concerned with.
STL namespace.
option_explicit_set(const std::string &t_param_name)
Definition dynamic_object.hpp:32
~option_explicit_set() noexcept override=default
option_explicit_set(const option_explicit_set &)=default