diff options
Diffstat (limited to 'src/core/script/Object.cpp')
-rw-r--r-- | src/core/script/Object.cpp | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/src/core/script/Object.cpp b/src/core/script/Object.cpp index 8d858bc..8fa9771 100644 --- a/src/core/script/Object.cpp +++ b/src/core/script/Object.cpp @@ -21,63 +21,90 @@ namespace ousia { namespace script { -bool Object::hasElement(std::string name) const +bool Object::hasElement(const std::string &name) const { return (properties.find(name) != properties.end()) || (methods.find(name) != methods.end()); } -void Object::addProperty(std::string name, const Property &property) +void Object::addProperty(const std::string &name, const Property &property) { if (hasElement(name)) { // TODO Throw another exception class here throw "Element already exists"; } - properties.emplace(name, property); + properties.insert(std::make_pair(name, property)); } -void Object::addProperty(std::string name, const Getter &get, const Setter &set) +void Object::addProperty(const std::string &name, const Getter &get, const Setter &set) { addProperty(name, Property{get, set}); } -void Object::addProperty(std::string name, VariantType type, +void Object::addProperty(const std::string &name, VariantType type, const GetterCallback get, const SetterCallback set) { addProperty(name, Property{type, get, set, data}); } -void Object::addReadonlyProperty(std::string name, const Getter &get) +void Object::addReadonlyProperty(const std::string &name, const Getter &get) { addProperty(name, Property{get, Setter{VariantType::null, nullptr}}); } -void Object::addReadonlyProperty(std::string name, const GetterCallback get) +void Object::addReadonlyProperty(const std::string &name, const GetterCallback get) { addProperty( name, Property{Getter{get, data}, Setter{VariantType::null, nullptr}}); } -void Object::addMethod(std::string name, const HostFunction &fun) +void Object::addMethod(const std::string &name, const HostFunction &fun) { if (hasElement(name)) { // TODO Throw another exception class here throw "Element already exists"; } - methods.emplace(name, fun); + methods.insert(std::make_pair(name, fun)); } -void Object::addMethod(std::string name, const HostFunctionCallback fun) +void Object::addMethod(const std::string &name, const HostFunctionCallback fun) { addMethod(name, HostFunction{fun, data}); } -void Object::addMethod(std::string name, const HostFunctionCallback fun, +void Object::addMethod(const std::string &name, const HostFunctionCallback fun, const std::vector<Argument> &signature) { addMethod(name, HostFunction{fun, signature, data}); } +const Property *Object::getProperty(const std::string &name) const +{ + auto it = properties.find(name); + return (it != properties.end()) ? &(it->second) : nullptr; +} + +const Function *Object::getMethod(const std::string &name) const +{ + auto it = methods.find(name); + return (it != methods.end()) ? &(it->second) : nullptr; +} + +bool Object::removeElement(const std::string &name) +{ + return removeProperty(name) || removeMethod(name); +} + +bool Object::removeProperty(const std::string &name) +{ + return properties.erase(name) > 0; +} + +bool Object::removeMethod(const std::string &name) +{ + return methods.erase(name) > 0; +} + } } |