You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GetterSetter.hpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef GETTERSETTER_H
  2. #define GETTERSETTER_H
  3. #include <functional>
  4. template<typename T>
  5. class GetterSetter
  6. {
  7. public:
  8. explicit GetterSetter(std::function<T(const T&)> getter, std::function<T(const T&)> setter, T value = T())
  9. : getter(getter), setter(setter) {
  10. operator=(value);
  11. }
  12. explicit GetterSetter(T value = T()){
  13. std::function<T(const T&)> dummy = [](const T& value) -> T{
  14. return value;
  15. };
  16. getter = dummy;
  17. setter = dummy;
  18. operator=(value);
  19. }
  20. virtual ~GetterSetter(){}
  21. operator T(){
  22. return getter(_value);
  23. }
  24. void operator =(const T &value){
  25. _value = setter(value);
  26. }
  27. bool operator ==(const T &value){
  28. return getter(_value) == value;
  29. }
  30. void operator +=(const T &value){
  31. _value = setter(getter(_value) + value);
  32. }
  33. void operator -=(const T &value){
  34. _value = setter(getter(_value) - value);
  35. }
  36. void operator *=(const T &value){
  37. _value = setter(getter(_value) * value);
  38. }
  39. void operator /=(const T &value){
  40. _value = setter(getter(_value) / value);
  41. }
  42. void operator &=(const T &value){
  43. _value = setter(getter(_value) & value);
  44. }
  45. void operator |=(const T &value){
  46. _value = setter(getter(_value) | value);
  47. }
  48. void operator ^=(const T &value){
  49. _value = setter(getter(_value) ^ value);
  50. }
  51. T operator ~(){
  52. return ~getter(_value);
  53. }
  54. private:
  55. T _value;
  56. std::function<T(const T&)> getter;
  57. std::function<T(const T&)> setter;
  58. };
  59. template<typename T, typename V>
  60. T operator+(GetterSetter<T> lhs, const V &rhs)
  61. {
  62. lhs += rhs;
  63. T result = lhs;
  64. return result;
  65. }
  66. template<typename T, typename V>
  67. T operator+(const V &lhs, GetterSetter<T> &rhs)
  68. {
  69. T value = rhs;
  70. return operator+(lhs, value);
  71. }
  72. template<typename T, typename V>
  73. T operator-(GetterSetter<T> lhs, const V &rhs)
  74. {
  75. lhs -= rhs;
  76. T result = lhs;
  77. return result;
  78. }
  79. template<typename T, typename V>
  80. T operator-(const V &lhs, GetterSetter<T> &rhs)
  81. {
  82. T value = rhs;
  83. return operator-(lhs, value);
  84. }
  85. template<typename T, typename V>
  86. T operator*(GetterSetter<T> lhs, const V &rhs)
  87. {
  88. lhs *= rhs;
  89. T result = lhs;
  90. return result;
  91. }
  92. template<typename T, typename V>
  93. T operator*(const V &lhs, GetterSetter<T> &rhs)
  94. {
  95. T value = rhs;
  96. return operator*(lhs, value);
  97. }
  98. template<typename T, typename V>
  99. T operator/(GetterSetter<T> lhs, const V &rhs)
  100. {
  101. lhs /= rhs;
  102. T result = lhs;
  103. return result;
  104. }
  105. template<typename T, typename V>
  106. T operator/(const V &lhs, GetterSetter<T> &rhs)
  107. {
  108. T value = rhs;
  109. return operator/(lhs, value);
  110. }
  111. template<typename T, typename V>
  112. T operator&(GetterSetter<T> lhs, const V &rhs)
  113. {
  114. lhs &= rhs;
  115. T result = lhs;
  116. return result;
  117. }
  118. template<typename T, typename V>
  119. T operator&(const V &lhs, GetterSetter<T> &rhs)
  120. {
  121. T value = rhs;
  122. return operator&(lhs, value);
  123. }
  124. template<typename T, typename V>
  125. T operator|(GetterSetter<T> lhs, const V &rhs)
  126. {
  127. lhs |= rhs;
  128. T result = lhs;
  129. return result;
  130. }
  131. template<typename T, typename V>
  132. T operator|(const V &lhs, GetterSetter<T> &rhs)
  133. {
  134. T value = rhs;
  135. return operator|(lhs, value);
  136. }
  137. template<typename T, typename V>
  138. T operator^(GetterSetter<T> lhs, const V &rhs)
  139. {
  140. lhs ^= rhs;
  141. T result = lhs;
  142. return result;
  143. }
  144. template<typename T, typename V>
  145. T operator^(const V &lhs, GetterSetter<T> &rhs)
  146. {
  147. T value = rhs;
  148. return operator^(lhs, value);
  149. }
  150. #endif // GETTERSETTER_H