-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.h
71 lines (56 loc) · 1.49 KB
/
Property.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include "Subject.h"
class Property;
typedef std::shared_ptr<Property> PropertyPtr;
class Property : public SubjectImplementation {
public:
Property(const std::string& name) : SubjectImplementation(name) {}
virtual ~Property() {};
template <typename T>
void set(const T& m) {
const MessageType msg(m);
setValue(msg);
}
void set(const char* m) {
set(std::string(m));
}
template <>
void set(const std::string& m);
template <typename T>
T getValue(T& m, T& defaultValue) {
if (get(m)) {
return m;
}
return defaultValue;
}
const char* getTypeName() const {
auto v = SubjectImplementation::getValue();
std::any a = v.getAny();
return a.type().name();
}
template <typename T>
bool get( T& m) {
auto v = SubjectImplementation::getValue();
std::any a = v.getAny();
if (a.has_value()) {
T* ptr = std::any_cast<T>(&a);
if (ptr) {
m = *ptr;
return true;
}
}
return false;
}
static std::pair<std::string, PropertyPtr> create(const std::string& name)
{
PropertyPtr p = std::make_shared<Property>(name);
return std::make_pair<>(name, p);
}
template <typename T>
static std::pair<std::string, PropertyPtr> create(const std::string& name, T initialValue)
{
PropertyPtr p = std::make_shared<Property>(name);
p->set(initialValue);
return std::make_pair<>(name, p);
}
};