-
Notifications
You must be signed in to change notification settings - Fork 1
/
winxedxx_classes.cxx
325 lines (261 loc) · 6.45 KB
/
winxedxx_classes.cxx
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// winxedxx_classes.cxx
// (C) 2011-2012 Julián Albo
#include "winxedxx_types.h"
#include "winxedxx_object.h"
#include "winxedxx_default.h"
#include "winxedxx_integer.h"
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include <dlfcn.h>
namespace WinxedXX
{
//*************************************************************
WxxRefcounted::WxxRefcounted() : refcount(0)
{
}
WxxRefcounted::WxxRefcounted(size_t initial) : refcount(initial)
{
}
WxxRefcounted::~WxxRefcounted()
{
}
void WxxRefcounted::incref()
{
++refcount;
}
void WxxRefcounted::decref()
{
if (refcount <= 0)
throw std::logic_error("refcounting error");
if (--refcount == 0)
delete this;
}
//*************************************************************
WxxStringIterator::WxxStringIterator(const std::string &container) :
WxxDefault("StringIterator"),
cnt(container),
current(0)
{
}
WxxStringIterator::~WxxStringIterator()
{
}
int WxxStringIterator::get_bool()
{
return current < int(cnt.size());
}
WxxObjectPtr WxxStringIterator::shift_pmc()
{
return new WxxString(std::string(1, cnt.at(current++)));
}
//*************************************************************
WxxHash::WxxHash() : WxxDefault("Hash")
{
}
WxxHash &WxxHash::set(const std::string &key, WxxObjectPtr value)
{
hsh[key] = value;
return *this;
}
int WxxHash::elements() const
{
return hsh.size();
}
WxxObjectPtr WxxHash::get_pmc_keyed(const std::string &s)
{
return hsh[s];
}
void WxxHash::set_pmc_keyed(const std::string &s, const WxxObjectPtr &value)
{
hsh[s] = value;
}
int WxxHash::exists(const std::string &key)
{
return hsh.find(key) != hsh.end();
}
//*************************************************************
WxxSub::WxxSub(WxxObjectPtr(*fnarg)(const WxxObjectArray &)) :
WxxDefault("Sub"),
fn(fnarg)
{
}
WxxSub::WxxSub(WxxObjectPtr(*fnarg)(const WxxObjectArray &),
const std::string &subname) :
WxxDefault("Sub"),
name(subname),
fn(fnarg)
{
}
std::string WxxSub::get_string()
{
return name;
}
void * WxxSub::get_pointer()
{
return (void *)fn;
}
WxxObjectPtr WxxSub::operator()(WxxObjectArray &args)
{
return (*fn)(args);
}
//*************************************************************
WxxClosure::WxxClosure(WxxInnerFunction * fnarg) :
WxxDefault("Sub"),
fn(fnarg)
{
fn->incref();
}
WxxClosure::~WxxClosure()
{
fn->decref();
}
WxxObjectPtr WxxClosure::operator()(WxxObjectArray &args)
{
return (*fn)(args);
}
//*************************************************************
typedef std::map<std::string, WxxClass *> WxxClassReg;
typedef std::vector<std::string> attrs_t;
typedef std::map<std::string, WxxClass::memberfun> regfun_t;
static WxxClassReg *wxxclassreg = 0;
WxxClass * WxxClass::getclass(const std::string &name)
{
if (wxxclassreg)
return (*wxxclassreg)[name];
else
return 0;
}
WxxClass::WxxClass(const std::string &name) :
WxxDefault("Class"),
clname(name)
{
if (! wxxclassreg)
wxxclassreg = new WxxClassReg();
(*wxxclassreg)[name] = this;
}
std::string WxxClass::get_string()
{
return clname;
}
std::string WxxClass::class_name() const
{
return clname;
}
void WxxClass::addbase(WxxClass *base)
{
if (!base)
throw std::runtime_error("Invalid call to addbase");
attrs_t & baseattrs = base->attrs;
for (attrs_t::iterator it = baseattrs.begin(); it != baseattrs.end(); ++it)
addattribute(*it);
regfun_t & baseregfun = base->regfun;
for (regfun_t::iterator it = baseregfun.begin(); it != baseregfun.end(); ++it)
addfunction(it->first, it->second);
}
void WxxClass::addattribute(const std::string &attrname)
{
attrs.push_back(attrname);
}
void WxxClass::addfunction(const std::string &fname, memberfun fun)
{
regfun[fname] = fun;
}
WxxClass::memberfun WxxClass::getfunction(const std::string &fname)
{
return regfun[fname];
}
WxxObjectPtr WxxClass::get_class()
{
return this;
}
WxxObjectPtr WxxClass::instantiate()
{
WxxInstance *instance = new WxxInstance(*this);
for (std::vector<std::string>::const_iterator it = attrs.begin();
it != attrs.end();
++it)
instance->attributes.insert(std::make_pair(*it, winxedxxnull));
return instance;
}
WxxObjectPtr WxxClass::call_method(const std::string &methname, WxxObjectArray &args)
{
if (methname == "new")
return instantiate();
else
return WxxDefault::call_method(methname, args);
}
//*************************************************************
WxxInstance::WxxInstance(const std::string &clname) :
WxxDefault(clname)
{
cl = WxxClass::getclass(clname);
if (! cl)
throw wxx_error("class not found: " + clname);
}
WxxInstance::WxxInstance(WxxClass &classobj) :
WxxDefault(classobj.class_name()),
cl(& classobj)
{
}
std::string WxxInstance::class_name() const
{
return cl->class_name();
}
WxxObjectPtr WxxInstance::get_class()
{
return cl;
}
WxxObjectPtr & WxxInstance::get_attr_str(const std::string &s)
{
std::map<std::string, WxxObjectPtr>::iterator attr = attributes.find(s);
if (attr == attributes.end())
throw wxx_error("No such attribute '" + s + "'");
else
return attr->second;
}
void WxxInstance::set_attr_str(const std::string &s, const WxxObjectPtr &value)
{
std::map<std::string, WxxObjectPtr>::iterator attr = attributes.find(s);
if (attr == attributes.end())
throw wxx_error("No such attribute '" + s + "'");
else
attr->second = value;
}
WxxObjectPtr WxxInstance::call_method(const std::string &methname, WxxObjectArray &args)
{
WxxClass::memberfun fun = cl->getfunction(methname);
if (fun) {
WxxObjectPtr obj(this);
return (*fun)(obj, args);
}
else
throw wxx_error("method '" + methname + "' not found in '" + class_name() + "'");
return winxedxxnull;
}
//*************************************************************
WxxLibrary::WxxLibrary(void *dl_handle) :
WxxDefault("ParrotLibrary"),
dl_h(dl_handle)
{
}
int WxxLibrary::get_bool()
{
return dl_h != 0;
}
int WxxLibrary::get_integer()
{
return dl_h != 0;
}
void * WxxLibrary::get_pointer()
{
return dl_h;
}
void * WxxLibrary::getsym(const std::string &funcname)
{
return dlsym(dl_h, funcname.c_str());
}
//*************************************************************
} // namespace WinxedXX
// End