Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T, T Tdefault = T()>
- struct field final {
- typedef T value_type;
- static constexpr const T &default_value = Tdefault;
- struct storage;
- struct behavior;
- class interface {
- /// add here other field::behavior:: classes
- friend class field::behavior::normal;
- protected:
- virtual bool _is_valid() const = 0;
- virtual T _get() const = 0;
- virtual void _set(const T &value) = 0;
- public:
- virtual operator T() const = 0;
- virtual bool operator == (const T &value) const = 0;
- virtual bool operator == (const field::interface &other) const = 0;
- virtual const field::interface& operator = (const T &value) = 0;
- virtual const field::interface& operator = (const field::interface &other) = 0;
- };
- template<typename Cstorage = typename field::storage::plain, typename Cbehavior = typename field::behavior::normal>
- class joint: public Cstorage, public Cbehavior, public virtual field::interface {
- protected:
- using Cstorage::_is_valid;
- using Cstorage::_get;
- using Cstorage::_set;
- public:
- using Cstorage::Cstorage;
- using Cbehavior::operator T;
- using Cbehavior::operator =;
- using Cbehavior::operator ==;
- };
- struct behavior final {
- class normal: public virtual field::interface {
- public:
- // field::interface
- virtual operator T() const override {
- if (!this->_is_valid()) return field::default_value;
- return this->_get();
- }
- // field::interface
- virtual bool operator == (const T& value) const override {
- if (!this->_is_valid()) return false;
- return (this->_get() == value);
- }
- // field::interface
- virtual bool operator == (const field::interface& other) const override {
- if (!this->_is_valid()) return false;
- if (!other._is_valid()) return false;
- return (this->_get() == other._get());
- }
- // field::interface
- virtual const field::interface& operator = (const T& value) override {
- if (!this->_is_valid()) return *this;
- this->_set(value);
- return *this;
- }
- // field::interface
- virtual const field::interface& operator = (const field::interface& other) override {
- if (!this->_is_valid()) return *this;
- if (!other._is_valid()) return *this;
- this->_set(other._get());
- return *this;
- }
- };
- };
- struct storage final {
- class plain: public virtual field::interface {
- T _raw;
- protected:
- // field::interface
- inline
- virtual bool _is_valid() const override {
- return true;
- }
- // field::interface
- virtual T _get() const {
- return _raw;
- }
- // field::interface
- virtual void _set(const T &value) {
- _raw = value;
- }
- public:
- plain() {
- _raw = field::default_value;
- }
- plain(const T &value) {
- _raw = value;
- }
- };
- class pointer: public virtual field::interface {
- T *_ptr;
- protected:
- // field::interface
- virtual bool _is_valid() const override {
- return (_ptr == nullptr);
- }
- // field::interface
- virtual T _get() const override {
- return *(_ptr);
- }
- // field::interface
- virtual void _set(const T &value) override {
- *(_ptr) = value;
- }
- public:
- pointer() {
- _ptr = nullptr;
- }
- pointer(const T *ptr) {
- _ptr = ptr;
- }
- };
- class pointer2: public virtual field::interface {
- void **_base;
- size_t _offset;
- const T* _ptr() const {
- return reinterpret_cast<const T*>(
- reinterpret_cast<size_t>(*_base) + _offset
- );
- }
- T* _ptr() {
- return reinterpret_cast<T*>(
- reinterpret_cast<size_t>(*_base) + _offset
- );
- }
- protected:
- // field::interface
- virtual bool _is_valid() const {
- if (_base == nullptr) return false;
- if (*_base == nullptr) return false;
- return true;
- }
- // field::interface
- virtual T _get() const override {
- return *(_ptr());
- }
- // field::interface
- virtual void _set(T value) override {
- *(_ptr()) = value;
- }
- public:
- pointer2() {
- _base = nullptr;
- _offset = 0;
- }
- template<typename B = void>
- pointer2(const B * const *base_ptr, const T *offset_ptr)
- : pointer2() {
- if (base_ptr == nullptr) return;
- ptrdiff_t offset;
- offset = reinterpret_cast<ptrdiff_t>(offset_ptr);
- offset -= reinterpret_cast<ptrdiff_t>(*base_ptr);
- if (offset < 0) return;
- _base = reinterpret_cast<void**>(base_ptr);
- _offset = static_cast<size_t>(offset);
- }
- };
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement