Mana
Loading...
Searching...
No Matches
properties.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <map>
25#include <sstream>
26#include <string>
27
32{
33 public:
34 virtual ~Properties() = default;
35
44 std::string getProperty(const std::string &name,
45 const std::string &def = std::string()) const
46 {
47 auto i = mProperties.find(name);
48 return (i != mProperties.end()) ? i->second : def;
49 }
50
59 float getFloatProperty(const std::string &name, float def = 0.0f) const
60 {
61 auto i = mProperties.find(name);
62 float ret = def;
63 if (i != mProperties.end())
64 {
65 std::stringstream ss;
66 ss.str(i->second);
67 ss >> ret;
68 }
69 return ret;
70 }
71
80 float getIntProperty(const std::string &name, int def = 0) const
81 {
82 auto i = mProperties.find(name);
83 int ret = def;
84 if (i != mProperties.end())
85 {
86 std::stringstream ss;
87 ss.str(i->second);
88 ss >> ret;
89 }
90 return ret;
91 }
92
101 bool getBoolProperty(const std::string &name, bool def = false) const
102 {
103 auto i = mProperties.find(name);
104 bool ret = def;
105 if (i != mProperties.end())
106 {
107 if (i->second == "true")
108 ret = true;
109 if (i->second == "false")
110 ret = false;
111 }
112 return ret;
113 }
114
122 bool hasProperty(const std::string &name) const
123 {
124 return (mProperties.find(name) != mProperties.end());
125 }
126
133 void setProperty(const std::string &name, const std::string &value)
134 {
135 mProperties[name] = value;
136 }
137
138 private:
139 using PropertyMap = std::map<std::string, std::string>;
141};
A class holding a set of properties.
Definition properties.h:32
void setProperty(const std::string &name, const std::string &value)
Set a map property.
Definition properties.h:133
std::map< std::string, std::string > PropertyMap
Definition properties.h:139
std::string getProperty(const std::string &name, const std::string &def=std::string()) const
Get a map property.
Definition properties.h:44
bool hasProperty(const std::string &name) const
Returns whether a certain property is available.
Definition properties.h:122
virtual ~Properties()=default
float getIntProperty(const std::string &name, int def=0) const
Gets a map property as an int.
Definition properties.h:80
PropertyMap mProperties
Definition properties.h:140
float getFloatProperty(const std::string &name, float def=0.0f) const
Gets a map property as a float.
Definition properties.h:59
bool getBoolProperty(const std::string &name, bool def=false) const
Gets a map property as a boolean.
Definition properties.h:101