Skip to content

Instantly share code, notes, and snippets.

@tmr232
Created July 31, 2017 12:31
Show Gist options
  • Save tmr232/6442a20fadcf2bfc7f5f2ea0b8b9c8a7 to your computer and use it in GitHub Desktop.
Save tmr232/6442a20fadcf2bfc7f5f2ea0b8b9c8a7 to your computer and use it in GitHub Desktop.
C++ property POC
#include <functional>
#include <cstdio>
template <class T>
class Property {
public:
using Setter = std::function<void(T)>;
using Getter = std::function<T()>;
Property(Setter setter, Getter getter) :_setter{setter}, _getter{getter}
{
}
Property(const Property&) = delete;
Property(Property&&) = delete;
Property& operator=(const T& value) {
_setter(value);
return *this;
}
operator T() const {
return _getter();
}
private:
Setter _setter;
Getter _getter;
};
class WithProps {
public:
Property<int> X{
[&](int x){_x = x;},
[&]{return _x;}
};
private:
int _x;
};
int main() {
WithProps wp;
wp.X = 2;
int x = wp.X;
printf("%d\n", (int)wp.X);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment