]> gitweb.factorcode.org Git - factor.git/blob - vm/tagged.hpp
Merge branch 'redis' of git://www.tiodante.com/git/factor
[factor.git] / vm / tagged.hpp
1 namespace factor
2 {
3
4 template <typename T> cell tag(T *value)
5 {
6         return RETAG(value,tag_for(T::type_number));
7 }
8
9 inline static cell tag_dynamic(object *value)
10 {
11         return RETAG(value,tag_for(value->h.hi_tag()));
12 }
13
14 template <typename T>
15 struct tagged
16 {
17         cell value_;
18
19         cell value() const { return value_; }
20         T *untagged() const { return (T *)(UNTAG(value_)); }
21
22         cell type() const {
23                 cell tag = TAG(value_);
24                 if(tag == OBJECT_TYPE)
25                         return untagged()->h.hi_tag();
26                 else
27                         return tag;
28         }
29
30         bool type_p(cell type_) const { return type() == type_; }
31
32         T *untag_check() const {
33                 if(T::type_number != TYPE_COUNT && !type_p(T::type_number))
34                         type_error(T::type_number,value_);
35                 return untagged();
36         }
37
38         explicit tagged(cell tagged) : value_(tagged) {
39 #ifdef FACTOR_DEBUG
40                 untag_check();
41 #endif
42         }
43
44         explicit tagged(T *untagged) : value_(factor::tag(untagged)) {
45 #ifdef FACTOR_DEBUG
46                 untag_check();
47 #endif
48         }
49
50         T *operator->() const { return untagged(); }
51         cell *operator&() const { return &value_; }
52
53         const tagged<T>& operator=(const T *x) { value_ = tag(x); return *this; }
54         const tagged<T>& operator=(const cell &x) { value_ = x; return *this; }
55
56         bool operator==(const tagged<T> &x) { return value_ == x.value_; }
57         bool operator!=(const tagged<T> &x) { return value_ != x.value_; }
58
59         template<typename X> tagged<X> as() { return tagged<X>(value_); }
60 };
61
62 template <typename T> T *untag_check(cell value)
63 {
64         return tagged<T>(value).untag_check();
65 }
66
67 template <typename T> T *untag(cell value)
68 {
69         return tagged<T>(value).untagged();
70 }
71
72 }