From 89d11c54d08ce78825ff7bcecdf44e3dfdfc9c17 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 11 Jan 2015 21:59:21 +0100 Subject: removed legacy files --- src/core/script/Function.cpp | 101 ----------- src/core/script/Function.hpp | 306 --------------------------------- src/core/script/Object.cpp | 110 ------------ src/core/script/Object.hpp | 144 ---------------- src/core/script/ScriptEngine.cpp | 67 -------- src/core/script/ScriptEngine.hpp | 223 ------------------------ src/core/script/Variant.cpp | 361 --------------------------------------- src/core/script/Variant.hpp | 269 ----------------------------- 8 files changed, 1581 deletions(-) delete mode 100644 src/core/script/Function.cpp delete mode 100644 src/core/script/Function.hpp delete mode 100644 src/core/script/Object.cpp delete mode 100644 src/core/script/Object.hpp delete mode 100644 src/core/script/ScriptEngine.cpp delete mode 100644 src/core/script/ScriptEngine.hpp delete mode 100644 src/core/script/Variant.cpp delete mode 100644 src/core/script/Variant.hpp (limited to 'src/core') diff --git a/src/core/script/Function.cpp b/src/core/script/Function.cpp deleted file mode 100644 index 84473a8..0000000 --- a/src/core/script/Function.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - 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 "Function.hpp" - -namespace ousia { -namespace script { - -/* Class ArgumentValidator */ - -std::pair> ArgumentValidator::setError( - int idx, const std::string &msg, std::vector &res) -{ - errorIndex = idx; - errorMessage = msg; - return std::make_pair(false, res); -} - -void ArgumentValidator::resetError() -{ - errorIndex = -1; - errorMessage = ""; -} - -std::pair> ArgumentValidator::validate( - const std::vector &args) -{ - std::vector res; - - // Reset any old error - resetError(); - - // Sanity check: Do not allow too many arguments - if (args.size() > signature.size()) { - return setError(signature.size(), - "Expected " + std::to_string(signature.size()) + - " arguments but got " + std::to_string(args.size()), - res); - } - - // Iterate over the given arguments and check their type - res.reserve(signature.size()); - for (unsigned int i = 0; i < args.size(); i++) { - // TODO: Implicit type conversion - const VariantType tGiven = args[i].getType(); - const VariantType tExpected = signature[i].type; - if (tGiven != tExpected) { - return setError(i, std::string("Expected type ") + - Variant::getTypeName(tExpected) + - " but got " + Variant::getTypeName(tGiven), - res); - } - res.push_back(args[i]); - } - - // Make sure the remaining arguments have a default value, and if they have - // one, add it to the result - for (unsigned int i = args.size(); i < signature.size(); i++) { - if (!signature[i].hasDefault) { - return setError(i, "Expected argument " + std::to_string(i), res); - } - res.push_back(signature[i].defaultValue); - } - - return std::make_pair(true, res); -} - -/* Class ValidatingFunction */ - -Variant ValidatingFunction::call(const std::vector &args) const -{ - if (validate) { - ArgumentValidator validator{signature}; - - std::pair> res = validator.validate(args); - if (!res.first) { - throw validator.error(); - } - return validatedCall(res.second); - } - - return validatedCall(args); -} -} -} - diff --git a/src/core/script/Function.hpp b/src/core/script/Function.hpp deleted file mode 100644 index 83160e4..0000000 --- a/src/core/script/Function.hpp +++ /dev/null @@ -1,306 +0,0 @@ -/* - 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 . -*/ - -#ifndef _OUSIA_FUNCTION_HPP_ -#define _OUSIA_FUNCTION_HPP_ - -#include -#include -#include - -#include "Variant.hpp" - -namespace ousia { -namespace script { - -/** - * The abstract Function class is most basic version of a function handle, - * maintaining a "call" function and basic virtual functions for lifecyle - * management. - */ -class Function { -public: - /** - * Virtual clone function (e.g. used in the variant class). - */ - virtual Function *clone() const = 0; - - /** - * Virtual destructor. - */ - virtual ~Function() {} - - /** - * 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 std::vector &args) const = 0; - - /** - * Calls the underlying function with no arguments. - * - * @return a Variant containing the return value. - */ - Variant call() const { return call({}); } - - // TODO: Use () operator instead of the call function -}; - -/** - * The Argument class is used to describe the type of a function - * argument. - */ -struct Argument { - const VariantType type; - const bool hasDefault; - const Variant defaultValue; - - Argument(VariantType type) : type(type), hasDefault(false){}; - - Argument(VariantType type, const Variant &defaultValue) - : type(type), hasDefault(true), defaultValue(defaultValue){}; -}; - -/** - * ArgumentValidatorError is an exception type used to represent argument - * validator errors. - */ -class ArgumentValidatorError : public std::exception { -public: - const int index; - - const std::string msg; - - ArgumentValidatorError(int index, const std::string &msg) - : index(index), msg(msg){}; - - virtual const char *what() const noexcept override { return msg.c_str(); } -}; - -/** - * The ArgumentValidator class is responsible for checking whether the given - * arguments passed to a function match the description. - */ -class ArgumentValidator { -private: - /** - * List containing the argument descriptors. - */ - const std::vector signature; - - /** - * Argument index in the input array, at which the last error occured. - */ - int errorIndex = -1; - - /** - * Error message for the last validation error. - */ - std::string errorMessage; - - /** - * Used internally to update the errorIndex and the errorMessage fields. - */ - std::pair> setError(int idx, - const std::string &msg, - std::vector &res); - - /** - * Resets the error state. - */ - void resetError(); - -public: - - /** - * Constructor of the argument validator class. - * - * @param descriptors is a list of Arguments which should be used - * for the validation. - */ - ArgumentValidator(const std::vector &signature) - : signature(signature) - { - } - - /** - * Validates and augments the given argument list (e.g. adds the default - * values). - * - * @param args contains the input arguments. - * @return a pair, where the first element specifies whether the arguments - * were validated sucessfully and the second argument contains the augmented - * list of arguments. If false is returned, use the error function to get - * more information about the error. - */ - std::pair> validate( - const std::vector &args); - - /** - * Returns an ArgumentValidatorError instance containing the argument index - * in the input array, at which the error occured and an explaining error - * message. As ArgumentValidatorError is derived from std::exception, - * the result of this function is throwable. - * - * @return an ArgumentValidatorError instance containing information about - * the last error. If no error occurred, the message will be empty and - * the argument index will be set to -1. - */ - ArgumentValidatorError error() - { - return ArgumentValidatorError{errorIndex, errorMessage}; - } -}; - -/** - * A validating function - */ -class ValidatingFunction : public Function { -private: - /** - * Specifies whether the validating function should actually run or just - * pass the arguments through. - */ - bool validate; - - /** - * Signature for which the function should be validated. - */ - const std::vector signature; - -protected: - virtual Variant validatedCall(const std::vector &args) const = 0; - - virtual Variant call(const std::vector &args) const override; - - using Function::call; - -public: - ValidatingFunction() : validate(false) {} - - ValidatingFunction(const std::vector &signature) - : validate(true), signature(signature) - { - } -}; - -using HostFunctionCallback = Variant (*)(const std::vector &args, - void *data); -using GetterCallback = Variant (*)(void *data); -using SetterCallback = void (*)(Variant arg, void *data); - -class HostFunction : public ValidatingFunction { -private: - HostFunctionCallback callback; - void *data; - -protected: - virtual Variant validatedCall( - const std::vector &args) const override - { - return callback(args, data); - } - -public: - HostFunction(HostFunctionCallback callback, std::vector signature, - void *data = nullptr) - : ValidatingFunction(signature), callback(callback), data(data) - { - } - - HostFunction(HostFunctionCallback callback, void *data = nullptr) - : ValidatingFunction(), callback(callback), data(data) - { - } - - Function *clone() const override { return new HostFunction(*this); } - - using ValidatingFunction::call; -}; - -class Getter : public ValidatingFunction { -private: - GetterCallback callback; - void *data; - -protected: - virtual Variant validatedCall( - const std::vector &args) const override - { - if (!callback) { - // TODO: Use another exception class here - throw "Getter not defined"; - } - return callback(data); - } - -public: - Getter(GetterCallback callback, void *data = nullptr) - : ValidatingFunction(std::vector{}), - callback(callback), - data(data){}; - - Function *clone() const override { return new Getter(*this); } - - Variant call() const { return ValidatingFunction::call(); } - - Variant operator()() const { return call(); } - - bool exists() const { return callback != nullptr; } -}; - -class Setter : public ValidatingFunction { -private: - SetterCallback callback; - void *data; - -protected: - virtual Variant validatedCall( - const std::vector &args) const override - { - if (!callback) { - // TODO: Use another exception class here - throw "Setter not defined"; - } - callback(args[0], data); - return Variant::Null; - } - -public: - Setter(VariantType type, SetterCallback callback, void *data = nullptr) - : ValidatingFunction({Argument{type}}), - callback(callback), - data(data){}; - - Function *clone() const override { return new Setter(*this); } - - void call(Variant arg) const { ValidatingFunction::call({arg}); } - - void operator()(Variant arg) const { return call(arg); } - - bool exists() const { return callback != nullptr; } -}; -} -} - -#endif /* _OUSIA_FUNCTION_HPP_ */ - diff --git a/src/core/script/Object.cpp b/src/core/script/Object.cpp deleted file mode 100644 index 8fa9771..0000000 --- a/src/core/script/Object.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - 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 "Object.hpp" - -namespace ousia { -namespace script { - -bool Object::hasElement(const std::string &name) const -{ - return (properties.find(name) != properties.end()) || - (methods.find(name) != methods.end()); -} - -void Object::addProperty(const std::string &name, const Property &property) -{ - if (hasElement(name)) { - // TODO Throw another exception class here - throw "Element already exists"; - } - properties.insert(std::make_pair(name, property)); -} - -void Object::addProperty(const std::string &name, const Getter &get, const Setter &set) -{ - addProperty(name, Property{get, set}); -} - -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(const std::string &name, const Getter &get) -{ - addProperty(name, Property{get, Setter{VariantType::null, nullptr}}); -} - -void Object::addReadonlyProperty(const std::string &name, const GetterCallback get) -{ - addProperty( - name, Property{Getter{get, data}, Setter{VariantType::null, nullptr}}); -} - -void Object::addMethod(const std::string &name, const HostFunction &fun) -{ - if (hasElement(name)) { - // TODO Throw another exception class here - throw "Element already exists"; - } - methods.insert(std::make_pair(name, fun)); -} - -void Object::addMethod(const std::string &name, const HostFunctionCallback fun) -{ - addMethod(name, HostFunction{fun, data}); -} - -void Object::addMethod(const std::string &name, const HostFunctionCallback fun, - const std::vector &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; -} - -} -} - diff --git a/src/core/script/Object.hpp b/src/core/script/Object.hpp deleted file mode 100644 index 350f800..0000000 --- a/src/core/script/Object.hpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - 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 . -*/ - -#ifndef _OBJECT_HPP_ -#define _OBJECT_HPP_ - -#include -#include - -#include "Function.hpp" - -namespace ousia { -namespace script { - -// TODO: Check names for being proper identifiers! - -/** - * The Property struct represents an object property with corresponding getter - * and setter function. - */ -struct Property { - /** - * Constructor of the Property struct. Copies the given getter and setter. - * - * @param get is the getter that should be used for the property. - * @param set is the setter that should be used for the property. - */ - Property(const Getter &get, const Setter &set) : get(get), set(set) {}; - - /** - * Constructor of the Property struct. Creates new Getter and Setter - * instances from the given parameters. - * - * @param type is the VariantType used within the getter function. - * @param get is the pointer to the getter function. - * @param set is the pointer to the setter function. - * @param data is the used-defined data that should be used. - */ - Property(VariantType type, const GetterCallback get, - const SetterCallback set, void *data = nullptr) - : get(get, data), set(type, set, data) {}; - - /** - * Getter function. - */ - const Getter get; - - /** - * Setter function. - */ - const Setter set; -}; - -/** - * The Object type represents an object on the script host. An object consits of - * properties with corresponding getter and setter functions and a number of - * methods which can be called on the object. - */ -class Object { -private: - /** - * Pointer to user defined data that is automatically passed to the - * underlying functions. - */ - void *data; - - /** - * Map used internally for storing all properties along with their - * corresponding - * name. - */ - std::map properties; - - /** - * Map used internally for storing all methods along with their - * corresponding name. - */ - std::map methods; - -public: - Object() : data(nullptr){}; - - Object(void *data) : data(data){}; - - bool hasElement(const std::string &name) const; - - void addProperty(const std::string &name, const Property &property); - - void addProperty(const std::string &name, const Getter &get, const Setter &set); - - void addProperty(const std::string &name, VariantType type, - const GetterCallback get, const SetterCallback set); - - void addReadonlyProperty(const std::string &name, const Getter &get); - - void addReadonlyProperty(const std::string &name, const GetterCallback get); - - void addMethod(const std::string &name, const HostFunction &fun); - - void addMethod(const std::string &name, const HostFunctionCallback fun); - - void addMethod(const std::string &name, const HostFunctionCallback fun, - const std::vector &signature); - - const Property *getProperty(const std::string &name) const; - - const Function *getMethod(const std::string &name) const; - - bool removeElement(const std::string &name); - - bool removeProperty(const std::string &name); - - bool removeMethod(const std::string &name); - - const std::map &getProperties() const - { - return properties; - } - - const std::map &getMethods() const - { - return methods; - } -}; -} -} - -#endif /* _OBJECT_HPP_ */ - diff --git a/src/core/script/ScriptEngine.cpp b/src/core/script/ScriptEngine.cpp deleted file mode 100644 index 99f2d3f..0000000 --- a/src/core/script/ScriptEngine.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - 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 - -#include "ScriptEngine.hpp" - -namespace ousia { -namespace script { - -/* Class ScriptEngineException */ - -ScriptEngineException::ScriptEngineException(int line, int col, - const std::string &msg) - : line(line), - col(col), - msg(std::to_string(line) + ":" + std::to_string(col) + " " + msg) -{ -} - -ScriptEngineException::ScriptEngineException(const std::string &msg) - : line(-1), col(-1), msg(msg) -{ -} - -const char *ScriptEngineException::what() const noexcept { return msg.c_str(); } - -/* Class ScriptEngineFactory */ - -void ScriptEngineFactory::registerScriptEngine(const std::string &name, - ScriptEngine *engine) -{ - registry[name] = engine; -} - -bool ScriptEngineFactory::unregisterScriptEngine(const std::string &name) -{ - return registry.erase(name) > 0; -} - -ScriptEngineScope *ScriptEngineFactory::createScope( - const std::string &name) const -{ - auto it = registry.find(name); - if (it != registry.end()) { - return it->second->createScope(); - } - return nullptr; -} -} -} - diff --git a/src/core/script/ScriptEngine.hpp b/src/core/script/ScriptEngine.hpp deleted file mode 100644 index 3c61cb0..0000000 --- a/src/core/script/ScriptEngine.hpp +++ /dev/null @@ -1,223 +0,0 @@ -/* - 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 . -*/ - -#ifndef _OUSIA_SCRIPT_ENGINE_HPP_ -#define _OUSIA_SCRIPT_ENGINE_HPP_ - -#include -#include -#include - -#include - -#include "Variant.hpp" - -// TODO: Provide more Exception classes than ScriptEngineException -- one for -// internal errors, one for script errors - -// TODO: Allow reporting multiple exceptions (e.g. to report all syntax errors -// at once) - -// TODO: Add API that allow pre-compilation of scripts - -namespace ousia { -namespace script { - -/** - * Class used for signaling errors while executing code or registering variables - * in the script engine. - */ -class ScriptEngineException : public std::exception { -public: - /** - * Line and column at which the exception occured. Set to -1 if the error - * does not correspond to a line or column. - */ - const int line, col; - - /** - * Error message. - */ - const std::string msg; - - /** - * ScriptEngineException constructor. - * - * @param line in the script code at which the exception occured. - * @param col in the script code at which the exception occured. - * @param msg is the message containing the reason for the exception. - */ - ScriptEngineException(int line, int col, const std::string &msg); - - /** - * ScriptEngineException constructor. - * - * @param msg is the message containing the reason for the exception. - */ - ScriptEngineException(const std::string &msg); - - /** - * Returns the error message. - */ - virtual const char *what() const noexcept override; -}; - -/** - * The ScriptEngineScope class represents an execution scope -- an execution - * scope is the base class - */ -class ScriptEngineScope { -private: - /** - * Helper used to check the given identifiers for their validity. - * - * @param name is the name of the identifier that should be checked. - * @throws ScriptEngineException if the given identifier is invalid. - */ - static void checkIdentifier(const std::string &name) - { - if (!Utils::isIdentifier(name)) { - throw ScriptEngineException{"Invalid identifier \"" + name + "\""}; - } - } - -protected: - /** - * Implementation of the run function. - */ - virtual Variant doRun(const std::string &code) = 0; - - /** - * Implementation of the setVariable function. - */ - virtual void doSetVariable(const std::string &name, const Variant &val, - bool constant) = 0; - - /** - * Implementation of the getVariable function. - */ - virtual Variant doGetVariable(const std::string &name) = 0; - -public: - /** - * Virtual destructor. Must be overwritten by implementing classes. - */ - virtual ~ScriptEngineScope(){}; - - /** - * Runs the given code in the excution context. - * - * @param code is a string containing the code the script engine should run. - * @return a variant containg the result of the executed code. - * @throws ScriptEngineException if an error occured during code execution. - */ - Variant run(const std::string &code) { return doRun(code); } - - /** - * Sets the value of a variable in the scope with the given name. - * - * @param name is the name of the variable in the scope. Must be a - * well-formed identifier. - * @param val is the value of the variable. - * @param constant if true, the value of the variable cannot be changed by - * the script code. - * @throws ScriptEngineException if name is not a well-formed identifier. - */ - void setVariable(const std::string &name, const Variant &val, - bool constant = false) - { - checkIdentifier(name); - doSetVariable(name, val, constant); - } - - /** - * Reads the value of the variable with the given name. - * - * @param name is the name of the variable. The name must be well-formed. - * @return the value of the variable, or a NULL variant if the variable does - * not exist. - * @throws ScriptEngineException if name is not a well-formed identifier. - */ - Variant getVariable(const std::string &name) - { - checkIdentifier(name); - return doGetVariable(name); - } -}; - -/** - * The abstract ScriptEngine class is used to provide an interface for script - * engine implementations. A script engine implementation has to provide a - * function which creates an execution scope. - */ -class ScriptEngine { -public: - /** - * Requests an execution scope from the script engine implementation. The - * calling code is responsible for disposing the returned pointer. - */ - virtual ScriptEngineScope *createScope() = 0; -}; - -/** - * The ScriptEngineFactory class is a central registry for ScriptEngine - * instances and factory of ScriptEngineScope instances for a certain scripting - * language. - */ -class ScriptEngineFactory { -private: - /** - * Internal map between the script language name and the actual script - * engine instance. - */ - std::map registry; - -public: - /** - * Registers a ScriptEngine instance for a new scripting language. - * - * @param name is the name of the scripting language as MIME, e.g. - * "text/javascript" - * @param engine is the backend that should be registered. - */ - void registerScriptEngine(const std::string &name, ScriptEngine *engine); - - /** - * Removes a script engine from the registry. - * - * @param name is the name of the script engine that - */ - bool unregisterScriptEngine(const std::string &name); - - /** - * Creates an execution scope for the scripting language with the given - * name. - * - * @param name is the name of the scripting language for which the scope - * is being created. - * @return a pointer to the new execution scope or null if a script engine - * with the given name does not exist. The caller of this function is - * responsible - */ - ScriptEngineScope *createScope(const std::string &name) const; -}; -} -} - -#endif /* _OUSIA_SCRIPT_ENGINE_HPP_ */ - diff --git a/src/core/script/Variant.cpp b/src/core/script/Variant.cpp deleted file mode 100644 index bb9f566..0000000 --- a/src/core/script/Variant.cpp +++ /dev/null @@ -1,361 +0,0 @@ -/* - 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 "Variant.hpp" -#include "Function.hpp" -#include "Object.hpp" - -namespace ousia { -namespace script { - -/* Class Variant */ - -const Variant Variant::Null; - -Variant::Variant(const Variant &v) : type(v.type) -{ - switch (v.type) { - case VariantType::null: - break; - case VariantType::boolean: - booleanValue = v.booleanValue; - break; - case VariantType::integer: - integerValue = v.integerValue; - break; - case VariantType::number: - numberValue = v.numberValue; - break; - case VariantType::string: - objectValue = - new std::string(*static_cast(v.objectValue)); - break; - case VariantType::array: - objectValue = new std::vector( - *static_cast *>(v.objectValue)); - break; - case VariantType::map: - objectValue = new std::map( - *static_cast *>(v.objectValue)); - break; - case VariantType::function: - objectValue = static_cast(v.objectValue)->clone(); - break; - case VariantType::object: - objectValue = new Object(*static_cast(v.objectValue)); - break; - case VariantType::buffer: - // TODO - break; - } -} - -Variant::Variant(Variant &&v) : type(v.type) -{ - switch (type) { - case VariantType::null: - break; - case VariantType::boolean: - booleanValue = v.booleanValue; - break; - case VariantType::integer: - integerValue = v.integerValue; - break; - case VariantType::number: - numberValue = v.numberValue; - break; - case VariantType::string: - case VariantType::array: - case VariantType::map: - case VariantType::function: - case VariantType::object: - case VariantType::buffer: - objectValue = v.objectValue; - v.objectValue = nullptr; - break; - } -} - -Variant::Variant() : type(VariantType::null) -{ -} - -Variant::Variant(bool b) : type(VariantType::boolean), booleanValue(b) -{ -} - -Variant::Variant(int64_t i) : type(VariantType::integer), integerValue(i) -{ -} - -Variant::Variant(double d) : type(VariantType::number), numberValue(d) -{ -} - -Variant::Variant(const char *s) - : type(VariantType::string), objectValue(new std::string(s)) -{ -} - -Variant::Variant(const std::vector &a) - : type(VariantType::array), objectValue(new std::vector(a)) -{ -} - -Variant::Variant(const std::map &m) - : type(VariantType::map), objectValue(new std::map(m)) -{ -} - -Variant::Variant(const Function *f) - : type(VariantType::function), objectValue(f->clone()) -{ -} - -Variant::Variant(const Object &o) - : type(VariantType::object), objectValue(new Object(o)) -{ -} - -Variant::~Variant() -{ - switch (type) { - case VariantType::string: - delete static_cast(objectValue); - break; - case VariantType::array: - delete static_cast *>(objectValue); - break; - case VariantType::map: - delete static_cast *>(objectValue); - break; - case VariantType::function: - delete static_cast(objectValue); - break; - case VariantType::object: - delete static_cast(objectValue); - break; - default: - break; - } -} - -bool Variant::getBooleanValue() const -{ - switch (type) { - case VariantType::null: - return false; - case VariantType::boolean: - return booleanValue; - case VariantType::integer: - return integerValue != 0; - case VariantType::number: - return numberValue != 0.0; - case VariantType::string: - return !getStringValue().empty(); - case VariantType::array: - return !getArrayValue().empty(); - case VariantType::map: - return !getMapValue().empty(); - default: - throw VariantTypeException{VariantType::boolean, type}; - } -} - -int64_t Variant::getIntegerValue() const -{ - switch (type) { - case VariantType::boolean: - return booleanValue ? 1 : 0; - case VariantType::integer: - return integerValue; - case VariantType::number: - return static_cast(numberValue); - default: - throw VariantTypeException{VariantType::integer, type}; - } -} - -double Variant::getNumberValue() const -{ - switch (type) { - case VariantType::boolean: - return booleanValue ? 1.0 : 0.0; - case VariantType::integer: - return static_cast(integerValue); - case VariantType::number: - return numberValue; - default: - throw VariantTypeException{VariantType::number, type}; - } -} - -const std::string &Variant::getStringValue() const -{ - switch (type) { - case VariantType::string: - return *(static_cast(objectValue)); - default: - throw VariantTypeException{VariantType::string, type}; - } -} - -const std::vector &Variant::getArrayValue() const -{ - switch (type) { - case VariantType::array: - return *(static_cast *>(objectValue)); - default: - throw VariantTypeException{VariantType::array, type}; - } -} - -const std::map &Variant::getMapValue() const -{ - switch (type) { - case VariantType::map: - return *(static_cast *>( - objectValue)); - default: - throw VariantTypeException{VariantType::map, type}; - } -} - -const Function *Variant::getFunctionValue() const -{ - switch (type) { - case VariantType::function: return static_cast(objectValue); - default: - throw VariantTypeException{VariantType::function, type}; - } -} - -const Object &Variant::getObjectValue() const -{ - switch (type) { - case VariantType::object: return *(static_cast(objectValue)); - default: - throw VariantTypeException{VariantType::object, type}; - } -} - -const char *Variant::getTypeName(VariantType type) -{ - switch (type) { - case VariantType::null: - return "null"; - case VariantType::boolean: - return "boolean"; - case VariantType::integer: - return "integer"; - case VariantType::number: - return "number"; - case VariantType::string: - return "string"; - case VariantType::array: - return "array"; - case VariantType::map: - return "map"; - case VariantType::function: - return "function"; - case VariantType::object: - return "object"; - case VariantType::buffer: - return "buffer"; - } - return "unknown"; -} - -/* Class VariantTypeException */ - -VariantTypeException::VariantTypeException(VariantType actualType, - VariantType requestedType) - : msg(std::string("Cannot get value of variant of type \"") + - Variant::getTypeName(actualType) + std::string("\" as \"") + - Variant::getTypeName(requestedType) + std::string("\"")), - actualType(actualType), - requestedType(requestedType) -{ -} - -const char *VariantTypeException::what() const noexcept -{ - return msg.c_str(); -} - -/* Global scope operator */ - -std::ostream &operator<<(std::ostream &os, const Variant &v) -{ - switch (v.type) { - case VariantType::null: - os << "null"; - break; - case VariantType::boolean: - os << (v.booleanValue ? "true" : "false"); - break; - case VariantType::integer: - os << v.integerValue; - break; - case VariantType::number: - os << v.numberValue; - break; - case VariantType::string: - os << "\"" << v.getStringValue() << "\""; - break; - case VariantType::array: { - bool first = true; - os << "["; - for (auto &v2 : v.getArrayValue()) { - if (!first) { - os << ", "; - } - os << v2; - first = false; - } - os << "]"; - break; - } - case VariantType::map: { - bool first = true; - os << "{"; - for (auto &v2 : v.getMapValue()) { - if (!first) { - os << ", "; - } - os << "\"" << v2.first << "\": " << v2.second; - first = false; - } - os << "}"; - break; - } - case VariantType::function: - os << ""; - break; - case VariantType::object: - os << ""; - break; - case VariantType::buffer: - os << ""; - break; - } - return os; -} -} -} - diff --git a/src/core/script/Variant.hpp b/src/core/script/Variant.hpp deleted file mode 100644 index 848c595..0000000 --- a/src/core/script/Variant.hpp +++ /dev/null @@ -1,269 +0,0 @@ -/* - 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 . -*/ - -#ifndef _OUSIA_VARIANT_HPP_ -#define _OUSIA_VARIANT_HPP_ - -#include -#include -#include -#include -#include -#include - -// TODO: Replace VariantType::number with VariantType::double -// TODO: Convert VariantType::integer to int32_t -// TODO: Use std::unique_ptr for *Function -// TODO: Move semantic in complex constructors -// TODO: Delete default constructors/assignment operators in pretty much everything -// TODO: Remove implicit type conversions, but add explicit conversion function! - -namespace ousia { -namespace script { - -/* Class forward declarations to avoid cyclic dependencies in the header */ -class Object; -class Function; - -/** - * Enum containing the possible types a variant may have. - */ -enum class VariantType : int16_t { - null = 0x0001, - boolean = 0x0002, - integer = 0x0004, - number = 0x0008, - string = 0x0010, - array = 0x0020, - map = 0x0040, - function = 0x0080, - object = 0x0100, - buffer = 0x0200 -}; - -/** - * Exception thrown whenever a variant is accessed via a getter function that - * is not supported for the current variant type. - */ -class VariantTypeException : public std::exception { -private: - /** - * Internally used string holding the exception message. - */ - const std::string msg; - -public: - /** - * Contains the actual type of the variant. - */ - const VariantType actualType; - - /** - * Contains the requested type of the variant. - */ - const VariantType requestedType; - - /** - * Constructor of the VariantTypeException. - * - * @param actualType describes the actual type of the variant. - * @param requestedType describes the type in which the variant was - * requested. - */ - VariantTypeException(VariantType actualType, VariantType requestedType); - - /** - * Returns the error message of the exception. - * - * @return the error message as C string. - */ - virtual const char *what() const noexcept override; -}; - -/** - * Instances of the Variant class represent any kind of data that is exchanged - * between the host application and the script engine. Variants are immutable. - */ -class Variant { -private: - /** - * Used to store the actual type of the variant. - */ - const VariantType type; - - /** - * Anonymous union containing the possible value of the variant. - */ - union { - /** - * The boolean value. Only valid if type is VariantType::boolean. - */ - bool booleanValue; - /** - * The integer value. Only valid if type is VariantType::integer. - */ - int64_t integerValue; - /** - * The number value. Only valid if type is VariantType::double. - */ - double numberValue; - /** - * Pointer to the more complex data structures on the free store. Only - * valid if type is one of VariantType::string, VariantType::array, - * VariantType::map, VariantType::function, VariantType::object. - */ - void *objectValue = nullptr; - }; - -public: - - using Int = int32_t; - - /** - * Copy constructor of the Variant class. - * - * @param v is the Variant instance that should be cloned. - */ - Variant(const Variant &v); - - /** - * Move constructor of the Variant class. - * - * @param v is the reference to the Variant instance that should be moved, - * this instance is invalidated afterwards. - */ - Variant(Variant &&v); - - /** - * Default constructor. Type is set to VariantType:null. - */ - Variant(); - - /** - * Constructor for boolean values. - * - * @param b boolean value. - */ - Variant(bool b); - - /** - * Constructor for integer values. - * - * @param i integer value. - */ - Variant(int64_t i); - - /** - * Constructor for number values. - * - * @param d number (double) value. - */ - Variant(double d); - - /** - * Constructor for string values. The given string is copied and managed by - * the new Variant instance. - * - * @param s is a reference to a C-Style string used as string value. - */ - Variant(const char *s); - - /** - * Constructor for array values. The given array is copied and managed by - * the new Variant instance. - * - * @param a is a reference to the array - */ - Variant(const std::vector &a); - - /** - * Constructor for map values. The given map is copied and managed by the - *new - * Variant instance. - * - * @param m is a reference to the map. - */ - Variant(const std::map &m); - - /** - * Constructor for function values. The given pointer to the function object - *is cloned and managed by the new Variant instance. - * - * @param f is a reference to the function. - */ - Variant(const Function *f); - - /** - * Constructor for object values. The given Object is copied and managed by - * the new Variant instance. - * - * @param o is a reference to the object. - */ - Variant(const Object &o); - - /** - * Destructor of the Variant class. - */ - ~Variant(); - - /** - * Assign operator. - */ - Variant &operator=(const Variant &v) = delete; - - /** - * Move assign operator. - */ - Variant &operator=(Variant &&v) = delete; - - /** - * Returns the current type of the Variant. - * - * @return the current type of the Variant. - */ - VariantType getType() const { return type; } - - bool getBooleanValue() const; - int64_t getIntegerValue() const; - double getNumberValue() const; - const std::string &getStringValue() const; - const std::vector &getArrayValue() const; - const std::map &getMapValue() const; - const Function *getFunctionValue() const; - const Object &getObjectValue() const; - - /** - * Shorthand for a constant representing a "null" as a variant. - */ - static const Variant Null; - - /** - * Returns the name of the given variant type as C-style string. - */ - static const char *getTypeName(VariantType type); - - /** - * Prints the object as JSON to the output stream. - */ - friend std::ostream &operator<<(std::ostream &os, const Variant &v); -}; -} -} - -#endif /* _OUSIA_VARIANT_HPP_ */ - -- cgit v1.2.3