-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperty.h
More file actions
93 lines (70 loc) · 2.37 KB
/
Property.h
File metadata and controls
93 lines (70 loc) · 2.37 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include <utility>
#include <type_traits>
namespace tower120::utils {
template<class Getter, class Setter>
class Property {
Getter getter;
Setter setter;
using GetT = decltype(getter());
static_assert(std::is_object_v<GetT> || std::is_const_v<std::remove_reference_t<GetT>>
,"Property Getter must return const ref/pointer or object.");
// from Boost
template<typename T>
class arrow_proxy {
T t;
public:
arrow_proxy(T &&t) : t(std::move(t)) {}
const T *operator->() const { return &t; }
};
public:
// require c++17 guaranteed rvo without move ctr
Property(const Property &) = delete;
Property(Property &&) = delete;
Property(const Getter &getter, const Setter &setter)
: getter(getter), setter(setter) {}
// getters
decltype(auto) get() const {
return getter();
}
operator GetT() const {
return get();
}
decltype(auto) operator*() const {
return get();
}
decltype(auto) operator->() const {
if constexpr (std::is_reference_v<GetT>) {
return &get();
} else if constexpr (std::is_pointer_v<GetT>) {
return get();
} else {
// is value
return arrow_proxy<GetT>(get());
}
}
// setters
// args... here, so you can use them with lambda overload
template<class ...Args>
void set(Args &&... args) {
setter(std::forward<Args>(args)...);
}
template<class Value>
void operator=(Value &&value) {
set(std::forward<Value>(value));
}
// forward comparison operators (do we need them?)
template<class OtherGetter, class OtherSetter>
bool operator==(const Property<OtherGetter, OtherSetter> &other) const {
return get() == other.get();
}
template<class OtherGetter, class OtherSetter>
bool operator!=(const Property<OtherGetter, OtherSetter> &other) const {
return get() != other.get();
}
auto make_const() const {
struct empty{};
return Property<Getter, empty>(getter, empty{});
}
};
}