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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include <utility>
#include <type_traits>
#include <iostream>
template <typename T, typename MemberT>
class Guard1{
public:
typedef const MemberT &(T::*getter)() const;
typedef void (T::*setter)(const MemberT &) ;
private:
T *obj;
setter s;
MemberT old;
void release(){
if (!this->obj)
return;
(this->obj->*this->s)(old);
this->obj = nullptr;
}
public:
Guard1(): obj(nullptr){}
Guard1(T &obj, const MemberT &val, getter g, setter s): old((obj.*g)()){
this->obj = &obj;
this->s = s;
(this->obj->*this->s)(val);
}
Guard1(Guard1 &&other): old(std::move(other.old)){
this->obj = other.obj;
other.obj = nullptr;
this->s = other.s;
}
Guard1(const Guard1 &other) = delete;
~Guard1(){
this->release();
}
const Guard1 &operator=(Guard1 &&other){
this->release();
this->old = std::move(other.old);
this->obj = other.obj;
other.obj = nullptr;
this->s = other.s;
return *this;
}
};
template <typename T, typename MemberT>
class Guard2{
public:
typedef MemberT T::*member;
private:
T *obj;
member m;
MemberT old;
void release(){
if (!this->obj)
return;
this->obj->*this->m = std::move(this->old);
this->obj = nullptr;
}
public:
Guard2(): obj(nullptr){}
Guard2(T &obj, const MemberT &val, member m): old(obj.*m){
this->obj = &obj;
this->m = m;
this->obj->*this->m = val;
}
Guard2(Guard2 &&other): old(std::move(other.old)){
this->obj = other.obj;
other.obj = nullptr;
this->m = other.m;
}
Guard2(const Guard2 &other) = delete;
~Guard2(){
this->release();
}
const Guard2 &operator=(Guard2 &&other){
this->release();
this->old = std::move(other.old);
this->obj = other.obj;
other.obj = nullptr;
this->m = other.m;
return *this;
}
};
template <typename T, typename R>
R get_ret_type(const R &(T::*)() const){
return R();
};
template <typename T, typename R>
R get_ret_type(R (T::*)() const){
return R();
};
#define MAGIC1(obj, prop, val) \
Guard1<decltype(obj), decltype(get_ret_type(&decltype(obj)::get_##prop))> guard_##__COUNTER__(obj, val, &decltype(obj)::get_##prop, &decltype(obj)::set_##prop)
#define MAGIC2(obj, prop, val) \
Guard2<decltype(obj), decltype(decltype(obj)::prop)> guard_##__COUNTER__(obj, val, &decltype(obj)::prop)
#define DEFINE_GETTER(x) \
const decltype(x) &get_##x() const{ \
return this->x; \
}
//------------------------------------------------------------------------------
#define DEFINE_NON_CONST_GETTER(x) \
decltype(x) &get_##x(){ \
return this->x; \
}
#define DEFINE_GETTER_SETTER(x) \
DEFINE_GETTER(x) \
void set_##x(const decltype(x) &value){ \
this->x = value; \
}
class A{
int i = 0;
public:
DEFINE_GETTER_SETTER(i)
};
class B{
double x = 0;
public:
B() = default;
explicit B(double x): x(x){}
DEFINE_GETTER_SETTER(x)
};
std::ostream &operator<<(std::ostream &stream, const B &b){
return stream << "B(" << b.get_x() << ")";
}
class C{
B b;
public:
C() = default;
C(const B &b): b(b){}
DEFINE_GETTER_SETTER(b)
};
struct D{
int j = 0;
};
//------------------------------------------------------------------------------
int main(){
A a;
B b;
C c;
D d;
{
std::cout << a.get_i() << std::endl;
{
MAGIC1(a, i, 42);
std::cout << a.get_i() << std::endl;
}
std::cout << a.get_i() << std::endl;
}
{
std::cout << b.get_x() << std::endl;
{
MAGIC1(b, x, 3.141592);
std::cout << b.get_x() << std::endl;
}
std::cout << b.get_x() << std::endl;
}
{
std::cout << c.get_b() << std::endl;
{
MAGIC1(c, b, B(2.718282));
std::cout << c.get_b() << std::endl;
}
std::cout << c.get_b() << std::endl;
}
{
std::cout << d.j << std::endl;
{
MAGIC2(d, j, 24);
std::cout << d.j << std::endl;
}
std::cout << d.j << std::endl;
}
return 0;
}
| |