10 #include <type_traits> 11 #include <unordered_map> 14 #include <SFML/Graphics/Color.hpp> 22 using PropertyTypes = std::variant<int, float, std::string, bool, sf::Color>;
24 auto hexToColor(
const std::string& color_string) -> sf::Color;
30 auto hasProperty(
const std::string& name)
const -> bool;
33 auto getProperty(
const std::string& name)
const ->
const T&;
36 void addProperty(
const std::string& name,
const T& value);
38 void printProperties()
const;
41 void parseProperties(
const pugi::xml_node& xmlnode_props);
44 std::unordered_map<std::string, PropertyTypes> m_properties;
49 auto PropertiesContainer::getProperty(
const std::string& name)
const ->
const T& {
50 if (m_properties.count(name)) {
51 if (std::holds_alternative<T>(m_properties.at(name))) {
52 return std::get<T>(m_properties.at(name));
54 std::cout <<
"Error (bad_variant_access) : Property «" << name
55 <<
"» is not of type " <<
typeid(T).name()
56 <<
". You are requesting the wrong type." << std::endl;
59 std::cout <<
"Error : Property named «" << name <<
"» does not exist." << std::endl;
64 void PropertiesContainer::addProperty(
const std::string& name,
const T& value) {
65 if (std::is_same_v<int, T> || std::is_same_v<float, T> || std::is_same_v<bool, T> ||
66 std::is_same_v<std::string, T> || std::is_same_v<sf::Color, T>) {
67 m_properties[name] = value;
70 std::cout <<
"Error : Cannot add property of type " <<
typeid(T).name()
71 <<
". (Accepted types are int, float, bool, std::string, sf::Color)."<< std::endl;