Browse Source

Move parameter dict to emergent

master
C. J. Howard 5 years ago
parent
commit
e3b9d143ce
Signed by: cjhoward GPG Key ID: 03E1FABA9C3EC195
5 changed files with 3 additions and 338 deletions
  1. +0
    -1
      src/game.cpp
  2. +0
    -1
      src/game.hpp
  3. +0
    -166
      src/parameter-dict.cpp
  4. +0
    -169
      src/parameter-dict.hpp
  5. +3
    -1
      src/resources/parameter-dict-loader.cpp

+ 0
- 1
src/game.cpp View File

@ -55,7 +55,6 @@
#include "entity/systems/particle-system.hpp" #include "entity/systems/particle-system.hpp"
#include "entity/systems/terrain-system.hpp" #include "entity/systems/terrain-system.hpp"
#include "configuration.hpp" #include "configuration.hpp"
#include "parameter-dict.hpp"
#include "stb/stb_image_write.h" #include "stb/stb_image_write.h"
#include "menu.hpp" #include "menu.hpp"
#include <algorithm> #include <algorithm>

+ 0
- 1
src/game.hpp View File

@ -74,7 +74,6 @@ class Menu;
class MenuItem; class MenuItem;
class CommandInterpreter; class CommandInterpreter;
class Logger; class Logger;
class ParameterDict;
enum class ComponentType; enum class ComponentType;
class Game: class Game:

+ 0
- 166
src/parameter-dict.cpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "parameter-dict.hpp"
#include <sstream>
void ParameterDict::unset(const std::string& name)
{
auto it = parameters.find(name);
if (it != parameters.end())
{
parameters.erase(it);
}
}
template <>
std::string ParameterDict::toString<std::string>(const std::string& value)
{
return value;
}
template <>
std::string ParameterDict::toString<bool>(const bool& value)
{
return std::to_string(static_cast<int>(value));
}
template <>
std::string ParameterDict::toString<char>(const char& value)
{
return std::to_string(static_cast<int>(value));
}
template <>
std::string ParameterDict::toString<unsigned char>(const unsigned char& value)
{
return std::to_string(static_cast<int>(value));
}
template <>
std::string ParameterDict::toString<int>(const int& value)
{
return std::to_string(value);
}
template <>
std::string ParameterDict::toString<unsigned int>(const unsigned int& value)
{
return std::to_string(value);
}
template <>
std::string ParameterDict::toString<long>(const long& value)
{
return std::to_string(value);
}
template <>
std::string ParameterDict::toString<unsigned long>(const unsigned long& value)
{
return std::to_string(value);
}
template <>
std::string ParameterDict::toString<float>(const float& value)
{
return std::to_string(value);
}
template <>
std::string ParameterDict::toString<double>(const double& value)
{
return std::to_string(value);
}
template<>
std::string ParameterDict::fromString<std::string>(const std::string& string)
{
return string;
}
template<>
bool ParameterDict::fromString<bool>(const std::string& string)
{
return static_cast<bool>(std::stoi(string));
}
template<>
char ParameterDict::fromString<char>(const std::string& string)
{
return static_cast<char>(std::stoi(string));
}
template<>
unsigned char ParameterDict::fromString<unsigned char>(const std::string& string)
{
return static_cast<unsigned char>(std::stoi(string));
}
template<>
int ParameterDict::fromString<int>(const std::string& string)
{
return std::stoi(string);
}
template<>
unsigned int ParameterDict::fromString<unsigned int>(const std::string& string)
{
return static_cast<unsigned int>(std::stoul(string));
}
template<>
long ParameterDict::fromString<long>(const std::string& string)
{
return std::stol(string);
}
template<>
unsigned long ParameterDict::fromString<unsigned long>(const std::string& string)
{
return std::stoul(string);
}
template<>
float ParameterDict::fromString<float>(const std::string& string)
{
return std::stof(string);
}
template<>
double ParameterDict::fromString<double>(const std::string& string)
{
return std::stod(string);
}
std::vector<std::string> ParameterDict::tokenize(const std::string& string)
{
std::istringstream stream(string);
std::string token;
std::vector<std::string> tokens;
const char delimeter = ' ';
while (std::getline(stream, token, delimeter))
{
tokens.push_back(token);
}
return tokens;
}

+ 0
- 169
src/parameter-dict.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef PARAMETER_DICT_HPP
#define PARAMETER_DICT_HPP
#include <array>
#include <map>
#include <optional>
#include <string>
#include <vector>
/**
* 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<class T>
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 <class T, std::size_t N>
void set(const std::string& name, const std::array<T, N>& 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<class T>
std::optional<T> 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 <class T, std::size_t N>
std::optional<std::array<T, N>> get(const std::string& name);
/// Returns all parameters in the dict.
const std::map<std::string, std::string>* getParameters() const;
private:
/// Converts a value to a string.
template <class T>
static std::string toString(const T& value);
/// Converts a string to a value.
template <class T>
static T fromString(const std::string& string);
/// Splits a string into space-delimeted tokens
static std::vector<std::string> tokenize(const std::string& string);
std::map<std::string, std::string> parameters;
};
template <class T>
void ParameterDict::set(const std::string& name, const T& value)
{
parameters[name] = toString<T>(value);
}
template <class T, std::size_t N>
void ParameterDict::set(const std::string& name, const std::array<T, N>& values)
{
std::string string;
for (std::size_t i = 0; i < N; ++i)
{
string += toString<T>(values[i]);
if (i < N - 1)
{
string += ' ';
}
}
parameters[name] = string;
}
template <class T>
std::optional<T> ParameterDict::get(const std::string& name)
{
auto it = parameters.find(name);
if (it == parameters.end())
{
return std::nullopt;
}
return fromString<T>(it->second);
}
template <class T, std::size_t N>
std::optional<std::array<T, N>> 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<T, N> values;
// Tokenize string
std::vector<std::string> 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<T>(tokens[i]);
}
return values;
}
inline const std::map<std::string, std::string>* ParameterDict::getParameters() const
{
return &parameters;
}
#endif // PARAMETER_DICT_HPP

+ 3
- 1
src/resources/parameter-dict-loader.cpp View File

@ -20,10 +20,12 @@
#include "resource-loader.hpp" #include "resource-loader.hpp"
#include "resource-manager.hpp" #include "resource-manager.hpp"
#include "string-table.hpp" #include "string-table.hpp"
#include "parameter-dict.hpp"
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <emergent/emergent.hpp>
using namespace Emergent;
template <> template <>
ParameterDict* ResourceLoader<ParameterDict>::load(ResourceManager* resourceManager, std::istream* is) ParameterDict* ResourceLoader<ParameterDict>::load(ResourceManager* resourceManager, std::istream* is)
{ {

Loading…
Cancel
Save