source: doc/generic_types/evaluation/object.hpp @ 33e22da

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 33e22da was a381b46, checked in by Aaron Moss <a3moss@…>, 7 years ago

Minor cleanup, also filled in benchmark source appendix

  • Property mode set to 100644
File size: 5.8 KB
Line 
1#pragma once
2#include <cstddef>
3#include <exception>
4#include <iomanip>
5#include <memory>
6#include <ostream>
7#include <string>
8#include <typeinfo>
9#include <typeindex>
10
11class bad_cast : public std::exception {
12        std::string why;
13public:
14        bad_cast( const std::type_index& f, const std::type_index& t ) : std::exception() {
15                why = std::string{"bad cast of "} + f.name() + " to " + t.name();
16        }
17        ~bad_cast() override = default;
18       
19        const char* what() const noexcept override { return why.c_str(); }
20};
21
22template<typename T> std::type_index class_of() { return { typeid(T) }; }
23
24template<typename T> using ptr = std::unique_ptr<T>;
25
26struct object {
27        std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; }
28
29        template<typename T> T& as() {
30                T* p = dynamic_cast<T*>(this);
31                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
32                return *p;
33        }
34
35        template<typename T> const T& as() const {
36                const T* p = dynamic_cast<const T*>(this);
37                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
38                return *p;
39        }
40
41        virtual ptr<object> new_inst() const = 0;
42        virtual ptr<object> new_copy() const = 0;
43        virtual object& operator= (const object&) = 0;
44        virtual ~object() = default;
45};
46
47template<typename T, typename... Args> static inline ptr<T> make(Args&&... args) {
48        return std::make_unique<T>(std::forward<Args>(args)...);
49}
50
51template<typename To, typename From> 
52ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; }
53
54struct ordered : public virtual object {
55        virtual int cmp(const ordered&) const = 0;
56
57        bool operator< (const ordered& that) const { return cmp(that) < 0; }
58        bool operator<= ( const ordered& that ) const { return cmp(that) <= 0; }
59        bool operator== ( const ordered& that ) const { return cmp(that) == 0; }
60        bool operator!= ( const ordered& that ) const { return cmp(that) != 0; }
61        bool operator> ( const ordered& that ) const { return cmp(that) > 0; }
62        bool operator>= ( const ordered& that ) const { return cmp(that) >= 0; }
63};
64
65struct printable : public virtual object {
66        virtual void print(std::ostream&) const = 0;
67};
68
69class boolean : public ordered, public printable {
70        bool x;
71public:
72        boolean() = default;
73        boolean(bool x) : x(x) {}
74        ptr<object> new_inst() const override { return make<boolean>(); }
75        ptr<object> new_copy() const override { return make<boolean>(*this); }
76        boolean& operator= (const boolean& that) {
77                x = that.x;
78                return *this;   
79        }
80        object& operator= (const object& that) override { return *this = that.as<boolean>(); } /***/
81        ~boolean() override = default;
82
83        int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
84        int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); } /***/
85
86        void print(std::ostream& out) const override { out << (x ? "true" : "false"); }
87};
88
89class character : public ordered, public printable {
90        char x;
91public:
92        character() = default;
93        character(char x) : x(x) {}
94        ptr<object> new_inst() const override { return make<character>(); }
95        ptr<object> new_copy() const override { return make<character>(*this); }
96        character& operator= (const character& that) {
97                x = that.x;
98                return *this;   
99        }
100        object& operator= (const object& that) override { return *this = that.as<character>(); } /***/
101        ~character() override = default;
102
103        int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
104        int cmp(const ordered& that) const override { return cmp( that.as<character>() ); } /***/
105
106        void print(std::ostream& out) const override {
107                if ( 0x20 <= x && x <= 0x7E ) { out << "'" << x << "'"; }
108                else { out << "'\\" << std::hex << (unsigned int)x << std::setbase(0) << "'"; }
109        }
110};
111
112class integer : public ordered, public printable {
113        int x;
114public:
115        integer() = default;
116        integer(int x) : x(x) {}
117        ptr<object> new_inst() const override { return make<integer>(); }
118        ptr<object> new_copy() const override { return make<integer>(*this); }
119        integer& operator= (const integer& that) {
120                x = that.x;
121                return *this;   
122        }
123        object& operator= (const object& that) override { return *this = that.as<integer>(); } /***/
124        ~integer() override = default;
125
126        int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
127        int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); } /***/
128
129        void print(std::ostream& out) const override { out << x; }
130};
131
132class c_string : public printable {
133        static constexpr const char* empty = "";
134        const char* s;
135public:
136        c_string() : s(empty) {}
137        c_string(const char* s) : s(s) {}
138        ptr<object> new_inst() const override { return make<c_string>(); }
139        ptr<object> new_copy() const override { return make<c_string>(s); }
140        c_string& operator= (const c_string& that) {
141                s = that.s;
142                return *this;
143        }
144        object& operator= (const object& that) override { return *this = that.as<c_string>(); } /***/
145        ~c_string() override = default;
146
147        void print(std::ostream& out) const override { out << s; }
148};
149
150class pair : public ordered, public printable {
151        ptr<object> x;
152        ptr<object> y;
153public:
154        pair() = default;
155        pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {}
156        ptr<object> new_inst() const override { return make<pair>(); }
157        ptr<object> new_copy() const override { return make<pair>(x->new_copy(), y->new_copy()); }
158        pair& operator= (const pair& that) {
159                x = that.x->new_copy();
160                y = that.y->new_copy();
161                return *this;
162        }
163        object& operator= (const object& that) override { return *this = that.as<pair>(); } /***/
164        ~pair() override = default;
165
166        int cmp(const pair& that) const {
167                int c = x->as<ordered>().cmp( that.x->as<ordered>() ); /***/
168                if ( c != 0 ) return c;
169                return y->as<ordered>().cmp( that.y->as<ordered>() ); /***/
170        }
171        int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); }
172
173        void print(std::ostream& out) const override {
174                out << "[";
175                x->as<printable>().print(out); /***/
176                out << ", ";
177                y->as<printable>().print(out); /***/
178                out << "]";
179        }
180};
Note: See TracBrowser for help on using the repository browser.