ChaiScript / ChaiScript
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-2018, 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

20
namespace chaiscript {
21
class Type_Conversions;
22
namespace dispatch {
23
class Proxy_Function_Base;
24
}  // namespace dispatch
25
}  // namespace chaiscript
26

27
namespace chaiscript
28
{
29
  namespace dispatch
30
  {
31
    struct option_explicit_set : std::runtime_error {
32 1
      explicit option_explicit_set(const std::string &t_param_name)
33 1
        : std::runtime_error("option explicit set and parameter '" + t_param_name + "' does not exist")
34
      {
35

36
      }
37

38
      option_explicit_set(const option_explicit_set &) = default;
39

40 1
      ~option_explicit_set() noexcept override = default;
41
    };
42

43
    class Dynamic_Object
44
    {
45
      public:
46 1
        explicit Dynamic_Object(std::string t_type_name)
47 1
          : m_type_name(std::move(t_type_name)), m_option_explicit(false)
48
        {
49
        }
50

51 1
        Dynamic_Object() = default;
52

53 1
        bool is_explicit() const noexcept
54
        {
55 1
          return m_option_explicit;
56
        }
57

58 1
        void set_explicit(const bool t_explicit) noexcept
59
        {
60 1
          m_option_explicit = t_explicit;
61
        }
62

63 1
        const std::string &get_type_name() const noexcept
64
        {
65 1
          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 0
        const Boxed_Value &get_attr(const std::string &t_attr_name) const
79
        {
80 0
          auto a = m_attrs.find(t_attr_name);
81

82 0
          if (a != m_attrs.end()) {
83 0
            return a->second;
84
          } else {
85 0
            throw std::range_error("Attr not found '" + t_attr_name + "' and cannot be added to const obj");
86
          }
87
        }
88

89 1
        bool has_attr(const std::string &t_attr_name) const {
90 1
          return m_attrs.find(t_attr_name) != m_attrs.end();
91
        }
92

93 1
        Boxed_Value &get_attr(const std::string &t_attr_name)
94
        {
95 1
          return m_attrs[t_attr_name];
96
        }
97

98 1
        Boxed_Value &method_missing(const std::string &t_method_name)
99
        {
100 1
          if (m_option_explicit && m_attrs.find(t_method_name) == m_attrs.end()) {
101 1
            throw option_explicit_set(t_method_name);
102
          }
103

104 1
          return get_attr(t_method_name);
105
        }
106

107 0
        const Boxed_Value &method_missing(const std::string &t_method_name) const
108
        {
109 0
          if (m_option_explicit && m_attrs.find(t_method_name) == m_attrs.end()) {
110 0
            throw option_explicit_set(t_method_name);
111
          }
112

113 0
          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

Read our documentation on viewing source code .

Loading