Created
March 24, 2022 08:42
-
-
Save Frityet/268106b5adec74d876563314f92b04af to your computer and use it in GitHub Desktop.
This file contains 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
template<typename T, T (*Get_F)(T *), void (*Set_F)(T *, T)> | |
class Property { | |
private: | |
T field_; | |
public: | |
explicit Property(T value) | |
{ this->field_ = value; } | |
[[nodiscard]] | |
T get() const | |
{ return Get_F(&(this->field_)); } | |
void set(T value) const | |
{ Set_F((T *)(&(this->field_)), value); } | |
Property<T, Get_F, Set_F> &operator =(T value) | |
{ | |
this->set(value); | |
return *this; | |
} | |
operator T() | |
{ return this->field_; } | |
}; | |
template<typename T, T (*Get_F)(T *) = [](T *field) { | |
return *field; | |
}, void (*Set_F)(T *, T) = [](T *field, T value) { | |
*field = value; | |
}> | |
using property = class Property<T, Get_F, Set_F>; | |
int main() | |
{ | |
puts("Start"); | |
auto prop = property<int, | |
[](int *i) { | |
printf("%d\n", *i); | |
return *i; | |
}, | |
[](int *ptr, int i) { | |
printf("%d, %d\n", *ptr, i); | |
*ptr = i; | |
}>(64); | |
prop = 56; | |
putchar(prop); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
error: