summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/item.h
blob: 2e3fb090eb75fdc680da41ac725196717f818f8b (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*****
 * inst.h
 * Tom Prince and John Bowman 2005/04/12
 *
 * Descibes the items that are used by the virtual machine.
 *****/

#ifndef ITEM_H
#define ITEM_H

#include "common.h"
#include <cfloat>
#include <cmath>
#include <typeinfo>
#include <cassert>

namespace vm {

class item;
class bad_item_value {};

template<typename T>
T get(const item&);

#if COMPACT
// Identify a default argument.
extern const Int DefaultValue;
  
// Identify an undefined item.
extern const Int Undefined;
#endif
  
extern const item Default;

class item : public gc {
private:
  
#if !COMPACT  
  const std::type_info *kind;
#endif
  
  union {
    Int i;
    double x;
    bool b;
    void *p;
  };

public:
#if COMPACT    
  bool empty() const
  {return i >= Undefined;}
  
  item() : i(Undefined) {}
  
  item(Int i)
    : i(i) {}
  item(int i)
    : i(i) {}
  item(double x)
    : x(x) {}
  item(bool b)
    : b(b) {}
  
  item& operator= (int a)
  { i=a; return *this; }
  item& operator= (Int a)
  { i=a; return *this; }
  item& operator= (double a)
  { x=a; return *this; }
  item& operator= (bool a)
  { b=a; return *this; }
  
  template<class T>
  item(T *p)
    : p((void *) p) {
    assert(p < (void *) Undefined);
  }
  
  template<class T>
  item(const T &P)
    : p(new(UseGC) T(P)) {
    assert(p < (void *) Undefined);
  }
  
  template<class T>
  item& operator= (T *a)
  { p=(void *) a; return *this; }
  
  template<class T>
  item& operator= (const T &it)
  { p=new(UseGC) T(it); return *this; }
#else    
  bool empty() const
  {return *kind == typeid(void);}
  
  item()
    : kind(&typeid(void)) {}
  
  item(Int i)
    : kind(&typeid(Int)), i(i) {}
  item(int i)
    : kind(&typeid(Int)), i(i) {}
  item(double x)
    : kind(&typeid(double)), x(x) {}
  item(bool b)
    : kind(&typeid(bool)), b(b) {}
  
  item& operator= (int a)
  { kind=&typeid(Int); i=a; return *this; }
  item& operator= (Int a)
  { kind=&typeid(Int); i=a; return *this; }
  item& operator= (double a)
  { kind=&typeid(double); x=a; return *this; }
  item& operator= (bool a)
  { kind=&typeid(bool); b=a; return *this; }
  
  template<class T>
  item(T *p)
    : kind(&typeid(T)), p((void *) p) {}
  
  template<class T>
  item(const T &p)
    : kind(&typeid(T)), p(new(UseGC) T(p)) {}
  
  template<class T>
  item& operator= (T *a)
  { kind=&typeid(T); p=(void *) a; return *this; }
  
  template<class T>
  item& operator= (const T &it)
  { kind=&typeid(T); p=new(UseGC) T(it); return *this; }
  
  const std::type_info &type() const
  { return *kind; }
#endif  
  
  template<typename T>
  friend inline T get(const item&);

  friend inline bool isdefault(const item&);
  
  friend ostream& operator<< (ostream& out, const item& i);

private:
  template <typename T>
  struct help;
  
  template <typename T>
  struct help<T*> {
    static T* unwrap(const item& it)
    {
#if COMPACT      
      if(!it.empty())
        return (T*) it.p;
#else        
      if(*it.kind == typeid(T))
        return (T*) it.p;
#endif        
      throw vm::bad_item_value();
    }
  };
  
  template <typename T>
  struct help {
    static T& unwrap(const item& it)
    {
#if COMPACT      
      if(!it.empty())
        return *(T*) it.p;
#else      
      if(*it.kind == typeid(T))
        return *(T*) it.p;
#endif      
      throw vm::bad_item_value();
    }
  };
};
  
class frame : public gc {
#ifdef DEBUG_FRAME
  string name;
#endif
  typedef mem::vector<item> vars_t;
  vars_t vars;
public:
#ifdef DEBUG_FRAME
  frame(string name, size_t size)
    : name(name), vars(size)
  {}

  string getName() { return name; }
#else
  frame(size_t size)
    : vars(size)
  {}
#endif

  item& operator[] (size_t n)
  { return vars[n]; }
  item operator[] (size_t n) const
  { return vars[n]; }

  size_t size()
  { return vars.size(); }
  
  // Extends vars to ensure it has a place for any variable indexed up to n.
  void extend(size_t n) {
    if(vars.size() < n)
      vars.resize(n);
  }
};

template<typename T>
inline T get(const item& it)
{
  return item::help<T>::unwrap(it);
} 

template <>
inline int get<int>(const item&)
{
  throw vm::bad_item_value();
}
  
template <>
inline Int get<Int>(const item& it)
{
#if COMPACT  
  if(!it.empty())
    return it.i;
#else
  if(*it.kind == typeid(Int))
    return it.i;
#endif  
  throw vm::bad_item_value();
}
  
template <>
inline double get<double>(const item& it)
{
#if COMPACT  
  if(!it.empty())
    return it.x;
#else
  if(*it.kind == typeid(double))
    return it.x;
#endif
  throw vm::bad_item_value();
}

template <>
inline bool get<bool>(const item& it)
{
#if COMPACT  
  if(!it.empty())
    return it.b;
#else  
  if(*it.kind == typeid(bool))
    return it.b;
#endif  
  throw vm::bad_item_value();
}

#if !COMPACT
// This serves as the object for representing a default argument.
struct default_t : public gc {};
#endif

inline bool isdefault(const item& it)
{
#if COMPACT  
  return it.i == DefaultValue;
#else  
  return *it.kind == typeid(default_t);
#endif  
} 

ostream& operator<< (ostream& out, const item& i);

} // namespace vm

#endif // ITEM_H