Advertisement
rockdrilla

field<T, Tdefault> template

Aug 11th, 2015
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 KB | None | 0 0
  1. template<typename T, T Tdefault = T()>
  2. struct field final {
  3.         typedef T value_type;
  4.  
  5.         static constexpr const T &default_value = Tdefault;
  6.  
  7.         struct storage;
  8.         struct behavior;
  9.  
  10.         class interface {
  11.                 /// add here other field::behavior:: classes
  12.                 friend class field::behavior::normal;
  13.  
  14.         protected:
  15.                 virtual bool _is_valid() const = 0;
  16.                 virtual T _get() const = 0;
  17.                 virtual void _set(const T &value) = 0;
  18.  
  19.         public:
  20.                 virtual operator T() const = 0;
  21.  
  22.                 virtual bool operator == (const T &value) const = 0;
  23.                 virtual bool operator == (const field::interface &other) const = 0;
  24.  
  25.                 virtual const field::interface& operator = (const T &value) = 0;
  26.                 virtual const field::interface& operator = (const field::interface &other) = 0;
  27.         };
  28.  
  29.         template<typename Cstorage = typename field::storage::plain, typename Cbehavior = typename field::behavior::normal>
  30.         class joint: public Cstorage, public Cbehavior, public virtual field::interface {
  31.         protected:
  32.                 using Cstorage::_is_valid;
  33.                 using Cstorage::_get;
  34.                 using Cstorage::_set;
  35.  
  36.         public:
  37.                 using Cstorage::Cstorage;
  38.  
  39.                 using Cbehavior::operator T;
  40.                 using Cbehavior::operator =;
  41.                 using Cbehavior::operator ==;
  42.         };
  43.  
  44.         struct behavior final {
  45.                 class normal: public virtual field::interface {
  46.                 public:
  47.                         // field::interface
  48.                         virtual operator T() const override {
  49.                                 if (!this->_is_valid()) return field::default_value;
  50.  
  51.                                 return this->_get();
  52.                         }
  53.  
  54.                         // field::interface
  55.                         virtual bool operator == (const T& value) const override {
  56.                                 if (!this->_is_valid()) return false;
  57.  
  58.                                 return (this->_get() == value);
  59.                         }
  60.  
  61.                         // field::interface
  62.                         virtual bool operator == (const field::interface& other) const override {
  63.                                 if (!this->_is_valid()) return false;
  64.                                 if (!other._is_valid()) return false;
  65.  
  66.                                 return (this->_get() == other._get());
  67.                         }
  68.  
  69.                         // field::interface
  70.                         virtual const field::interface& operator = (const T& value) override {
  71.                                 if (!this->_is_valid()) return *this;
  72.  
  73.                                 this->_set(value);
  74.                                 return *this;
  75.                         }
  76.  
  77.                         // field::interface
  78.                         virtual const field::interface& operator = (const field::interface& other) override {
  79.                                 if (!this->_is_valid()) return *this;
  80.                                 if (!other._is_valid()) return *this;
  81.  
  82.                                 this->_set(other._get());
  83.                                 return *this;
  84.                         }
  85.                 };
  86.         };
  87.  
  88.         struct storage final {
  89.                 class plain: public virtual field::interface {
  90.                         T _raw;
  91.  
  92.                 protected:
  93.                         // field::interface
  94.                         inline
  95.                         virtual bool _is_valid() const override {
  96.                                 return true;
  97.                         }
  98.  
  99.                         // field::interface
  100.                         virtual T _get() const {
  101.                                 return _raw;
  102.                         }
  103.  
  104.                         // field::interface
  105.                         virtual void _set(const T &value) {
  106.                                 _raw = value;
  107.                         }
  108.  
  109.                 public:
  110.                         plain() {
  111.                                 _raw = field::default_value;
  112.                         }
  113.  
  114.                         plain(const T &value) {
  115.                                 _raw = value;
  116.                         }
  117.                 };
  118.  
  119.                 class pointer: public virtual field::interface {
  120.                         T *_ptr;
  121.  
  122.                 protected:
  123.                         // field::interface
  124.                         virtual bool _is_valid() const override {
  125.                                 return (_ptr == nullptr);
  126.                         }
  127.  
  128.                         // field::interface
  129.                         virtual T _get() const override {
  130.                                 return *(_ptr);
  131.                         }
  132.  
  133.                         // field::interface
  134.                         virtual void _set(const T &value) override {
  135.                                 *(_ptr) = value;
  136.                         }
  137.  
  138.                 public:
  139.                         pointer() {
  140.                                 _ptr = nullptr;
  141.                         }
  142.  
  143.                         pointer(const T *ptr) {
  144.                                 _ptr = ptr;
  145.                         }
  146.                 };
  147.  
  148.                 class pointer2: public virtual field::interface {
  149.                         void **_base;
  150.                         size_t _offset;
  151.  
  152.                         const T* _ptr() const {
  153.                                 return reinterpret_cast<const T*>(
  154.                                         reinterpret_cast<size_t>(*_base) + _offset
  155.                                 );
  156.                         }
  157.  
  158.                         T* _ptr() {
  159.                                 return reinterpret_cast<T*>(
  160.                                         reinterpret_cast<size_t>(*_base) + _offset
  161.                                 );
  162.                         }
  163.  
  164.                 protected:
  165.                         // field::interface
  166.                         virtual bool _is_valid() const {
  167.                                 if (_base == nullptr) return false;
  168.                                 if (*_base == nullptr) return false;
  169.                                 return true;
  170.                         }
  171.  
  172.                         // field::interface
  173.                         virtual T _get() const override {
  174.                                 return *(_ptr());
  175.                         }
  176.  
  177.                         // field::interface
  178.                         virtual void _set(T value) override {
  179.                                 *(_ptr()) = value;
  180.                         }
  181.  
  182.                 public:
  183.                         pointer2() {
  184.                                 _base = nullptr;
  185.                                 _offset = 0;
  186.                         }
  187.  
  188.                         template<typename B = void>
  189.                         pointer2(const B * const *base_ptr, const T *offset_ptr)
  190.                         : pointer2() {
  191.                                 if (base_ptr == nullptr) return;
  192.  
  193.                                 ptrdiff_t offset;
  194.                                 offset =  reinterpret_cast<ptrdiff_t>(offset_ptr);
  195.                                 offset -= reinterpret_cast<ptrdiff_t>(*base_ptr);
  196.  
  197.                                 if (offset < 0) return;
  198.  
  199.                                 _base = reinterpret_cast<void**>(base_ptr);
  200.                                 _offset = static_cast<size_t>(offset);
  201.                         }
  202.                 };
  203.         };
  204. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement