File: Variant.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (197 lines) | stat: -rw-r--r-- 4,118 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "Variant.h"
#include "Runtime.h"
#include "Handle.h"
#include "StrBuf.h"

namespace storm {

	Variant::Variant() : data(null) {}

	Variant::Variant(const Variant &o) : data(null) {
		if (!o.data)
			return;

		const GcType *t = runtime::gcTypeOf(o.data);
		if (t->kind == GcType::tArray) {
			// Value.
			GcArray<Byte> *alloc = (GcArray<Byte> *)o.data;
			init(alloc->v, t->type);
		} else {
			// Class.
			data = o.data;
		}
	}

	Variant &Variant::operator =(const Variant &o) {
		Variant t(o);
		std::swap(t.data, data);
		return *this;
	}

	Variant::Variant(RootObject *o) : data(o) {}

	Variant::Variant(Object *o) : data(o) {}

	Variant::Variant(TObject *o) : data(o) {}

	Variant::Variant(const void *value, Type *type) {
		init(value, type);
	}

	void Variant::init(const void *value, Type *type) {
		const Handle &h = runtime::typeHandle(type);
		data = runtime::allocArray(h.engine(), h.gcArrayType, 1);

		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		h.safeCopy(alloc->v, value);
		alloc->filled = 1;
	}

	Variant::~Variant() {
		if (!data)
			return;

		const GcType *t = runtime::gcTypeOf(data);
		if (t->kind != GcType::tArray)
			return;

		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		if (!alloc->filled)
			return; // Not initialized.

		const Handle &h = runtime::typeHandle(t->type);
		h.safeDestroy(alloc->v);
		alloc->filled = 0;
	}

	void Variant::deepCopy(CloneEnv *env) {
		if (!data)
			return;

		const GcType *t = runtime::gcTypeOf(data);
		if (t->kind == GcType::tArray) {
			// Value.
			const Handle &h = runtime::typeHandle(t->type);
			if (!h.deepCopyFn)
				return;

			GcArray<Byte> *alloc = (GcArray<Byte> *)data;
			(*h.deepCopyFn)(alloc->v, env);
		} else {
			// Class.
			data = runtime::cloneObjectEnv((RootObject *)data, env);
		}
	}

	Bool Variant::empty() const {
		if (!data)
			return true;

		const GcType *t = runtime::gcTypeOf(data);
		if (t->kind == GcType::tArray) {
			// We have data, but is it initialized?
			GcArray<Byte> *alloc = (GcArray<Byte> *)data;
			return alloc->filled == 0;
		} else {
			return false;
		}
	}

	Bool Variant::has(Type *type) const {
		if (!data)
			return false;

		const GcType *t = runtime::gcTypeOf(data);
		return runtime::isA(type, t->type);
	}

	MAYBE(Type *) Variant::type() const {
		if (!data)
			return null;

		const GcType *t = runtime::gcTypeOf(data);
		return t->type;
	}

	void *Variant::getValue() const {
		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		return alloc->v;
	}

	Variant Variant::uninitializedValue(Type *type) {
		assert(runtime::isValue(type));

		Variant v;

		const Handle &h = runtime::typeHandle(type);
		v.data = runtime::allocArray(h.engine(), h.gcArrayType, 1);

		return v;
	}

	void Variant::valueInitialized() {
		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		alloc->filled = 1;
	}

	void Variant::valueRemoved() {
		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		alloc->filled = 0;
	}

	void Variant::moveValue(void *to) {
		const GcType *t = runtime::gcTypeOf(data);
		GcArray<Byte> *alloc = (GcArray<Byte> *)data;
		memcpy(to, alloc->v, t->stride);
		memset(alloc->v, 0, t->stride);
		valueRemoved();
	}

	RootObject *Variant::getObject() const {
		return (RootObject *)data;
	}

	void *Variant::getPointer() {
		if (!data)
			return null;

		const GcType *t = runtime::gcTypeOf(data);
		if (t->kind == GcType::tArray) {
			return getValue();
		} else {
			return &data;
		}
	}

	Engine &Variant::engine() const {
		const GcType *t = runtime::gcTypeOf(data);
		return runtime::allocEngine((RootObject *)t->type);
	}

	void Variant::toS(StrBuf *to) const {
		if (empty()) {
			*to << S("<empty>");
			return;
		}

		const GcType *t = runtime::gcTypeOf(data);
		const Handle &h = runtime::typeHandle(t->type);
		if (t->kind == GcType::tArray) {
			GcArray<Byte> *alloc = (GcArray<Byte> *)data;
			(*h.toSFn)(alloc->v, to);
		} else {
			(*h.toSFn)(&data, to);
		}
	}

	wostream &operator <<(wostream &to, const Variant &v) {
		if (v.empty())
			return to << L"<empty>";

		StrBuf *b = new (v.engine()) StrBuf();
		*b << v;
		return to << ::toS(b);
	}

}