Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/evaluation/object.hpp

    rb276be5 r87c5f40  
    1919};
    2020
     21template<typename T>
     22std::type_index class_of() { return { typeid(T) }; }
     23
    2124class object {
    2225public:
     
    2528        template<typename T>
    2629        T& as() {
    27                 std::type_index from = get_class(), to = { typeid(T) };
     30                std::type_index from = get_class(), to = class_of<T>();
    2831                if ( from != to ) throw bad_cast{ from, to };
    2932                return reinterpret_cast<T&>(*this);
     
    3235        template<typename T>
    3336        const T& as() const {
    34                 std::type_index from = get_class(), to = { typeid(T) };
     37                std::type_index from = get_class(), to = class_of<T>();
    3538                if ( from != to ) throw bad_cast{ from, to };
    3639                return reinterpret_cast<const T&>(*this);
     
    4649};
    4750
    48 class integer : public object {
     51template<typename T>
     52T* as_subclass_of( object* o ) {
     53        T* r = dynamic_cast<T*>( o );
     54        if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() };
     55        return r;
     56}
     57
     58template<typename T>
     59const T* as_subclass_of( const object* o ) {
     60        const T* r = dynamic_cast<const T*>( o );
     61        if ( r == nullptr ) throw bad_cast{ o->get_class(), class_of<T>() };
     62        return r;
     63}
     64
     65class ordered : public object {
     66public:
     67        virtual int cmp(const ordered&) const = 0;
     68
     69        bool operator< (const ordered& that) const { return cmp(that) < 0; }
     70
     71        bool operator<= ( const ordered& that ) const { return cmp(that) <= 0; }
     72
     73        bool operator== ( const ordered& that ) const { return cmp(that) == 0; }
     74
     75        bool operator!= ( const ordered& that ) const { return cmp(that) != 0; }
     76
     77        bool operator> ( const ordered& that ) const { return cmp(that) > 0; }
     78
     79        bool operator>= ( const ordered& that ) const { return cmp(that) >= 0; }
     80};
     81
     82class boolean : public ordered {
     83private:
     84        bool x;
     85
     86public:
     87        boolean() = default;
     88
     89        boolean(bool x) : x(x) {}
     90
     91        std::unique_ptr<object> new_inst() const override { return std::make_unique<boolean>(); }
     92       
     93        std::unique_ptr<object> new_copy() const override { return std::make_unique<boolean>(*this); }
     94
     95        boolean& operator= (const boolean& that) {
     96                x = that.x;
     97                return *this;   
     98        }
     99
     100        object& operator= (const object& that) override {
     101                std::type_index from = that.get_class(), to = get_class();
     102                if ( from != to ) throw bad_cast{ from, to };
     103                return *this = static_cast<const boolean&>(that);
     104        }
     105
     106        ~boolean() override = default;
     107
     108        int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
     109
     110        // bool operator< (const boolean& that) const { return x < that.x; }
     111
     112        // bool operator== (const boolean& that) const { return x == that.x; }
     113
     114        int cmp(const ordered& that) const override {
     115                std::type_index from = that.get_class(), to = get_class();
     116                if ( from != to ) throw bad_cast{ from, to };
     117                return cmp( static_cast<const boolean&>(that) );
     118        }
     119};
     120
     121class character : public ordered {
     122private:
     123        char x;
     124
     125public:
     126        character() = default;
     127
     128        character(char x) : x(x) {}
     129
     130        std::unique_ptr<object> new_inst() const override { return std::make_unique<character>(); }
     131       
     132        std::unique_ptr<object> new_copy() const override { return std::make_unique<character>(*this); }
     133
     134        character& operator= (const character& that) {
     135                x = that.x;
     136                return *this;   
     137        }
     138
     139        object& operator= (const object& that) override {
     140                std::type_index from = that.get_class(), to = get_class();
     141                if ( from != to ) throw bad_cast{ from, to };
     142                return *this = static_cast<const character&>(that);
     143        }
     144
     145        ~character() override = default;
     146
     147        int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
     148
     149        // bool operator< (const character& that) const { return x < that.x; }
     150
     151        // bool operator== (const character& that) const { return x == that.x; }
     152
     153        int cmp(const ordered& that) const override {
     154                std::type_index from = that.get_class(), to = get_class();
     155                if ( from != to ) throw bad_cast{ from, to };
     156                return cmp( static_cast<const character&>(that) );
     157        }
     158};
     159
     160class integer : public ordered {
    49161private:
    50162        int x;
     
    67179                std::type_index from = that.get_class(), to = get_class();
    68180                if ( from != to ) throw bad_cast{ from, to };
    69                 return *this = reinterpret_cast<const integer&>(that);
     181                return *this = static_cast<const integer&>(that);
    70182        }
    71183
    72184        ~integer() override = default;
    73185
    74         integer& operator+= (const integer& that) {
    75                 x += that.x;
     186        int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
     187
     188        // bool operator< (const integer& that) const { return x < that.x; }
     189
     190        // bool operator== (const integer& that) const { return x == that.x; }
     191
     192        int cmp(const ordered& that) const override {
     193                std::type_index from = that.get_class(), to = get_class();
     194                if ( from != to ) throw bad_cast{ from, to };
     195                return cmp( static_cast<const integer&>(that) );
     196        }
     197};
     198
     199class pair : public ordered {
     200private:
     201        std::unique_ptr<object> x;
     202        std::unique_ptr<object> y;
     203
     204public:
     205        pair() = default;
     206
     207        pair(std::unique_ptr<object>&& x, std::unique_ptr<object>&& y)
     208                : x(std::move(x)), y(std::move(y)) {}
     209       
     210        std::unique_ptr<object> new_inst() const override { return std::make_unique<pair>(); }
     211
     212        std::unique_ptr<object> new_copy() const override {
     213                return std::make_unique<pair>(x->new_copy(), y->new_copy());
     214        }
     215
     216        pair& operator= (const pair& that) {
     217                x = that.x->new_copy();
     218                y = that.y->new_copy();
    76219                return *this;
    77220        }
    78 };
     221
     222        object& operator= (const object& that) override {
     223                std::type_index from = that.get_class(), to = get_class();
     224                if ( from != to ) throw bad_cast{ from, to };
     225                return *this = static_cast<const pair&>(that);
     226        }
     227
     228        ~pair() override = default;
     229
     230        int cmp(const pair& that) const {
     231                const ordered* a = as_subclass_of<ordered>( x.get() );
     232                const ordered* b = as_subclass_of<ordered>( that.x.get() );
     233                int c = a->cmp( *b );
     234                if ( c != 0 ) return c;
     235                a = as_subclass_of<ordered>( y.get() );
     236                b = as_subclass_of<ordered>( that.y.get() );
     237                return a->cmp( *b );
     238        }
     239
     240        // bool operator< (const pair& that) const { return cmp(that) < 0; }
     241
     242        // bool operator== ( const pair& that) const { return cmp(that) == 0; }
     243
     244        int cmp(const ordered& that) const override {
     245                std::type_index from = that.get_class(), to = get_class();
     246                if ( from != to ) throw bad_cast{ from, to };
     247                return cmp( static_cast<const pair&>(that) );
     248        }
     249};
Note: See TracChangeset for help on using the changeset viewer.