-
Notifications
You must be signed in to change notification settings - Fork 1
/
winxedxx_namespace.cxx
125 lines (103 loc) · 2.85 KB
/
winxedxx_namespace.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
// winxedxx_namespace.cxx
// (C) 2012 Julián Albo "NotFound"
#include "winxedxx_namespace.h"
#include "winxedxx_integer.h"
namespace WinxedXX
{
typedef std::map<std::string, WxxObjectPtr> symbols_t;
typedef std::map<std::string, WxxNamespace *> childs_t;
WxxNamespace::WxxNamespace() :
WxxDefault("NameSpace"),
name("winxedxx"),
parentns(0)
{
}
WxxNamespace::WxxNamespace(const std::string &nsname, WxxNamespace *parent) :
WxxDefault("NameSpace"),
name(nsname),
parentns(parent)
{
}
WxxObjectPtr WxxNamespace::get(const std::string &name)
{
symbols_t::const_iterator it = symbols.find(name);
if (it == symbols.end())
return winxedxxnull;
return WxxObjectPtr(it->second);
}
WxxObjectPtr WxxNamespace::find_symbol(const std::string &name)
{
symbols_t::const_iterator it = symbols.find(name);
if (it == symbols.end()) {
if (parentns)
return parentns->find_symbol(name);
else
return winxedxxnull;
}
return WxxObjectPtr(it->second);
}
WxxNamespace &WxxNamespace::childNamespace(const std::string &name)
{
childs_t::const_iterator it = childs.find(name);
if (it == childs.end()) {
WxxNamespace *child = new WxxNamespace(name, this);
childs[name] = child;
return *child;
}
return *it->second;
}
void WxxNamespace::set(const std::string &name, const WxxObjectPtr &value)
{
symbols[name] = value;
}
void WxxNamespace::setClass(const std::string &name, WxxClass *value)
{
classes[name] = value;
symbols[name] = WxxObjectPtr(value);
}
WxxClass * WxxNamespace::getClass(const std::string &name)
{
WxxClass *cl = classes[name];
if (!cl)
throw wxx_error("class not found in namespace: " + name);
return cl;
}
std::string WxxNamespace::get_string()
{
return name;
}
WxxObjectPtr WxxNamespace::get_pmc_keyed(const std::string &s)
{
return get(s);
}
void WxxNamespace::set_pmc_keyed(const std::string &s, const WxxObjectPtr &value)
{
set(s, value);
}
WxxObjectPtr WxxNamespace::call_method(const std::string &methname, WxxObjectArray &args)
{
if (methname == "get_parent")
return parentns ? parentns : winxedxxnull;
else if (methname == "find_var")
return get(args.get_string_keyed(0));
else if (methname == "add_var") {
set(args.get_string_keyed(0), args.get_pmc_keyed(1));
return winxedxxnull;
}
else
return WxxDefault::call_method(methname, args);
}
//**************************************************************
class WxxRootNamespace : WxxNamespace
{
friend WxxNamespace &getRootNamespace();
};
static WxxRootNamespace *rootnamespace = 0;
WxxNamespace &getRootNamespace()
{
if (! rootnamespace)
rootnamespace = new WxxRootNamespace();
return *rootnamespace;
}
} // namespace WinxedXX
// End of winxedxx_namespace.cxx