-
Notifications
You must be signed in to change notification settings - Fork 1
/
oo_manager.cpp
161 lines (139 loc) · 3.15 KB
/
oo_manager.cpp
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
#include "oo_manager.h"
namespace oo
{
Manager::~Manager()
{
Clear();
}
std::weak_ptr<Class> Manager::NewClass(const std::string &name)
{
auto pairs = m_classes.emplace(name, std::make_shared<Class>(name));
return pairs.first->second;
}
std::weak_ptr<Class> Manager::NewClass(const std::string &name, std::vector<std::weak_ptr<Class>> &&supers)
{
auto pairs = m_classes.emplace(name, std::make_shared<Class>(name, std::move(supers)));
return pairs.first->second;
}
ObjectHash Manager::NewObject(std::weak_ptr<Class> isa)
{
std::shared_ptr<Object> obj = std::make_shared<Object>();
obj->isa = isa;
for (auto cls : isa.lock()->mro)
{
for (const auto &pairs : cls.lock()->vars)
{
std::string name = cls.lock()->name + "@" + pairs.first;
int size = pairs.second;
obj->vars.try_emplace(name, Variable(size));
}
}
auto hash = FindEmptyObjectID();
m_objects.emplace(hash, std::move(obj));
m_object_index++;
return hash;
}
void Manager::DeleteObject(ObjectHash obj_hash)
{
m_objects.erase(obj_hash);
}
int Manager::FindEmptyObjectID()
{
if (!m_object_check)
{
if (m_object_index == 0)
{
m_object_check = true;
m_object_index++;
while (m_object_index == 0 || m_objects.find(m_object_index) != m_objects.end())
{
m_object_index++;
}
}
}
else
{
while (m_object_index == 0 || m_objects.find(m_object_index) != m_objects.end())
{
m_object_index++;
}
}
return m_object_index;
}
std::weak_ptr<Class> Manager::ToClass(const std::string &name) const
{
auto iter = m_classes.find(name);
if (iter == m_classes.end())
return std::weak_ptr<Class>();
return iter->second;
}
std::weak_ptr<Object> Manager::ToObject(ObjectHash obj_hash) const
{
auto iter = m_objects.find(obj_hash);
if (iter == m_objects.end())
return std::weak_ptr<Object>();
return iter->second;
}
const Constructor* Manager::FindConstructor(std::weak_ptr<Class> cls, int arg_size) const
{
for (auto current : cls.lock()->mro)
{
const auto &ctors = current.lock()->ctors;
auto it = ctors.find(arg_size);
if (it != ctors.end())
{
return &(it->second);
}
}
return nullptr;
}
const Method* Manager::FindMethod(std::weak_ptr<Class> cls, const std::string &name) const
{
for (auto current : cls.lock()->mro)
{
const auto &mthds = current.lock()->mthds;
auto iter = mthds.find(name);
if (iter != mthds.end())
{
return &(iter->second);
}
}
return nullptr;
}
Variable* Manager::FindVariable(std::weak_ptr<Object> obj, const std::string &name)
{
auto &vars = obj.lock()->vars;
if (name.find("@") == std::string::npos)
{
for (auto current : obj.lock()->isa.lock()->mro)
{
auto iter = vars.find(current.lock()->name + "@" + name);
if (iter != vars.end())
{
return &(iter->second);
}
}
}
else
{
auto iter = vars.find(name);
if (iter != vars.end())
{
return &(iter->second);
}
}
return nullptr;
}
void Manager::Clear()
{
m_classes.clear();
m_objects.clear();
m_object_index = 1;
m_object_check = false;
}
Manager *Manager::Instance()
{
static Manager mgr;
return &mgr;
}
}