Debian Codegraph @

kiwi/variable.h

raw
1 /*-----------------------------------------------------------------------------
2 | Copyright (c) 2013-2026, Nucleic Development Team.
3 |
4 | Distributed under the terms of the Modified BSD License.
5 |
6 | The full license is in the file LICENSE, distributed with this software.
7 |----------------------------------------------------------------------------*/
8 #pragma once
9 #include <memory>
10 #include <string>
11 #include "shareddata.h"
12
13 namespace kiwi
14 {
15
16 class Variable
17 {
18
19 public:
20 class Context
21 {
22 public:
23 Context() = default;
24 virtual ~Context() {} // LCOV_EXCL_LINE
25 };
26
27 Variable(Context *context = 0) : m_data(new VariableData("", context)) {}
28
29 Variable(std::string name, Context *context = 0) : m_data(new VariableData(std::move(name), context)) {}
30
31 Variable(const char *name, Context *context = 0) : m_data(new VariableData(name, context)) {}
32
33 Variable(const Variable&) = default;
34
35 Variable(Variable&&) noexcept = default;
36
37 ~Variable() = default;
38
39 const std::string &name() const
40 {
41 return m_data->m_name;
42 }
43
44 void setName(const char *name)
45 {
46 m_data->m_name = name;
47 }
48
49 void setName(const std::string &name)
50 {
51 m_data->m_name = name;
52 }
53
54 Context *context() const
55 {
56 return m_data->m_context.get();
57 }
58
59 void setContext(Context *context)
60 {
61 m_data->m_context.reset(context);
62 }
63
64 double value() const
65 {
66 return m_data->m_value;
67 }
68
69 void setValue(double value)
70 {
71 m_data->m_value = value;
72 }
73
74 // operator== is used for symbolics
75 bool equals(const Variable &other) const
76 {
77 return m_data == other.m_data;
78 }
79
80 Variable& operator=(const Variable&) = default;
81
82 Variable& operator=(Variable&&) noexcept = default;
83
84 private:
85 class VariableData : public SharedData
86 {
87
88 public:
89 VariableData(std::string name, Context *context) : SharedData(),
90 m_name(std::move(name)),
91 m_context(context),
92 m_value(0.0) {}
93
94 VariableData(const char *name, Context *context) : SharedData(),
95 m_name(name),
96 m_context(context),
97 m_value(0.0) {}
98
99 ~VariableData() = default;
100
101 std::string m_name;
102 std::unique_ptr<Context> m_context;
103 double m_value;
104
105 private:
106 VariableData(const VariableData &other);
107
108 VariableData &operator=(const VariableData &other);
109 };
110
111 SharedDataPtr<VariableData> m_data;
112
113 friend bool operator<(const Variable &lhs, const Variable &rhs)
114 {
115 return lhs.m_data < rhs.m_data;
116 }
117 };
118
119 } // namespace kiwi