fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. template<typename T>
  5. struct property
  6. {
  7. public:
  8. typedef std::function<T()> getter;
  9. typedef std::function<void(T)> setter;
  10.  
  11. public:
  12. property(getter get, setter set)
  13. : get_(get)
  14. , set_(set)
  15. { }
  16.  
  17. operator T() const { return get_(); }
  18. property& operator=(T x) { set_(x); return *this; }
  19.  
  20. private:
  21. getter get_;
  22. setter set_;
  23. };
  24.  
  25. class Vec2
  26. {
  27. public:
  28. Vec2(int vx, int vy)
  29. : x(std::bind(&Vec2::get_x, this), std::bind(&Vec2::set_x, this, std::placeholders::_1))
  30. , y(std::bind(&Vec2::get_y, this), std::bind(&Vec2::set_y, this, std::placeholders::_1))
  31. , x_(vx)
  32. , y_(vy)
  33. { }
  34.  
  35. property<int> x;
  36. property<int> y;
  37.  
  38. protected:
  39. int get_x() { return x_; }
  40. void set_x(int x) { x_ = x; }
  41.  
  42. int get_y() { return y_; }
  43. void set_y(int y) { y_ = y; }
  44.  
  45. private:
  46. int x_, y_;
  47. };
  48.  
  49. class Vec3
  50. {
  51. public:
  52. Vec3(int vx, int vy, int vz)
  53. : x(std::bind(&Vec3::get_x, this), std::bind(&Vec3::set_x, this, std::placeholders::_1))
  54. , y(std::bind(&Vec3::get_y, this), std::bind(&Vec3::set_y, this, std::placeholders::_1))
  55. , z(std::bind(&Vec3::get_z, this), std::bind(&Vec3::set_z, this, std::placeholders::_1))
  56. , xy(std::bind(&Vec3::get_xy, this), std::bind(&Vec3::set_xy, this, std::placeholders::_1))
  57. , xz(std::bind(&Vec3::get_xz, this), std::bind(&Vec3::set_xz, this, std::placeholders::_1))
  58. , yz(std::bind(&Vec3::get_yz, this), std::bind(&Vec3::set_yz, this, std::placeholders::_1))
  59. , x_(vx)
  60. , y_(vy)
  61. , z_(vz)
  62. { }
  63.  
  64. property<int> x;
  65. property<int> y;
  66. property<int> z;
  67.  
  68. property<Vec2> xy;
  69. property<Vec2> xz;
  70. property<Vec2> yz;
  71.  
  72. protected:
  73. int get_x() { return x_; }
  74. void set_x(int x) { x_ = x; }
  75.  
  76. int get_y() { return y_; }
  77. void set_y(int y) { y_ = y; }
  78.  
  79. int get_z() { return z_; }
  80. void set_z(int z) { z_ = z; }
  81.  
  82. Vec2 get_xy() { return { x_, y_ }; }
  83. void set_xy(Vec2 xy) { x_ = xy.x; y_ = xy.y; }
  84.  
  85. Vec2 get_xz() { return { x_, z_ }; }
  86. void set_xz(Vec2 xz) { x_ = xz.x; z_ = xz.y; }
  87.  
  88. Vec2 get_yz() { return { y_, z_ }; }
  89. void set_yz(Vec2 yz) { y_ = yz.x; z_ = yz.y; }
  90.  
  91. private:
  92. int x_, y_, z_;
  93. };
  94.  
  95. std::ostream& operator<<(std::ostream& out, const Vec2& v2)
  96. {
  97. out << '[' << v2.x << ", " << v2.y << ']';
  98. return out;
  99. }
  100.  
  101. std::ostream& operator<<(std::ostream& out, const Vec3& v3)
  102. {
  103. out << '[' << v3.x << ", " << v3.y << ", " << v3.z << ']';
  104. return out;
  105. }
  106.  
  107. int main(int argc, char** argv)
  108. {
  109. Vec3 v3 { 2, 0, 1 };
  110. std::cout << v3 << std::endl;
  111. v3.y = 3;
  112. std::cout << v3.xy << std::endl;
  113. std::cout << v3.xz << std::endl;
  114. std::cout << v3.yz << std::endl;
  115.  
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0s 3488KB
stdin
Standard input is empty
stdout
[2, 0, 1]
[2, 3]
[2, 1]
[3, 1]