-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptable.cpp
More file actions
146 lines (109 loc) · 3.17 KB
/
Scriptable.cpp
File metadata and controls
146 lines (109 loc) · 3.17 KB
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
#include "Pch.h"
#include "Interfaces.h"
#include "Formula.h"
#include "Actions.h"
#include "TokenPool.h"
#include "PropertyBag.h"
#include "EventHandler.h"
#include "Scriptable.h"
#include "EngineBind.h"
Scriptable::Scriptable(Scriptable && other)
: m_scopes(std::move(other.m_scopes)),
m_listMemberships(std::move(other.m_listMemberships)),
m_bindings(std::move(other.m_bindings))
{
m_eventHandlers = other.m_eventHandlers;
other.m_eventHandlers = nullptr;
}
Scriptable::~Scriptable() {
for(auto & membership : m_listMemberships)
membership.owner->ListRemoveEntry(membership.token, *this);
for(auto & pair : m_bindings)
delete pair.second;
if(m_eventHandlers)
m_eventHandlers->DecRef();
}
void Scriptable::AddBinding(unsigned bindingToken) {
assert(m_bindings.find(bindingToken) == m_bindings.end());
m_bindings[bindingToken] = nullptr;
}
void Scriptable::BindAll(IEngineBinder * binder, ScriptWorld * world) {
for(auto & pair : m_bindings) {
if(pair.second == nullptr) {
pair.second = binder->CreateBinding(this, world, pair.first);
}
}
}
IEngineBinding * Scriptable::GetBinding(unsigned token) {
auto iter = m_bindings.find(token);
if(iter == m_bindings.end())
return nullptr;
return iter->second;
}
const IEngineBinding * Scriptable::GetBinding(unsigned token) const {
auto iter = m_bindings.find(token);
if(iter == m_bindings.end())
return nullptr;
return iter->second;
}
Scriptable * Scriptable::Instantiate() const {
Scriptable * clone = new Scriptable;
clone->m_eventHandlers = m_eventHandlers;
clone->m_scopes.InstantiateFrom(m_scopes);
clone->m_bindings = m_bindings;
if(m_eventHandlers)
m_eventHandlers->IncRef();
BindingPropertyBag bag(clone);
clone->m_scopes.SetBindings(bag);
return clone;
}
void Scriptable::OnListMembershipAdded(unsigned listToken, IListContainer * owner) const {
Membership membership;
membership.owner = owner;
membership.token = listToken;
m_listMemberships.push_back(membership);
}
void Scriptable::OnListMembershipRemoved(unsigned listToken, IListContainer * owner) const {
for(auto iter = m_listMemberships.begin(); iter != m_listMemberships.end(); ) {
if(iter->owner == owner && iter->token == listToken)
iter = m_listMemberships.erase(iter);
else
++iter;
}
}
Result Scriptable::ResolveBinding(const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
const IEngineBinding * binding = nullptr;
if(scope) {
binding = GetBinding(scope);
}
else {
for(auto iter : m_bindings) {
if(iter.second->HasPropertyBinding(token)) {
binding = iter.second;
break;
}
}
}
Result ret;
if (!binding) {
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
ret.code = RESULT_CODE_OK;
unsigned arity = binding->GetPropertyBinding(token, &ret.payload.txt.token);
if (arity == 1) {
ret.type = RESULT_TYPE_TOKEN;
return ret;
}
arity = binding->GetPropertyBinding(token, &ret.payload.num.value, &ret.payload.num.value2);
if(arity == 0)
ret.code = RESULT_CODE_MISSING_DEFINITION;
else if(arity == 1)
ret.type = RESULT_TYPE_SCALAR;
else if(arity == 2)
ret.type = RESULT_TYPE_VECTOR2;
else
ret.code = RESULT_CODE_TYPE_ERROR;
return ret;
}