Created
July 31, 2017 12:31
-
-
Save tmr232/6442a20fadcf2bfc7f5f2ea0b8b9c8a7 to your computer and use it in GitHub Desktop.
C++ property POC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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