From e3b9d143ce4544b162c7f8b005ae32b712701f90 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Fri, 29 Mar 2019 22:07:41 +0800 Subject: [PATCH] Move parameter dict to emergent --- src/game.cpp | 1 - src/game.hpp | 1 - src/parameter-dict.cpp | 166 ----------------------- src/parameter-dict.hpp | 169 ------------------------ src/resources/parameter-dict-loader.cpp | 4 +- 5 files changed, 3 insertions(+), 338 deletions(-) delete mode 100644 src/parameter-dict.cpp delete mode 100644 src/parameter-dict.hpp diff --git a/src/game.cpp b/src/game.cpp index 87a4588..e3f288d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -55,7 +55,6 @@ #include "entity/systems/particle-system.hpp" #include "entity/systems/terrain-system.hpp" #include "configuration.hpp" -#include "parameter-dict.hpp" #include "stb/stb_image_write.h" #include "menu.hpp" #include diff --git a/src/game.hpp b/src/game.hpp index ba5282a..bdb6571 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -74,7 +74,6 @@ class Menu; class MenuItem; class CommandInterpreter; class Logger; -class ParameterDict; enum class ComponentType; class Game: diff --git a/src/parameter-dict.cpp b/src/parameter-dict.cpp deleted file mode 100644 index 53f0310..0000000 --- a/src/parameter-dict.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2017-2019 Christopher J. Howard - * - * This file is part of Antkeeper Source Code. - * - * Antkeeper Source Code 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. - * - * Antkeeper Source Code 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 Antkeeper Source Code. If not, see . - */ - -#include "parameter-dict.hpp" -#include - -void ParameterDict::unset(const std::string& name) -{ - auto it = parameters.find(name); - if (it != parameters.end()) - { - parameters.erase(it); - } -} - -template <> -std::string ParameterDict::toString(const std::string& value) -{ - return value; -} - -template <> -std::string ParameterDict::toString(const bool& value) -{ - return std::to_string(static_cast(value)); -} - -template <> -std::string ParameterDict::toString(const char& value) -{ - return std::to_string(static_cast(value)); -} - -template <> -std::string ParameterDict::toString(const unsigned char& value) -{ - return std::to_string(static_cast(value)); -} - -template <> -std::string ParameterDict::toString(const int& value) -{ - return std::to_string(value); -} - -template <> -std::string ParameterDict::toString(const unsigned int& value) -{ - return std::to_string(value); -} - -template <> -std::string ParameterDict::toString(const long& value) -{ - return std::to_string(value); -} - -template <> -std::string ParameterDict::toString(const unsigned long& value) -{ - return std::to_string(value); -} - -template <> -std::string ParameterDict::toString(const float& value) -{ - return std::to_string(value); -} - -template <> -std::string ParameterDict::toString(const double& value) -{ - return std::to_string(value); -} - -template<> -std::string ParameterDict::fromString(const std::string& string) -{ - return string; -} - -template<> -bool ParameterDict::fromString(const std::string& string) -{ - return static_cast(std::stoi(string)); -} - -template<> -char ParameterDict::fromString(const std::string& string) -{ - return static_cast(std::stoi(string)); -} - -template<> -unsigned char ParameterDict::fromString(const std::string& string) -{ - return static_cast(std::stoi(string)); -} - -template<> -int ParameterDict::fromString(const std::string& string) -{ - return std::stoi(string); -} - -template<> -unsigned int ParameterDict::fromString(const std::string& string) -{ - return static_cast(std::stoul(string)); -} - -template<> -long ParameterDict::fromString(const std::string& string) -{ - return std::stol(string); -} - -template<> -unsigned long ParameterDict::fromString(const std::string& string) -{ - return std::stoul(string); -} - -template<> -float ParameterDict::fromString(const std::string& string) -{ - return std::stof(string); -} - -template<> -double ParameterDict::fromString(const std::string& string) -{ - return std::stod(string); -} - -std::vector ParameterDict::tokenize(const std::string& string) -{ - std::istringstream stream(string); - std::string token; - std::vector tokens; - const char delimeter = ' '; - - while (std::getline(stream, token, delimeter)) - { - tokens.push_back(token); - } - - return tokens; -} - diff --git a/src/parameter-dict.hpp b/src/parameter-dict.hpp deleted file mode 100644 index c3518da..0000000 --- a/src/parameter-dict.hpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2017-2019 Christopher J. Howard - * - * This file is part of Antkeeper Source Code. - * - * Antkeeper Source Code 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. - * - * Antkeeper Source Code 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 Antkeeper Source Code. If not, see . - */ - -#ifndef PARAMETER_DICT_HPP -#define PARAMETER_DICT_HPP - -#include -#include -#include -#include -#include - -/** - * A dictionary of parameters. Parameter values are stored as strings. - */ -class ParameterDict -{ -public: - /** - * Sets the value of a parameter. This value will be stored as a string. - * - * @tparam T Parameter value type. - * @param name Name of the parameter. - * @param value Value of the parameter. - */ - template - void set(const std::string& name, const T& value); - - /** - * Sets the values of an array parameter. These values will be stored as a string. - * - * @tparam T Array element type. - * @tparam N Size of the array. - * @param name Name of the parameter. - * @param values Array of values. - */ - template - void set(const std::string& name, const std::array& values); - - /** - * Removes a parameter from the parameter dict. - * - * @param name Name of the parameter to be removed. - */ - void unset(const std::string& name); - - /** - * Returns the value of a parameter. If the parameter has not been set, std::nullopt will be returned. - * - * @tparam T Parameter value type. - * @param name Name of the parameter. - */ - template - std::optional get(const std::string& name); - - /** - * Returns the values of an array parameter. If the parameter has not been set, std::nyllopt will be returned. - * - * @tparam T Array element type. - * @tparam N Size of the array. - * @param name Name of the parameter. - */ - template - std::optional> get(const std::string& name); - - /// Returns all parameters in the dict. - const std::map* getParameters() const; - -private: - /// Converts a value to a string. - template - static std::string toString(const T& value); - - /// Converts a string to a value. - template - static T fromString(const std::string& string); - - /// Splits a string into space-delimeted tokens - static std::vector tokenize(const std::string& string); - - std::map parameters; -}; - -template -void ParameterDict::set(const std::string& name, const T& value) -{ - parameters[name] = toString(value); -} - -template -void ParameterDict::set(const std::string& name, const std::array& values) -{ - std::string string; - for (std::size_t i = 0; i < N; ++i) - { - string += toString(values[i]); - - if (i < N - 1) - { - string += ' '; - } - } - - parameters[name] = string; -} - -template -std::optional ParameterDict::get(const std::string& name) -{ - auto it = parameters.find(name); - if (it == parameters.end()) - { - return std::nullopt; - } - - return fromString(it->second); -} - -template -std::optional> ParameterDict::get(const std::string& name) -{ - auto it = parameters.find(name); - if (it == parameters.end()) - { - return std::nullopt; - } - - // Create array to hold values - std::array values; - - // Tokenize string - std::vector tokens = tokenize(it->second); - for (std::size_t i = 0; i < tokens.size(); ++i) - { - if (i >= values.size()) - { - break; - } - - // Convert token to value - values[i] = fromString(tokens[i]); - } - - return values; -} - -inline const std::map* ParameterDict::getParameters() const -{ - return ¶meters; -} - -#endif // PARAMETER_DICT_HPP - diff --git a/src/resources/parameter-dict-loader.cpp b/src/resources/parameter-dict-loader.cpp index 03ad262..3775ad5 100644 --- a/src/resources/parameter-dict-loader.cpp +++ b/src/resources/parameter-dict-loader.cpp @@ -20,10 +20,12 @@ #include "resource-loader.hpp" #include "resource-manager.hpp" #include "string-table.hpp" -#include "parameter-dict.hpp" #include #include +#include +using namespace Emergent; + template <> ParameterDict* ResourceLoader::load(ResourceManager* resourceManager, std::istream* is) {