GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/map/properties.h Lines: 3 26 11.5 %
Date: 2017-11-29 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef RESOURCES_MAP_PROPERTIES_H
24
#define RESOURCES_MAP_PROPERTIES_H
25
26
#include "localconsts.h"
27
28
#include <map>
29
#include <sstream>
30
31
/**
32
 * A class holding a set of properties.
33
 */
34
class Properties notfinal
35
{
36
    public:
37
94
        Properties() :
38
188
            mProperties()
39
        {
40
        }
41
42
        A_DELETE_COPY(Properties)
43
44
        /**
45
         * Destructor.
46
         */
47
        virtual ~Properties()
48
188
        { }
49
50
        /**
51
         * Get a map property.
52
         *
53
         * @param name The name of the property.
54
         * @param def  Default value, empty string by default.
55
         * @return the value of the given property or the given default when it
56
         *         doesn't exist.
57
         */
58
        const std::string getProperty(const std::string &name,
59
                                      const std::string &def = "")
60
                                      const A_WARN_UNUSED
61
        {
62
            const PropertyMap::const_iterator i = mProperties.find(name);
63
            return (i != mProperties.end()) ? i->second : def;
64
        }
65
66
        /**
67
         * Gets a map property as a float.
68
         *
69
         * @param name The name of the property.
70
         * @param def  Default value, 0.0F by default.
71
         * @return the value of the given property or the given default when it
72
         *         doesn't exist.
73
         */
74
        float getFloatProperty(const std::string &name,
75
                               const float def = 0.0F) const A_WARN_UNUSED
76
        {
77
            const PropertyMap::const_iterator i = mProperties.find(name);
78
            float ret = def;
79
            if (i != mProperties.end())
80
            {
81
                std::stringstream ss;
82
                ss.str(i->second);
83
                ss >> ret;
84
            }
85
            return ret;
86
        }
87
88
        /**
89
         * Gets a map property as a boolean.
90
         *
91
         * @param name The name of the property.
92
         * @param def  Default value, false by default.
93
         * @return the value of the given property or the given default when it
94
         *         doesn't exist.
95
         */
96
        bool getBoolProperty(const std::string &name,
97
                             const bool def = false) const A_WARN_UNUSED
98
        {
99
            const PropertyMap::const_iterator i = mProperties.find(name);
100
            bool ret = def;
101
            if (i != mProperties.end())
102
            {
103
                if (i->second == "true")
104
                    ret = true;
105
                if (i->second == "false")
106
                    ret = false;
107
            }
108
            return ret;
109
        }
110
111
        /**
112
         * Returns whether a certain property is available.
113
         *
114
         * @param name The name of the property.
115
         * @return <code>true</code> when a property is defined,
116
         *         <code>false</code> otherwise.
117
         */
118
        bool hasProperty(const std::string &name) const A_WARN_UNUSED
119
        { return (mProperties.find(name) != mProperties.end()); }
120
121
        /**
122
         * Set a map property.
123
         *
124
         * @param name  The name of the property.
125
         * @param value The value of the property.
126
         */
127
        void setProperty(const std::string &name, const std::string &value)
128
        { mProperties[name] = value; }
129
130
131
    private:
132
        typedef std::map<std::string, std::string> PropertyMap;
133
        PropertyMap mProperties;
134
};
135
136
#endif  // RESOURCES_MAP_PROPERTIES_H