Skip to content

Commit d37f2e7

Browse files
committed
wip
1 parent d01691d commit d37f2e7

4 files changed

Lines changed: 182 additions & 89 deletions

File tree

src/celengine/config.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "config.h"
2+
#include "property.h"
3+
4+
5+
namespace celengine
6+
{
7+
8+
void Config::setProperty(BaseProperty *p)
9+
{
10+
auto pos = std::find(m_props.begin(), m_props.end(), p);
11+
if (pos != m_props.end())
12+
m_props.push_back(p);
13+
}
14+
15+
void Config::removeProperty(BaseProperty *p)
16+
{
17+
auto pos = std::find(m_props.begin(), m_props.end(), p);
18+
if (pos != m_props.end())
19+
m_props.erase(pos);
20+
}
21+
22+
const Value Config::operator[](const std::string& name)
23+
{
24+
auto pos = m_values.find(name);
25+
if (pos != m_values.end())
26+
return pos->second;
27+
28+
return Value();
29+
}
30+
31+
void Config::beginUpdate()
32+
{
33+
m_update = true;
34+
}
35+
36+
void Config::set(const std::string& name, const Value& value)
37+
{
38+
m_values[name] = value;
39+
}
40+
41+
void Config::endUpdate()
42+
{
43+
m_update = false;
44+
onUpdate();
45+
}
46+
47+
void Config::onUpdate()
48+
{
49+
for (auto* p : m_props)
50+
p->update();
51+
}
52+
53+
} // namespace;

src/celengine/config.h

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,33 @@
22

33
#include <string>
44
#include <memory>
5+
#include <vector>
6+
#include <map>
57
#include "parser.h"
68

9+
710
namespace celengine
811
{
912
class BaseProperty;
13+
1014
class Config
1115
{
1216
public:
1317
void setProperty(BaseProperty*);
18+
void removeProperty(BaseProperty*);
1419
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-
};
4320

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();
7224

7325
private:
74-
Config *m_config { nullptr };
75-
std::string m_name;
76-
T m_value {};
77-
};
26+
void onUpdate();
7827

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;
9030

91-
template<>
92-
void Property<bool>::update()
93-
{
94-
m_value = (*m_config)[m_name].getBoolean();
95-
}
31+
bool m_update { false };
32+
};
9633

97-
Property<bool> p;
9834
} // namespace;

src/celengine/parser.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,45 @@ typedef AssociativeArray Hash;
7272

7373
class Value
7474
{
75-
public:
76-
enum ValueType {
77-
NumberType = 0,
78-
StringType = 1,
79-
ArrayType = 2,
80-
HashType = 3,
81-
BooleanType = 4
75+
public:
76+
enum ValueType
77+
{
78+
NullType = 0,
79+
NumberType = 1,
80+
StringType = 2,
81+
ArrayType = 3,
82+
HashType = 4,
83+
BooleanType = 5
8284
};
8385

8486
Value(double);
8587
Value(const string&);
8688
Value(ValueArray*);
8789
Value(Hash*);
8890
Value(bool);
91+
Value() = default;
8992
~Value();
9093

9194
ValueType getType() const;
9295

96+
bool isNull() const;
9397
double getNumber() const;
9498
string getString() const;
9599
ValueArray* getArray() const;
96100
Hash* getHash() const;
97101
bool getBoolean() const;
98102

99-
private:
100-
ValueType type;
101-
102-
union {
103+
private:
104+
union Data
105+
{
103106
string* s;
104107
double d;
105108
ValueArray* a;
106109
Hash* h;
107-
} data;
110+
};
111+
112+
ValueType type{ NullType };
113+
Data data;
108114
};
109115

110116

src/celengine/property.h

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

0 commit comments

Comments
 (0)