diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-13 23:44:40 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-13 23:44:40 +0100 |
commit | c5345f13b3d33630d3e3d0021f10945627ae6e2d (patch) | |
tree | e2464f0c638818034ca9fc937dfdc044d3d70cf3 /src/core/common/Property.cpp | |
parent | df82b147b98cd09cb94f017910e400e148886fbe (diff) |
Added new Property class for defining getters and setters
Diffstat (limited to 'src/core/common/Property.cpp')
-rw-r--r-- | src/core/common/Property.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/core/common/Property.cpp b/src/core/common/Property.cpp new file mode 100644 index 0000000..ea40182 --- /dev/null +++ b/src/core/common/Property.cpp @@ -0,0 +1,96 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "Exceptions.hpp" +#include "Logger.hpp" +#include "Property.hpp" +#include "VariantConverter.hpp" + +namespace ousia { + +/* Class PropertyType */ + +const PropertyType PropertyType::None; + +/* Class GetterFunction */ + +void GetterFunction::validateArguments(Variant::arrayType &args) const +{ + if (!args.empty()) { + throw PropertyException( + std::string("Getter function has no arguments, but got ") + + std::to_string(args.size())); + } +} + +void GetterFunction::validateResult(Variant &res) const +{ + ExceptionLogger logger; + VariantConverter::convert(res, propertyType->type, propertyType->innerType, + logger); +} + +Variant GetterFunction::get(void *obj) +{ + return call(Variant::arrayType{}, obj); +} + +/* Class SetterFunction */ + +void SetterFunction::validateArguments(Variant::arrayType &args) const +{ + // Make sure exactly one argument is given + if (args.size() != 1U) { + throw PropertyException( + std::string( + "Expected exactly one argument to be passed to the property " + "setter, but got ") + + std::to_string(args.size())); + } + + // Convert the one argument to the requested type, throw an exception if + // this fails. + ExceptionLogger logger; + VariantConverter::convert(args[0], propertyType->type, + propertyType->innerType, logger); +} + +void SetterFunction::set(const Variant &value, void *obj) +{ + call(Variant::arrayType{value}, obj); +} + +/* Class PropertyDescriptor */ + +PropertyDescriptor::PropertyDescriptor(const PropertyType &type, + std::unique_ptr<GetterFunction> getter, + std::unique_ptr<SetterFunction> setter) + : type(type), getter(std::move(getter)), setter(std::move(setter)) +{ + if (!this->getter->isValid()) { + throw PropertyException( + "Getter must be valid, writeonly properties are not " + "supported!"); + } + + // Assign the property type reference to the getter and setter + this->getter->propertyType = &this->type; + this->setter->propertyType = &this->type; +} +} + |