-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobject.hh
More file actions
61 lines (49 loc) · 1.6 KB
/
object.hh
File metadata and controls
61 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef _XT_OBJECT_HH_
#define _XT_OBJECT_HH_
#include "conf.hh"
#include <memory>
#include <vector>
namespace xt {
static TypeInfo bool_type { "Boolean" };
static TypeInfo char_type { "Char" };
static TypeInfo number_type { "Number"};
static TypeInfo nil_type { "Nil" };
struct Object {
Object() : type(Type::Nil), typeinfo(&nil_type) {}
Object(const Object& o);
explicit Object(bool b) : type(Type::Boolean), typeinfo(&bool_type), b(b) {}
explicit Object(int n) : type(Type::Number), typeinfo(&number_type), number(n) {}
explicit Object(double n) : type(Type::Number), typeinfo(&number_type), number(n) {}
explicit Object(char ch) : type(Type::Char), typeinfo(&char_type), ch(ch) {}
Object(void* t, TypeInfo* ti) : type(Type::Pointer), typeinfo(ti), raw(t) {}
template<class T>
Object(std::shared_ptr<T> t, TypeInfo* ti) : type(Type::Pointer), typeinfo(ti), ptr(t) {}
template<class T>
Object(std::shared_ptr<T> t) : type(Type::Pointer), typeinfo(&T::type_info), ptr(t) {}
Object& operator=(const Object& o);
bool operator==(const Object& o2) const { return type == o2.type && typeinfo == o2.typeinfo && number == o2.number; }
~Object() {}
operator bool() const;
Type type;
TypeInfo* typeinfo;
union {
double number;
bool b;
char ch;
void* raw;
std::shared_ptr<void> ptr;
};
};
typedef std::vector<Object> _ObjectArray;
typedef std::shared_ptr<_ObjectArray> ObjectArray;
ObjectArray new_object_array(unsigned int size = 0);
}
namespace std {
template<>
struct hash<typename xt::Object> {
std::size_t operator()(xt::Object const &key) {
return hash<void*>()(key.ptr.get());
}
};
}
#endif