diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-21 00:40:52 +0100 |
---|---|---|
committer | Andreas Stöckel <andreas@somweyr.de> | 2014-12-21 00:40:52 +0100 |
commit | 989cc6d8064a95426962588f806bb987a6896e7f (patch) | |
tree | 9e8b4b923dff3305396fba8414fc46c2a9d59c07 /src | |
parent | 30c60d9317c7f56b17dce82a4a02fd50c3a7dfc2 (diff) |
moved Rtti from managed to common folder, added Function header
Diffstat (limited to 'src')
-rw-r--r-- | src/core/common/Function.cpp | 24 | ||||
-rw-r--r-- | src/core/common/Function.hpp | 149 | ||||
-rw-r--r-- | src/core/common/Rtti.cpp (renamed from src/core/managed/Rtti.cpp) | 12 | ||||
-rw-r--r-- | src/core/common/Rtti.hpp (renamed from src/core/managed/Rtti.hpp) | 51 | ||||
-rw-r--r-- | src/core/managed/Managed.hpp | 3 |
5 files changed, 226 insertions, 13 deletions
diff --git a/src/core/common/Function.cpp b/src/core/common/Function.cpp new file mode 100644 index 0000000..d4b8ccc --- /dev/null +++ b/src/core/common/Function.cpp @@ -0,0 +1,24 @@ +/* + 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 "Function.hpp" + +namespace ousia { + +} + diff --git a/src/core/common/Function.hpp b/src/core/common/Function.hpp new file mode 100644 index 0000000..3a0ed9f --- /dev/null +++ b/src/core/common/Function.hpp @@ -0,0 +1,149 @@ +/* + 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/>. +*/ + +/** + * @file Function.hpp + * + * Contains the definition of a Function class used to describe both methods and + * functions in the host code and functions in the script code. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_FUNCTION_HPP_ +#define _OUSIA_FUNCTION_HPP_ + +#include <cassert> +#include <memory> + +#include "Variant.hpp" + +namespace ousia { + +/** + * The AbstractFunction interface defines all the methods needed to represent + * a reference at a generic function object. Function objects can be called + * using the call function in which an array of Variant is supplied to the + * function and a Variant is returned to the caller. + */ +class AbstractFunction { +private: + /** + * Private, non-callable constructor. + */ + AbstractFunction(){}; + + /* No copy constructor */ + AbstractFunction(const AbstractFunction &) = delete; + + /* No move constructor */ + AbstractFunction(AbstractFunction &&) = delete; + +public: + /** + * Pure virtual method used to create a copy of the AbstractFunction + * object. + * + * @return a unique pointer pointing at a copy of the AbstractFunction + * object. + */ + virtual std::unique_ptr<AbstractFunction> clone() const = 0; + + /** + * Virtual destructor. + */ + virtual ~AbstractFunction() {} + + /** + * Abstract function which is meant to call the underlying function (be it + * a host or a script function) with the given arguments. + * + * @param args is a vector containing all arguments that shall be passed to + * the function. + * @return a Variant containing the return value. + */ + virtual Variant call(const Variant::arrayType &args = Variant::arrayType{}, + void *thisRef = nullptr) const = 0; +}; + +/** + * The Method class refers to a method in the C++ code, belonging to an object + * of a certain type T. + * + * @tparam T is the type of the method that should be called. + */ +template <class T> +class Method : public AbstractFunction { +public: + /** + * Type of the Callback function that is being called by the "call" + * function. + * + * @param args contains the input arguments that were passed to the + * function. + * @param thisRef is a pointer pointing at an instance of type T. + * @return the return value of the function as Variant instance. + */ + using Callback = Variant (*)(const Variant::arrayType &args, T *thisRef); + +private: + /** + * Pointer at the actual C++ method being called. + */ + const Callback method; + +public: + /** + * Constructor of the Method class. + * + * @param method is a pointer at the C++ function that should be called. + */ + Method(Callback method) : method(method){}; + + /** + * Creates a copy of this Method object. + */ + std::unique_ptr<AbstractFunction> clone() const override + { + return std::unique_ptr<AbstractFunction>{new Method<T>(method)}; + } + + /** + * Calls the underlying method. + * + * @param args is a vector containing all arguments that shouild be passed + * to the method. + * @return a Variant containing the return value. + */ + Variant call(const Variant::arrayType &args = Variant::arrayType{}, + void *thisRef = nullptr) const override + { + // Dynamically cast thisRef to the given type + T *tRef = dynamic_cast<T>(thisRef); + + // Make sure the cast is successfull + assert(tRef != nullptr); + + // Call the method + return method(args, tRef); + } +}; +} + +#endif /* _OUSIA_FUNCTION_HPP_ */ + diff --git a/src/core/managed/Rtti.cpp b/src/core/common/Rtti.cpp index eade524..eeae41f 100644 --- a/src/core/managed/Rtti.cpp +++ b/src/core/common/Rtti.cpp @@ -38,7 +38,7 @@ const RttiBase &RttiStore::lookup(const std::type_info &native) const auto &tbl = table(); auto it = tbl.find(std::type_index{native}); if (it == tbl.end()) { - return RttiBase::None; + return RttiTypes::None; } else { return *(it->second); } @@ -46,8 +46,6 @@ const RttiBase &RttiStore::lookup(const std::type_info &native) /* Class RttiBase */ -const RttiBase RttiBase::None; - bool RttiBase::isa(const RttiBase &other) const { if (&other == this) { @@ -61,6 +59,14 @@ bool RttiBase::isa(const RttiBase &other) const return false; } +/* Constant initialization */ + +const RttiBase RttiTypes::None; +const RttiBase RttiTypes::Int; +const RttiBase RttiTypes::Double; +const RttiBase RttiTypes::String; +const RttiBase RttiTypes::Array; +const RttiBase RttiTypes::Map; } diff --git a/src/core/managed/Rtti.hpp b/src/core/common/Rtti.hpp index 4a754a7..b91bd35 100644 --- a/src/core/managed/Rtti.hpp +++ b/src/core/common/Rtti.hpp @@ -49,7 +49,7 @@ * \code{.hpp} * // Only needed if the type needs to be accessed * // from other compilation units! - * const Rtti<MyType> MyType_Rtti; + * extern const Rtti<MyType> MyType_Rtti; * \endcode * In the source file: * \code{.cpp} @@ -59,8 +59,8 @@ * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ -#ifndef _OUSIA_MANAGED_RTTI_HPP_ -#define _OUSIA_MANAGED_RTTI_HPP_ +#ifndef _OUSIA_RTTI_HPP_ +#define _OUSIA_RTTI_HPP_ #include <typeinfo> #include <typeindex> @@ -117,11 +117,6 @@ private: public: /** - * Rtti of no particular type. - */ - static const RttiBase None; - - /** * Human readable name associated with the type. */ const std::string name; @@ -214,7 +209,45 @@ inline const RttiBase &typeOf(const T &obj) { return RttiStore::lookup(typeid(obj)); } + +/** + * Struct defining static constants describing certain Variant types. These + * constants are used to e.g. define the type of function arguments while + * allowing for both primitive variant types and more complex variant types. + */ +struct RttiTypes { + /** + * Type of no particular color. + */ + static const RttiBase None; + + /** + * Constant representing a variant int type. + */ + static const RttiBase Int; + + /** + * Constant representing a variant double type. + */ + static const RttiBase Double; + + /** + * Constant representing a variant string type. + */ + static const RttiBase String; + + /** + * Constant representing a variant array type. + */ + static const RttiBase Array; + + /** + * Constant representing a variant map type. + */ + static const RttiBase Map; +}; + } -#endif /* _OUSIA_MANAGED_RTTI_HPP_ */ +#endif /* _OUSIA_RTTI_HPP_ */ diff --git a/src/core/managed/Managed.hpp b/src/core/managed/Managed.hpp index 8582702..70136d3 100644 --- a/src/core/managed/Managed.hpp +++ b/src/core/managed/Managed.hpp @@ -19,7 +19,8 @@ #ifndef _OUSIA_MANAGED_HPP_ #define _OUSIA_MANAGED_HPP_ -#include "Rtti.hpp" +#include <core/common/Rtti.hpp> + #include "Manager.hpp" namespace ousia { |