-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComponentBaseImpl.cpp
234 lines (193 loc) · 6.11 KB
/
ComponentBaseImpl.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
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
#include "ComponentBaseImpl.h"
// -----------------------------------------------------------------------------
// --SECTION-- Utils
// -----------------------------------------------------------------------------
namespace memutils {
bool copy_string(const ComponentBaseImpl& comp, const std::wstring& str, WCHAR_T** out) {
if (*out = comp.alloc<WCHAR_T[]>(size_t(str.size() + 1))) {
std::copy(str.begin(), str.end(), *out);
return true;
}
return false;
}
WCHAR_T* copy_string(const ComponentBaseImpl& comp, const std::wstring& str) {
WCHAR_T* out;
auto res = copy_string(comp, str, &out);
assert(res);
return out;
}
} // memutils
// -----------------------------------------------------------------------------
// --SECTION-- ComponentBaseImpl
// -----------------------------------------------------------------------------
ComponentBaseImpl::Property::Property(const std::wstring& name)
: name(name) {
}
ComponentBaseImpl::Property::Property(ComponentBaseImpl::Property&& rhs)
: name(std::move(rhs.name)),
eng_name(std::move(rhs.eng_name)),
get(std::move(rhs.get)),
set(std::move(rhs.set)) {
}
ComponentBaseImpl::Property&
ComponentBaseImpl::Property::operator=(ComponentBaseImpl::Property&& rhs) {
if (this != &rhs) {
name = std::move(rhs.name);
eng_name = std::move(rhs.eng_name);
get = std::move(rhs.get);
set = std::move(rhs.set);
}
return *this;
}
ComponentBaseImpl::Method::Method(const std::wstring& name)
: name(name) {
}
ComponentBaseImpl::Method::Method(ComponentBaseImpl::Method&& rhs)
: func(std::move(rhs.func)),
args(std::move(rhs.args)),
name(std::move(rhs.name)),
eng_name(std::move(rhs.eng_name)),
is_func(rhs.is_func) {
}
ComponentBaseImpl::Method&
ComponentBaseImpl::Method::operator=(ComponentBaseImpl::Method&& rhs) {
if (this != &rhs) {
func = std::move(rhs.func);
args = std::move(rhs.args);
name = std::move(rhs.name);
eng_name = std::move(rhs.eng_name);
is_func = rhs.is_func;
}
return *this;
}
bool ComponentBaseImpl::Method::empty_argument(tVariant* arg) {
TV_VT(arg) = VTYPE_EMPTY;
return true;
}
ComponentBaseImpl::ComponentBaseImpl(const std::wstring& name, long version)
: name_(name), connect_{}, memory_{}, ver_(version) {
}
// IInitDoneBase
bool ComponentBaseImpl::Init(void* conn) {
return nullptr != (connect_ = reinterpret_cast<IAddInDefBase*>(conn));
}
bool ComponentBaseImpl::setMemManager(void* mem) {
return nullptr != (memory_ = reinterpret_cast<IMemoryManager*>(mem));
}
long ComponentBaseImpl::GetInfo() {
return ver_;
}
void ComponentBaseImpl::Done() {}
// ILanguageExtenderBase
bool ComponentBaseImpl::RegisterExtensionAs(WCHAR_T** extName) {
return memutils::copy_string(*this, name_, extName);
}
long ComponentBaseImpl::GetNProps() {
return props_.size();
}
long ComponentBaseImpl::FindProp(const WCHAR_T* wsPropName) {
return props_.find_id(wsPropName);
}
const WCHAR_T* ComponentBaseImpl::GetPropName(
long lPropNum,
long lPropAlias) {
auto& prop = props_.at(lPropNum);
return memutils::copy_string(*this, (lPropAlias ? prop.name : prop.eng_name));
}
bool ComponentBaseImpl::GetPropVal(
const long lPropNum,
tVariant* pvarPropVal) {
return props_.at(lPropNum).get(pvarPropVal);
}
bool ComponentBaseImpl::SetPropVal(
const long lPropNum,
tVariant* varPropVal) {
return props_.at(lPropNum).set(varPropVal);
}
bool ComponentBaseImpl::IsPropReadable(const long lPropNum) {
return bool(props_.at(lPropNum).get);
}
bool ComponentBaseImpl::IsPropWritable(const long lPropNum) {
return bool(props_.at(lPropNum).set);
}
long ComponentBaseImpl::GetNMethods() {
return methods_.size();
}
long ComponentBaseImpl::FindMethod(const WCHAR_T* wsMethodName) {
return methods_.find_id(wsMethodName);
}
const WCHAR_T* ComponentBaseImpl::GetMethodName(
const long lMethodNum,
const long lMethodAlias) {
auto& meth = methods_.at(lMethodNum);
return memutils::copy_string(*this, lMethodAlias ? meth.name : meth.eng_name);
}
long ComponentBaseImpl::GetNParams(const long lMethodNum) {
return methods_.at(lMethodNum).args.size();
}
bool ComponentBaseImpl::GetParamDefValue(
const long lMethodNum,
const long lParamNum,
tVariant *pvarParamDefValue) {
return methods_.at(lMethodNum).args[lParamNum](pvarParamDefValue);
}
bool ComponentBaseImpl::HasRetVal(const long lMethodNum) {
return methods_.at(lMethodNum).is_func;
}
bool ComponentBaseImpl::CallAsProc(
const long lMethodNum,
tVariant* paParams,
const long lSizeArray) {
return methods_.at(lMethodNum).func(paParams, lSizeArray, nullptr);
}
bool ComponentBaseImpl::CallAsFunc(
const long lMethodNum,
tVariant* pvarRetValue,
tVariant* paParams,
const long lSizeArray) {
return methods_.at(lMethodNum).func(paParams, lSizeArray, pvarRetValue);
}
// LocaleBase
void ComponentBaseImpl::SetLocale(const WCHAR_T* loc) {
#ifndef __linux__
_wsetlocale(LC_ALL, loc);
#else
//We convert in char* char_locale
//also we establish locale
//setlocale(LC_ALL, char_locale);
#endif
}
void* ComponentBaseImpl::malloc(unsigned long size) const {
void* out{};
if (memory_) {
memory_->AllocMemory(&out, size);
}
return out;
}
void ComponentBaseImpl::free(void** ptr) const {
if (memory_) {
memory_->FreeMemory(ptr);
}
}
void ComponentBaseImpl::add_property(
const std::wstring& name,
const Property::prop_f& get /* = Property::prop_f() */,
const Property::prop_f& set /* = Property::prop_f() */,
const std::wstring& eng_name /* = std::wstring() */ ) {
auto& prop = props_.insert(name);
prop.eng_name = eng_name.empty() ? name : eng_name;
prop.get = get;
prop.set = set;
}
void ComponentBaseImpl::add_method(
const std::wstring& name, bool is_func,
const Method::method_f& func,
const Method::get_arg_list_t& args /* = {} */,
const std::wstring& eng_name /* = std::wstring() */ ) {
auto& meth = methods_.insert(name);
meth.eng_name = eng_name.empty() ? name : eng_name;
meth.is_func = is_func;
meth.args = args;
meth.func = func;
}
std::wstring ComponentRegister::class_names_;