fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. template <typename T>
  5. struct property {
  6. typedef std::function<T(T)> filter_function;
  7. typedef T value_type;
  8.  
  9. property() { }
  10.  
  11. property(value_type value)
  12. : m_value(value)
  13. { }
  14.  
  15. property(value_type value, filter_function onGet)
  16. : m_value(value)
  17. , m_onGet(onGet)
  18. { }
  19.  
  20. property(value_type value, filter_function onGet, filter_function onSet)
  21. : m_value(value)
  22. , m_onGet(onGet)
  23. , m_onSet(onSet)
  24. { }
  25.  
  26. operator value_type() const {
  27. if (m_onGet) {
  28. return m_onGet(m_value);
  29. }
  30. return m_value;
  31. }
  32.  
  33. property& operator=(value_type value) {
  34. if (m_onSet) {
  35. m_value = m_onSet(value);
  36. } else {
  37. m_value = value;
  38. }
  39. return *this;
  40. }
  41.  
  42. value_type m_value;
  43. bool m_readOnly;
  44. filter_function m_onGet;
  45. filter_function m_onSet;
  46. };
  47.  
  48.  
  49. class FuckingClass {
  50. public:
  51. property<std::string> fuckingProperty;
  52.  
  53. FuckingClass()
  54. {
  55. fuckingProperty = property<std::string>("hello",
  56. std::bind(&FuckingClass::OnFuckingPropertyGet, this, std::placeholders::_1),
  57. std::bind(&FuckingClass::OnFuckingPropertySet, this, std::placeholders::_1)
  58. );
  59. }
  60.  
  61. std::string OnFuckingPropertyGet(std::string s) {
  62. std::cout << "Getting fuckingProperty original value=" << s << std::endl;
  63. return "learn c++ dude";
  64. }
  65.  
  66. std::string OnFuckingPropertySet(std::string s) {
  67. std::cout << "Setting fuckingProperty to value=" << s << std::endl;
  68. return s;
  69. }
  70. };
  71.  
  72. int main(int argc, char **argv) {
  73. FuckingClass instance;
  74. instance.fuckingProperty = "world";
  75. std::string s = instance.fuckingProperty;
  76. std::cout << "s = " << s << std::endl;
  77. return 0;
  78. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Setting fuckingProperty to value=world
Getting fuckingProperty original value=world
s = learn c++ dude