Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////
- // smart_value<T> definition
- ////////////////////////////////////////////
- template<typename T, T Tdefault = T(), T Tinvalid = Tdefault>
- class smart_value final {
- smart_value() {}
- public:
- class interface {
- public:
- virtual bool smart_value__is_valid(T value) const = 0;
- virtual bool smart_value__is_valid() const = 0;
- virtual T smart_value__get() const = 0;
- virtual void smart_value__set(T value) = 0;
- virtual operator T() const = 0;
- virtual bool operator == (const T& value) const = 0;
- virtual bool operator == (const interface& other) const = 0;
- virtual const interface& operator = (const T& value) = 0;
- virtual const interface& operator = (const interface& other) = 0;
- static constexpr const T& smart_value__default = Tdefault;
- static constexpr const T& smart_value__invalid = Tinvalid;
- };
- private:
- class base: public virtual smart_value::interface {
- public:
- base() {}
- virtual T smart_value__get() const override = 0;
- virtual void smart_value__set(T value) override = 0;
- inline virtual bool smart_value__is_valid(T value) const override {
- return true;
- }
- inline virtual bool smart_value__is_valid() const override {
- return true;
- }
- inline virtual operator T() const override {
- return smart_value__get();
- }
- inline virtual bool operator == (const T& value) const override {
- return (smart_value__get() == value);
- }
- inline virtual bool operator == (const smart_value::interface& other) const override {
- return (smart_value__get() == other.smart_value__get());
- }
- inline virtual const smart_value::interface& operator = (const T& value) override {
- smart_value__set(value);
- return *this;
- }
- inline virtual const smart_value::interface& operator = (const smart_value::interface& other) override {
- smart_value__set(other.smart_value__get());
- return *this;
- }
- };
- public:
- class container: public smart_value::base, public virtual smart_value::interface {
- T _raw;
- public:
- using smart_value::base::operator=;
- using smart_value::base::operator==;
- using smart_value::base::smart_value__is_valid;
- container() { smart_value__set(smart_value::interface::smart_value__default); }
- container(T value) { smart_value__set(value); }
- // smart_value::interface
- inline virtual T smart_value__get() const override {
- if (!smart_value__is_valid()) return smart_value::interface::smart_value__default;
- return _raw;
- };
- // smart_value::interface
- inline virtual void smart_value__set(T value) override {
- if (!smart_value__is_valid()) return;
- if (!smart_value__is_valid(value)) return;
- _raw = value;
- }
- };
- class pointer: public smart_value::base, public virtual smart_value::interface {
- T* _ptr;
- public:
- using smart_value::base::operator=;
- using smart_value::base::operator==;
- using smart_value::base::smart_value__is_valid;
- pointer() { _ptr = nullptr; }
- pointer(T* ptr) { _ptr = ptr; }
- // smart_value::interface
- inline virtual T smart_value__get() const override {
- if (!smart_value__is_valid()) return smart_value::interface::smart_value__default;
- return *(_ptr);
- };
- // smart_value::interface
- inline virtual void smart_value__set(T value) override {
- if (!smart_value__is_valid()) return;
- if (!smart_value__is_valid(value)) return;
- *(_ptr) = value;
- }
- // smart_value::interface
- inline virtual bool smart_value__is_valid() const override {
- return (_ptr == nullptr);
- }
- // smart_value::interface
- inline bool operator == (const T& value) const override {
- if (!smart_value__is_valid()) return false;
- return (*(_ptr) == value);
- }
- inline bool operator == (const T* ptr) const {
- return (_ptr == ptr);
- }
- inline bool operator == (const smart_value::pointer& other) const {
- return (_ptr == other._ptr);
- }
- inline const smart_value::interface& operator = (const T* ptr) {
- _ptr = ptr;
- return *this;
- }
- inline const smart_value::interface& operator = (const smart_value::pointer& other) {
- _ptr = other._ptr;
- return *this;
- }
- };
- class deep_pointer: public smart_value::base, public virtual smart_value::interface {
- void** _base;
- size_t _offset;
- inline const T* _ptr() const {
- return reinterpret_cast<const T*>(reinterpret_cast<size_t>(*_base) + _offset);
- }
- inline T* _ptr() {
- return reinterpret_cast<T*>(reinterpret_cast<size_t>(*_base) + _offset);
- }
- public:
- using smart_value::base::operator=;
- using smart_value::base::operator==;
- using smart_value::base::smart_value__is_valid;
- deep_pointer() {
- _base = nullptr;
- _offset = 0;
- }
- template<typename B = void>
- deep_pointer(B** base_ptr, T* offset_ptr): deep_pointer() {
- if (base_ptr == nullptr) return;
- long loffset = reinterpret_cast<long>(offset_ptr) - reinterpret_cast<long>(*base_ptr);
- if (loffset < 0) return;
- _base = reinterpret_cast<void**>(base_ptr);
- _offset = static_cast<size_t>(loffset);
- }
- // smart_value::interface
- inline virtual bool smart_value__is_valid() const override {
- if (_base == nullptr) return false;
- if (*_base == nullptr) return false;
- return true;
- }
- // smart_value::interface
- inline virtual T smart_value__get() const override {
- if (!smart_value__is_valid()) return smart_value::interface::smart_value__default;
- return *(_ptr());
- };
- // smart_value::interface
- inline virtual void smart_value__set(T value) override {
- if (!smart_value__is_valid()) return;
- if (!smart_value__is_valid(value)) return;
- *(_ptr()) = value;
- }
- // smart_value::interface
- inline bool operator == (const T& value) const override {
- if (!smart_value__is_valid()) return false;
- return (*(_ptr()) == value);
- }
- inline bool operator == (const T* ptr) const {
- return (_ptr() == ptr);
- }
- inline bool operator == (const smart_value::deep_pointer& other) const {
- return (_base == other._base) && (_offset == other._offset);
- }
- inline const smart_value::interface& operator = (const smart_value::deep_pointer& other) {
- _base = other._base;
- _offset = other._offset;
- return *this;
- }
- };
- template<class Cdst, class Cmid>
- class proxy: public Cdst, public virtual smart_value::interface {
- mutable Cmid _mid;
- static void constraints(void) {
- Cdst a1; static_cast<smart_value::interface&>(a1);
- Cmid a2; dynamic_cast<smart_value::interface&>(a2);
- }
- public:
- using Cdst::Cdst;
- using Cdst::operator=;
- using Cdst::operator==;
- using Cdst::smart_value__is_valid;
- proxy(): Cdst() { void(*p)() = constraints; }
- // smart_value::interface
- inline virtual T smart_value__get() const override {
- if (!smart_value__is_valid()) return smart_value::interface::smart_value__default;
- _mid.smart_value__set(Cdst::smart_value__get());
- if (!_mid.smart_value__is_valid()) return smart_value::interface::smart_value__default;
- return _mid.smart_value__get();
- };
- // smart_value::interface
- inline virtual void smart_value__set(T value) override {
- if (!smart_value__is_valid()) return;
- if (!smart_value__is_valid(value)) return;
- if (!_mid.smart_value__is_valid(value)) return;
- _mid.smart_value__set(value);
- Cdst::smart_value__set(_mid.smart_value__get());
- }
- };
- };
- ////////////////////////////////////////////
- // smart_value<T> usage examples
- ////////////////////////////////////////////
- ////////////////////////////////////////////
- // example #1 : simple
- ////////////////////////////////////////////
- class asn1 final {
- asn1() {}
- public:
- class value: public smart_value<unsigned char, 0x00, 0xFF>::container {
- public:
- using container::container;
- using container::operator=;
- using container::operator==;
- using container::smart_value__is_valid;
- using container::smart_value__get;
- using container::smart_value__set;
- };
- class tag final: public value {
- public:
- using value::value;
- using value::operator=;
- using value::operator==;
- using value::smart_value__is_valid;
- using value::smart_value__get;
- using value::smart_value__set;
- static const tag& eoc; // 0x00
- static const tag& boolean; // 0x01
- static const tag& integer; // 0x02
- static const tag& bit_string; // 0x03
- static const tag& octet_string; // 0x04
- static const tag& null; // 0x05
- static const tag& object_id; // 0x06
- static const tag& object_descriptor; // 0x07
- static const tag& relative_oid; // 0x0D
- static const tag& sequence; // 0x10
- static const tag& set; // 0x11
- static const tag& long_form; // 0x1F
- };
- class type final: public value {
- public:
- using value::value;
- using value::operator=;
- using value::operator==;
- using value::smart_value__is_valid;
- using value::smart_value__get;
- using value::smart_value__set;
- static const type& primitive; // 0x00
- static const type& complex; // 0x20
- };
- class typeclass final: public value {
- public:
- using value::value;
- using value::operator=;
- using value::operator==;
- using value::smart_value__is_valid;
- using value::smart_value__get;
- using value::smart_value__set;
- static const typeclass& universal; // 0x00
- static const typeclass& application; // 0x40
- static const typeclass& context; // 0x80
- static const typeclass& private_usage; // 0xC0
- };
- };
- const asn1::tag& asn1::tag::eoc(0x00);
- const asn1::tag& asn1::tag::boolean(0x01);
- const asn1::tag& asn1::tag::integer(0x02);
- const asn1::tag& asn1::tag::bit_string(0x03);
- const asn1::tag& asn1::tag::octet_string(0x04);
- const asn1::tag& asn1::tag::null(0x05);
- const asn1::tag& asn1::tag::object_id(0x06);
- const asn1::tag& asn1::tag::object_descriptor(0x07);
- const asn1::tag& asn1::tag::relative_oid(0x0D);
- const asn1::tag& asn1::tag::sequence(0x10);
- const asn1::tag& asn1::tag::set(0x11);
- const asn1::tag& asn1::tag::long_form(0x1F);
- const asn1::type& asn1::type::primitive(0x00);
- const asn1::type& asn1::type::complex(0x20);
- const asn1::typeclass& asn1::typeclass::universal(0x00);
- const asn1::typeclass& asn1::typeclass::application(0x40);
- const asn1::typeclass& asn1::typeclass::context(0x80);
- const asn1::typeclass& asn1::typeclass::private_usage(0xC0);
- ////////////////////////////////////////////
- // example #2 : a little bit complex
- ////////////////////////////////////////////
- class pdutype final: public smart_value<int, 0, 0xFFFF>::container {
- public:
- using container::container;
- using container::operator=;
- using container::operator==;
- using container::smart_value__is_valid;
- using container::smart_value__get;
- using container::smart_value__set;
- inline bool smart_value__is_valid(int value) const override;
- static const pdutype& get; // 160
- static const pdutype& getnext; // 161
- static const pdutype& response; // 162
- static const pdutype& set; // 163
- static const pdutype& trap; // 164
- static const pdutype& getbulk; // 165
- static const pdutype& inform; // 166
- static const pdutype& trap2; // 167
- static const pdutype& report; // 168
- };
- inline bool pdutype::smart_value__is_valid(int value) const {
- switch(value) {
- case 160:
- case 161:
- case 162:
- case 163:
- case 164:
- case 165:
- case 166:
- case 167:
- case 168:
- return true;
- default:
- return false;
- }
- }
- const pdutype& pdutype::get(160);
- const pdutype& pdutype::getnext(161);
- const pdutype& pdutype::response(162);
- const pdutype& pdutype::set(163);
- const pdutype& pdutype::trap(164);
- const pdutype& pdutype::getbulk(165);
- const pdutype& pdutype::inform(166);
- const pdutype& pdutype::trap2(167);
- const pdutype& pdutype::report(168);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement