Ignore:
Timestamp:
Apr 15, 2017, 7:09:59 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
c57d1935
Parents:
308880c
Message:

Minor cleanup, also filled in benchmark source appendix

File:
1 edited

Legend:

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

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <cstddef>
    43#include <exception>
     
    1615                why = std::string{"bad cast of "} + f.name() + " to " + t.name();
    1716        }
    18 
    1917        ~bad_cast() override = default;
    2018       
     
    2220};
    2321
    24 template<typename T>
    25 std::type_index class_of() { return { typeid(T) }; }
     22template<typename T> std::type_index class_of() { return { typeid(T) }; }
    2623
    2724template<typename T> using ptr = std::unique_ptr<T>;
    2825
    29 class object {
    30 public:
     26struct object {
    3127        std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; }
    3228
    33         template<typename T>
    34         T& as() {
     29        template<typename T> T& as() {
    3530                T* p = dynamic_cast<T*>(this);
    3631                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     
    3833        }
    3934
    40         template<typename T>
    41         const T& as() const {
     35        template<typename T> const T& as() const {
    4236                const T* p = dynamic_cast<const T*>(this);
    4337                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     
    4640
    4741        virtual ptr<object> new_inst() const = 0;
    48        
    4942        virtual ptr<object> new_copy() const = 0;
    50        
    5143        virtual object& operator= (const object&) = 0;
    52        
    5344        virtual ~object() = default;
    5445};
    5546
    56 template<typename T, typename... Args>
    57 static inline ptr<T> make(Args&&... args) { return std::make_unique<T>(std::forward<Args>(args)...); }
     47template<typename T, typename... Args> static inline ptr<T> make(Args&&... args) {
     48        return std::make_unique<T>(std::forward<Args>(args)...);
     49}
    5850
    59 template<typename To, typename From>
     51template<typename To, typename From> 
    6052ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; }
    6153
    62 class ordered : public virtual object {
    63 public:
     54struct ordered : public virtual object {
    6455        virtual int cmp(const ordered&) const = 0;
    6556
    6657        bool operator< (const ordered& that) const { return cmp(that) < 0; }
    67 
    6858        bool operator<= ( const ordered& that ) const { return cmp(that) <= 0; }
    69 
    7059        bool operator== ( const ordered& that ) const { return cmp(that) == 0; }
    71 
    7260        bool operator!= ( const ordered& that ) const { return cmp(that) != 0; }
    73 
    7461        bool operator> ( const ordered& that ) const { return cmp(that) > 0; }
    75 
    7662        bool operator>= ( const ordered& that ) const { return cmp(that) >= 0; }
    7763};
    7864
    79 class printable : public virtual object {
    80 public:
     65struct printable : public virtual object {
    8166        virtual void print(std::ostream&) const = 0;
    8267};
     
    8469class boolean : public ordered, public printable {
    8570        bool x;
    86 
    8771public:
    8872        boolean() = default;
    89 
    9073        boolean(bool x) : x(x) {}
    91 
    9274        ptr<object> new_inst() const override { return make<boolean>(); }
    93        
    9475        ptr<object> new_copy() const override { return make<boolean>(*this); }
    95 
    9676        boolean& operator= (const boolean& that) {
    9777                x = that.x;
    9878                return *this;   
    9979        }
    100 
    10180        object& operator= (const object& that) override { return *this = that.as<boolean>(); } /***/
    102 
    10381        ~boolean() override = default;
    10482
    10583        int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
    106 
    10784        int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); } /***/
    10885
     
    11289class character : public ordered, public printable {
    11390        char x;
    114 
    11591public:
    11692        character() = default;
    117 
    11893        character(char x) : x(x) {}
    119 
    12094        ptr<object> new_inst() const override { return make<character>(); }
    121        
    12295        ptr<object> new_copy() const override { return make<character>(*this); }
    123 
    12496        character& operator= (const character& that) {
    12597                x = that.x;
    12698                return *this;   
    12799        }
    128 
    129100        object& operator= (const object& that) override { return *this = that.as<character>(); } /***/
    130 
    131101        ~character() override = default;
    132102
    133103        int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    134 
    135104        int cmp(const ordered& that) const override { return cmp( that.as<character>() ); } /***/
    136105
     
    143112class integer : public ordered, public printable {
    144113        int x;
    145 
    146114public:
    147115        integer() = default;
    148 
    149116        integer(int x) : x(x) {}
    150 
    151117        ptr<object> new_inst() const override { return make<integer>(); }
    152        
    153118        ptr<object> new_copy() const override { return make<integer>(*this); }
    154 
    155119        integer& operator= (const integer& that) {
    156120                x = that.x;
    157121                return *this;   
    158122        }
    159 
    160123        object& operator= (const object& that) override { return *this = that.as<integer>(); } /***/
    161 
    162124        ~integer() override = default;
    163125
    164126        int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    165 
    166127        int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); } /***/
    167128
     
    174135public:
    175136        c_string() : s(empty) {}
    176 
    177137        c_string(const char* s) : s(s) {}
    178 
    179138        ptr<object> new_inst() const override { return make<c_string>(); }
    180 
    181139        ptr<object> new_copy() const override { return make<c_string>(s); }
    182 
    183140        c_string& operator= (const c_string& that) {
    184141                s = that.s;
    185142                return *this;
    186143        }
    187 
    188144        object& operator= (const object& that) override { return *this = that.as<c_string>(); } /***/
    189 
    190145        ~c_string() override = default;
    191146
     
    196151        ptr<object> x;
    197152        ptr<object> y;
    198 
    199153public:
    200154        pair() = default;
    201 
    202155        pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {}
    203        
    204156        ptr<object> new_inst() const override { return make<pair>(); }
    205 
    206         ptr<object> new_copy() const override {
    207                 return make<pair>(x->new_copy(), y->new_copy());
    208         }
    209 
     157        ptr<object> new_copy() const override { return make<pair>(x->new_copy(), y->new_copy()); }
    210158        pair& operator= (const pair& that) {
    211159                x = that.x->new_copy();
     
    213161                return *this;
    214162        }
    215 
    216163        object& operator= (const object& that) override { return *this = that.as<pair>(); } /***/
    217 
    218164        ~pair() override = default;
    219165
     
    223169                return y->as<ordered>().cmp( that.y->as<ordered>() ); /***/
    224170        }
    225 
    226171        int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); }
    227172
Note: See TracChangeset for help on using the changeset viewer.