From c5345f13b3d33630d3e3d0021f10945627ae6e2d Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 13 Jan 2015 23:44:40 +0100 Subject: Added new Property class for defining getters and setters --- src/core/common/Property.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/core/common/Property.cpp (limited to 'src/core/common/Property.cpp') 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 . +*/ + +#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 getter, + std::unique_ptr 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; +} +} + -- cgit v1.2.3