OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 std::string name; | 173 std::string name; |
174 FunctionInfo call; | 174 FunctionInfo call; |
175 }; | 175 }; |
176 | 176 |
177 struct DifferentiatorInfo | 177 struct DifferentiatorInfo |
178 { | 178 { |
179 size_t offset; | 179 size_t offset; |
180 std::vector<std::pair<int, std::string>> mapping; | 180 std::vector<std::pair<int, std::string>> mapping; |
181 }; | 181 }; |
182 | 182 |
| 183 typedef std::vector<PropertyInfo> Properties; |
| 184 typedef std::vector<MethodInfo> Methods; |
| 185 |
183 struct ClassInfo | 186 struct ClassInfo |
184 { | 187 { |
185 TYPEID id; | 188 TYPEID id; |
186 TYPEID baseClass; | 189 TYPEID baseClass; |
187 std::string name; | 190 std::string name; |
188 std::vector<PropertyInfo> properties; | 191 Properties properties; |
189 std::vector<MethodInfo> methods; | 192 Methods methods; |
190 DifferentiatorInfo subclass_differentiator; | 193 DifferentiatorInfo subclass_differentiator; |
191 ptrdiff_t ref_counted_offset; | 194 ptrdiff_t ref_counted_offset; |
| 195 FunctionInfo instanceGetter; |
192 }; | 196 }; |
193 | 197 |
194 void register_class(const char* name, TYPEID classID, TYPEID baseClassID, | 198 void register_class(const char* name, TYPEID classID, TYPEID baseClassID, |
195 ptrdiff_t ref_counted_offset); | 199 ptrdiff_t ref_counted_offset, |
| 200 const FunctionInfo& instanceGetter = FunctionInfo()); |
196 | 201 |
197 void register_property(TYPEID classID, const char* name, | 202 void register_property(TYPEID classID, const char* name, |
198 const FunctionInfo& getter, const FunctionInfo& setter, | 203 const FunctionInfo& getter, const FunctionInfo& setter, |
199 const char* jsValue = ""); | 204 const char* jsValue = ""); |
200 | 205 |
201 void register_method(TYPEID classID, const char* name, | 206 void register_method(TYPEID classID, const char* name, |
202 const FunctionInfo& call); | 207 const FunctionInfo& call); |
203 | 208 |
204 void register_differentiator(TYPEID classID, size_t offset, | 209 void register_differentiator(TYPEID classID, size_t offset, |
205 std::vector<std::pair<int, std::string>>& mapping); | 210 std::vector<std::pair<int, std::string>>& mapping); |
206 | 211 |
207 std::string wrapCall(const FunctionInfo& call, bool isFunction = true); | 212 std::string wrapCall(const FunctionInfo& call, bool isFunction = true, |
| 213 const FunctionInfo& instanceGetter = FunctionInfo()); |
208 } | 214 } |
209 | 215 |
210 template<typename ClassType, | 216 template<typename ClassType, |
211 typename BaseClass = bindings_internal::NoBaseClass, | 217 typename BaseClass = bindings_internal::NoBaseClass, |
212 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ
e* = nullptr> | 218 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ
e* = nullptr> |
213 class class_ | 219 class class_ |
214 { | 220 { |
215 public: | 221 public: |
216 class_(const char* name) | 222 class_(const char* name) |
217 { | 223 { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::vector<std::pair<int, std::string>> mapping; | 287 std::vector<std::pair<int, std::string>> mapping; |
282 for (const auto& item : list) | 288 for (const auto& item : list) |
283 mapping.emplace_back(item.first, item.second); | 289 mapping.emplace_back(item.first, item.second); |
284 | 290 |
285 bindings_internal::register_differentiator( | 291 bindings_internal::register_differentiator( |
286 bindings_internal::TypeInfo<ClassType>(), offset, mapping); | 292 bindings_internal::TypeInfo<ClassType>(), offset, mapping); |
287 return *this; | 293 return *this; |
288 } | 294 } |
289 }; | 295 }; |
290 | 296 |
| 297 template<typename ClassType> |
| 298 class singleton |
| 299 { |
| 300 public: |
| 301 singleton(const char* name, ClassType* (*instanceGetter)()) |
| 302 { |
| 303 bindings_internal::register_class(name, |
| 304 bindings_internal::TypeInfo<ClassType>(), |
| 305 bindings_internal::TypeInfo<bindings_internal::NoBaseClass>(), |
| 306 0, |
| 307 instanceGetter |
| 308 ); |
| 309 } |
| 310 |
| 311 template<typename FieldType> |
| 312 const singleton& property(const char* name, |
| 313 FieldType (ClassType::*getter)() const, |
| 314 void (ClassType::*setter)(FieldType) = nullptr) const |
| 315 { |
| 316 bindings_internal::register_property( |
| 317 bindings_internal::TypeInfo<ClassType>(), name, getter, setter); |
| 318 return *this; |
| 319 } |
| 320 |
| 321 template<typename ReturnType, typename... Args> |
| 322 const singleton& function(const char* name, ReturnType (ClassType::*method)(Ar
gs...)) const |
| 323 { |
| 324 bindings_internal::register_method( |
| 325 bindings_internal::TypeInfo<ClassType>(), name, method); |
| 326 return *this; |
| 327 } |
| 328 |
| 329 template<typename ReturnType, typename... Args> |
| 330 const singleton& function(const char* name, ReturnType (ClassType::*method)(Ar
gs...) const) const |
| 331 { |
| 332 bindings_internal::register_method( |
| 333 bindings_internal::TypeInfo<ClassType>(), name, method); |
| 334 return *this; |
| 335 } |
| 336 }; |
| 337 |
291 void printBindings(); | 338 void printBindings(); |
OLD | NEW |