996
|
1 // Var.h
|
|
2
|
|
3 // This is the base class for all variables in rail. The
|
|
4 // acquire/release is for reference counting, which we will use for
|
|
5 // our GC implementation. We can use reference counting here because
|
|
6 // the only primitives are cons (':'), lambda ('&'), and stack
|
|
7 // operations. No modifications of cons cells or closures are allowed.
|
|
8
|
|
9 // The simple tag-based polymorphism here is used because all of the
|
|
10 // actions work only on specific well-defined types. Thus we want
|
|
11 // to bypass the complex multiple-dispatch pattern.
|
|
12
|
|
13 #ifndef VAR_H_RAIL_1
|
|
14 #define VAR_H_RAIL_1
|
|
15
|
|
16 class StringVar;
|
|
17 class ListVar;
|
|
18 class NilVar;
|
|
19 class Closure;
|
|
20
|
|
21 class Var
|
|
22 {
|
|
23 public:
|
|
24 Var() : refCount(0) {}
|
|
25 virtual ~Var() {}
|
|
26
|
|
27 void acquire(void)
|
|
28 {
|
|
29 ++refCount;
|
|
30 }
|
|
31
|
|
32 void release(void)
|
|
33 {
|
|
34 --refCount;
|
|
35 if (refCount <= 0)
|
|
36 {
|
|
37 delete this;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 virtual StringVar * getString(void)=0;
|
|
42 virtual ListVar * getList(void)=0;
|
|
43 virtual NilVar * getNil(void)=0;
|
|
44 virtual Closure * getClosure(void)=0;
|
|
45
|
|
46 virtual std::string toString(void) const=0;
|
|
47 private:
|
|
48 Var(Var const &) {}
|
|
49 Var & operator=(Var const &) { return *this; }
|
|
50 private:
|
|
51 int refCount;
|
|
52 };
|
|
53
|
|
54 #endif
|