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