|
2 | 2 |
|
3 | 3 | #include <string> |
4 | 4 | #include <memory> |
| 5 | +#include <vector> |
| 6 | +#include <map> |
5 | 7 | #include "parser.h" |
6 | 8 |
|
| 9 | + |
7 | 10 | namespace celengine |
8 | 11 | { |
9 | 12 | class BaseProperty; |
| 13 | + |
10 | 14 | class Config |
11 | 15 | { |
12 | 16 | public: |
13 | 17 | void setProperty(BaseProperty*); |
| 18 | + void removeProperty(BaseProperty*); |
14 | 19 | const Value operator[](const std::string& name); |
15 | | -}; |
16 | | - |
17 | | -class BaseProperty |
18 | | -{ |
19 | | - public: |
20 | | - virtual void update() = 0; |
21 | | - friend class Config; |
22 | | -}; |
23 | | - |
24 | | -template<typename T> |
25 | | -class Property : BaseProperty |
26 | | -{ |
27 | | - public: |
28 | | - Property() = default; |
29 | | - |
30 | | - Property(Config *config, std::string name) : |
31 | | - m_config(config), |
32 | | - m_name(std::move(name)) |
33 | | - { |
34 | | - m_config->setProperty(this); |
35 | | - }; |
36 | | - |
37 | | - Property(Config *config, std::string name, T value) : |
38 | | - m_config(config), |
39 | | - m_name(std::move(name)), |
40 | | - m_value(std::move(value)) |
41 | | - { |
42 | | - }; |
43 | 20 |
|
44 | | - // Getters and setters |
45 | | - inline Property& set(T value) |
46 | | - { |
47 | | - m_value = value; |
48 | | - }; |
49 | | - |
50 | | - Property& operator() (T value) |
51 | | - { |
52 | | - return set(value); |
53 | | - }; |
54 | | - |
55 | | - Property& operator=(T value) |
56 | | - { |
57 | | - return set(value); |
58 | | - }; |
59 | | - |
60 | | - T get() const |
61 | | - { |
62 | | - return m_value; |
63 | | - }; |
64 | | - |
65 | | - T operator()() const |
66 | | - { |
67 | | - return get(); |
68 | | - }; |
69 | | - |
70 | | - // Used by Config to propagate changes |
71 | | - void update() override; |
| 21 | + void beginUpdate(); |
| 22 | + void set(const std::string& name, const Value& value); |
| 23 | + void endUpdate(); |
72 | 24 |
|
73 | 25 | private: |
74 | | - Config *m_config { nullptr }; |
75 | | - std::string m_name; |
76 | | - T m_value {}; |
77 | | -}; |
| 26 | + void onUpdate(); |
78 | 27 |
|
79 | | -template<> |
80 | | -void Property<double>::update() |
81 | | -{ |
82 | | - m_value = (*m_config)[m_name].getNumber(); |
83 | | -} |
84 | | - |
85 | | -template<> |
86 | | -void Property<std::string>::update() |
87 | | -{ |
88 | | - m_value = (*m_config)[m_name].getString(); |
89 | | -} |
| 28 | + std::vector<BaseProperty*> m_props; |
| 29 | + std::map<std::string, Value> m_values; |
90 | 30 |
|
91 | | -template<> |
92 | | -void Property<bool>::update() |
93 | | -{ |
94 | | - m_value = (*m_config)[m_name].getBoolean(); |
95 | | -} |
| 31 | + bool m_update { false }; |
| 32 | +}; |
96 | 33 |
|
97 | | -Property<bool> p; |
98 | 34 | } // namespace; |
0 commit comments