Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

PROPERTY Macro

By: coder0xff on Aug 7th, 2012  |  syntax: C++  |  size: 1.86 KB  |  views: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #ifndef Core_Property_h
  2. #define Core_Property_h
  3.  
  4. #include "Util/Util.h"
  5. #include <stdint.h>
  6.  
  7. #define ENABLE_PROPERTIES(...) typedef __VA_ARGS__ _core_property_h_thisClass_
  8.  
  9. #define TYPE() type
  10.  
  11. #define GETTER(type, name) type Get##name()
  12. #define SETTER(type, name) type & Set##name(type & value)
  13.  
  14. #define PROPERTY(type, name, ...) \
  15.         __VA_ARGS__ \
  16.         typedef type _core_property_h_##name##type; \
  17.         class UNIQUE_TOKEN(_core_property_h_property_class_) \
  18.         { \
  19.         public: \
  20.                 UNIQUE_TOKEN(_core_property_h_property_class_) (type& initialValue) { GetContainingInstance()->Set##name(initialValue); } \
  21.                 operator type() { return GetContainingInstance()->Get##name(); } \
  22.                 template<typename T> \
  23.                 T& operator =(T& other) { GetContainingInstance()->Set##name(other); return other; } \
  24.         private: \
  25.                 _core_property_h_thisClass_* GetContainingInstance() { return  (_core_property_h_thisClass_*)((int8_t*)(this) - offsetof(_core_property_h_thisClass_, UNIQUE_TOKEN(_core_property_h_property_datamember_))); } \
  26.         }; \
  27.         UNIQUE_TOKEN(_core_property_h_property_class_) UNIQUE_TOKEN(_core_property_h_property_datamember_) \
  28.  
  29. //For non Standard-Layout classes as per C++11. Requires initialization because offsetof is not permitted
  30. #define NON_STANDARD_LAYOUT_PROPERTY(type, name, ...) \
  31.         __VA_ARGS__ \
  32.         class UNIQUE_TOKEN(_core_property_h_property_class_) \
  33.         { \
  34.         public: \
  35.                 UNIQUE_TOKEN(_core_property_h_property_class_) (_core_property_h_thisClass_& container, type& initialValue) : container(container) { container.Set##name(initialValue); } \
  36.                 operator type() { return container.Get##name(); } \
  37.                 template<typename T> \
  38.                 T& operator =(T& other) { container.Set##name(other); return other; } \
  39.         private: \
  40.                 _core_property_h_thisClass_& container; \
  41.         }; \
  42.         UNIQUE_TOKEN(_core_property_h_property_class_) UNIQUE_TOKEN(_core_property_h_property_datamember_) \
  43.  
  44. #endif