fork download
  1. #ifndef _popcorn_property_hpp_
  2. #define _popcorn_property_hpp_
  3.  
  4. namespace popcorn
  5. {
  6. enum property_flag
  7. {
  8. read = 1 << 0,
  9. write = 1 << 1,
  10. readwrite = read | write,
  11. };
  12.  
  13. template<typename T, int flags = 0>
  14. struct property;
  15.  
  16. template<typename T>
  17. struct property<T, 0>
  18. { protected: T value; };
  19.  
  20. template<typename T>
  21. struct property<T, read> : protected virtual property<T, 0>
  22. {
  23. operator T&() { return this->value; }
  24. operator const T&() const { return this->value; }
  25. };
  26.  
  27. template<typename T>
  28. struct property<T, write> : protected virtual property<T, 0>
  29. {
  30. property<T, write>& operator=(T val) { this->value = val; return *this; }
  31. };
  32.  
  33. template<typename T>
  34. struct property<T, readwrite> : property<T, read>, property<T, write>
  35. {
  36. property<T, readwrite>& operator=(T val) { this->value = val; return *this; }
  37. };
  38. }
  39.  
  40. #endif
  41.  
  42. int main()
  43. {
  44. using namespace popcorn;
  45. property<int, read> read_only;
  46. property<int, write> write_only;
  47. property<int, read | write> read_write;
  48. property<int> no_access;
  49.  
  50. static_cast<int>(read_only);
  51. write_only = 42;
  52. static_cast<int>(read_write);
  53. read_write = 42;
  54. }
  55.  
Success #stdin #stdout 0.01s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty