fork download
  1. namespace detail
  2. {
  3. template<typename T>
  4. struct dummy
  5. {
  6. T get() const { return T(); }
  7. void set(T) { }
  8. };
  9. }
  10.  
  11. #include <type_traits>
  12.  
  13. // Simple Property
  14. template<typename Value,
  15. typename ValueOwner = detail::dummy<Value>,
  16. Value (ValueOwner::*Getter)(void) const = &ValueOwner::get,
  17. void (ValueOwner::*Setter)(Value) = &ValueOwner::set,
  18. bool = std::is_same<ValueOwner, detail::dummy<Value>>::value
  19. >
  20. class Property {
  21. public:
  22.  
  23. Value get() const { return m_value; }
  24.  
  25. void set(Value value) { m_value = value; }
  26.  
  27. private:
  28.  
  29. Value m_value;
  30. };
  31.  
  32. // Complicated Property
  33. template<
  34. typename Value,
  35. typename ValueOwner,
  36. Value (ValueOwner::*Getter)(void) const,
  37. void (ValueOwner::*Setter)(Value)
  38. >
  39. class Property<Value, ValueOwner, Getter, Setter, false>
  40. {
  41. // Stuff for calling the getter & setter
  42. };
  43.  
  44. struct MyClass
  45. {
  46. int get() const { return 42; }
  47. void set(int) { }
  48. };
  49.  
  50. int main()
  51. {
  52. Property<int> simpleProperty;
  53. simpleProperty.get();
  54.  
  55. Property<int, MyClass, &MyClass::get, &MyClass::set> complicatedProperty;
  56. }
  57.  
Success #stdin #stdout 0s 2892KB
stdin
Standard input is empty
stdout
Standard output is empty