1 | //----------------------------------------------------------------------------- |
---|
2 | /// @file rf_term.ih |
---|
3 | /// |
---|
4 | /// Refal+ term inline method implementation |
---|
5 | // |
---|
6 | // $Source$ |
---|
7 | // $Revision: 723 $ |
---|
8 | // $Date: 2003-05-05 12:04:11 +0000 (Mon, 05 May 2003) $ |
---|
9 | //----------------------------------------------------------------------------- |
---|
10 | |
---|
11 | #ifndef __rf_term_ih__ |
---|
12 | #define __rf_term_ih__ |
---|
13 | |
---|
14 | #include "rf_term.hh" |
---|
15 | #include "rf_types.ih" |
---|
16 | #include "rf_object.ih" |
---|
17 | #include "rf_expr.hh" |
---|
18 | |
---|
19 | namespace rfrt |
---|
20 | { |
---|
21 | |
---|
22 | using namespace rftype ; |
---|
23 | |
---|
24 | inline Term::Term (term_class_t _class, unsigned _type) |
---|
25 | { |
---|
26 | set_class(_class); |
---|
27 | set_type(_type); |
---|
28 | } |
---|
29 | |
---|
30 | inline Term::Term (uint16_t _c) : |
---|
31 | uint_data2 (_c) |
---|
32 | { |
---|
33 | set_class(term_sym); |
---|
34 | set_type(type_char); |
---|
35 | } |
---|
36 | |
---|
37 | inline Term::Term (Object* _obj) |
---|
38 | { |
---|
39 | set_class(term_obj); |
---|
40 | set_type(_obj->get_type()); |
---|
41 | new(&uint_data2) Ref(_obj); |
---|
42 | } |
---|
43 | |
---|
44 | inline Term::~Term () |
---|
45 | { |
---|
46 | #if 0 |
---|
47 | if (is_ref()) { |
---|
48 | // Expr e(this); |
---|
49 | // e.get_mem_chunk()->dec_ref_count(); |
---|
50 | MemoryChunk* m = get_mem_chunk(); |
---|
51 | // |
---|
52 | // Decrement reference counter and destroy an object if it is zero |
---|
53 | if (m->dec_ref_count() == 0) { |
---|
54 | // |
---|
55 | // Walk through and decrement reference counters on childs |
---|
56 | // bool f = (data1 & REF_BIT) == 0 ? true : false; |
---|
57 | Expr::deref_childs( |
---|
58 | get_first(), (data1 & FLAT_BIT) != 0 ? get_last() : get_first(), m |
---|
59 | ); |
---|
60 | // |
---|
61 | // Deallocate expression holder in memory |
---|
62 | MemoryChunk::destroy_instance(m); |
---|
63 | } |
---|
64 | } |
---|
65 | #else |
---|
66 | (this->*dtor_funcs[get_class()])(); |
---|
67 | #endif |
---|
68 | } |
---|
69 | |
---|
70 | inline Term::Term (Term const& _t) |
---|
71 | { |
---|
72 | (this->*ctor_funcs[get_class()])(_t); |
---|
73 | } |
---|
74 | |
---|
75 | inline Term& Term::operator = (Term const& _t) |
---|
76 | { |
---|
77 | if (_t != self) { |
---|
78 | this->~Term(); |
---|
79 | new(this) Term(_t); |
---|
80 | } |
---|
81 | return self; |
---|
82 | } |
---|
83 | |
---|
84 | inline Expr Term::create_expr (size_t _len, int _align /* = 0 */) |
---|
85 | { |
---|
86 | return Expr(_len, _align); |
---|
87 | } |
---|
88 | |
---|
89 | inline bool Term::is_ref () const |
---|
90 | { |
---|
91 | return get_class() == term_ref; |
---|
92 | } |
---|
93 | |
---|
94 | inline bool Term::is_sym () const |
---|
95 | { |
---|
96 | return get_class() == term_sym; |
---|
97 | } |
---|
98 | |
---|
99 | inline bool Term::is_border () const |
---|
100 | { |
---|
101 | return data1 == SYM_BORDER; |
---|
102 | } |
---|
103 | |
---|
104 | inline Term* Term::get_first () const |
---|
105 | { |
---|
106 | return (Term*)(data1 & PTR_MASK); |
---|
107 | } |
---|
108 | |
---|
109 | inline Term* Term::get_last () const |
---|
110 | { |
---|
111 | // return (Term*)(data2 & PTR_MASK); |
---|
112 | return (Term*)uint_data2; |
---|
113 | } |
---|
114 | |
---|
115 | inline unsigned Term::get_type () const |
---|
116 | { |
---|
117 | return data1 >> TYPE_SHIFT; |
---|
118 | } |
---|
119 | |
---|
120 | inline void Term::set_type (unsigned _type) |
---|
121 | { |
---|
122 | data1 = (_type << TYPE_SHIFT) | get_class(); |
---|
123 | } |
---|
124 | |
---|
125 | inline term_class_t Term::get_class () const |
---|
126 | { |
---|
127 | return (term_class_t)(data1 & CLASS_MASK); |
---|
128 | } |
---|
129 | |
---|
130 | inline void Term::set_class (term_class_t _class) |
---|
131 | { |
---|
132 | data1 &= ~(CLASS_MASK); |
---|
133 | data1 |= _class; |
---|
134 | } |
---|
135 | |
---|
136 | inline MemoryChunk* Term::get_mem_chunk() const |
---|
137 | { |
---|
138 | return static_cast<MemoryChunk*>(allocator.get_block(get_first())); |
---|
139 | } |
---|
140 | |
---|
141 | #if defined(RF_STORE_CHUNK_ORDER_IN_TERM) |
---|
142 | inline unsigned get_order () const |
---|
143 | { |
---|
144 | return Term::ORDER(data1 & ORDER1_MASK, data2 & ORDER2_MASK); |
---|
145 | } |
---|
146 | #endif |
---|
147 | |
---|
148 | inline bool Term::operator == (Term const& _t) const |
---|
149 | { |
---|
150 | term_class_t cl = get_class(); |
---|
151 | // |
---|
152 | // If both terms have the same type... |
---|
153 | if (cl == _t.get_class()){ |
---|
154 | // |
---|
155 | // ...compare them |
---|
156 | return (this->*eq_funcs[cl])(_t); |
---|
157 | } else { |
---|
158 | return false; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | inline bool Term::operator != (Term const& _t) const |
---|
163 | { |
---|
164 | return !(*this == _t); |
---|
165 | } |
---|
166 | |
---|
167 | inline bool Term::eq ( |
---|
168 | Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2 |
---|
169 | ) |
---|
170 | { |
---|
171 | // |
---|
172 | // First compare term sequences lengths |
---|
173 | if ((_l1 - _f1) == (_l2 - _f2)) { |
---|
174 | // |
---|
175 | // If lengths are equal do per-term comparison |
---|
176 | for (; _f1 < _l1; _f1++, _f2++) { |
---|
177 | if (*_f1 != *_f2) return false; |
---|
178 | } |
---|
179 | return true; |
---|
180 | } |
---|
181 | return false; |
---|
182 | } |
---|
183 | |
---|
184 | inline Term::Term (Expr const& _expr) : |
---|
185 | data1 ((uintptr_t)_expr.get_first()), |
---|
186 | uint_data2 ((uintptr_t)_expr.get_last()) |
---|
187 | { |
---|
188 | set_class(term_ref); |
---|
189 | D(printf("+ term %p from expression %p(%p,%p,%p)\n", |
---|
190 | this, &_expr, _expr.first, _expr.last, _expr.mem_chunk);) |
---|
191 | data1 |= (_expr.get_flags() & FLAT_BIT); |
---|
192 | _expr.get_mem_chunk()->inc_ref_count(); |
---|
193 | } |
---|
194 | |
---|
195 | inline Object& Term::get_object () |
---|
196 | { |
---|
197 | if (get_class() == term_obj) { |
---|
198 | Ref* r1 = (Ref*)(&uint_data2); |
---|
199 | return *(*r1); |
---|
200 | } else { |
---|
201 | FATAL("Term does not contain an object"); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | inline Term::operator pxx::WString () const |
---|
207 | { |
---|
208 | return (this->*to_string_funcs[get_class()])(); |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | #endif // __rf_term_ih__ |
---|