1// SuperTux
2// Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#ifndef HEADER_SUPERTUX_UTIL_READER_MAPPING_HPP
18#define HEADER_SUPERTUX_UTIL_READER_MAPPING_HPP
19
20#include <boost/optional.hpp>
21
22#include "util/reader_iterator.hpp"
23
24namespace sexp {
25class Value;
26} // namespace sexp
27
28class ReaderDocument;
29class ReaderCollection;
30
31class ReaderMapping final
32{
33public:
34 static bool s_translations_enabled;
35
36public:
37 // sx should point to (section (name value)...)
38 ReaderMapping(const ReaderDocument& doc, const sexp::Value& sx);
39
40 ReaderIterator get_iter() const;
41
42 bool get(const char* key, bool& value, const boost::optional<bool>& default_value = boost::none) const;
43 bool get(const char* key, int& value, const boost::optional<int>& default_value = boost::none) const;
44 bool get(const char* key, uint32_t& value, const boost::optional<uint32_t>& default_value = boost::none) const;
45 bool get(const char* key, float& value, const boost::optional<float>& default_value = boost::none) const;
46 bool get(const char* key, std::string& value, const boost::optional<const char*>& default_value = boost::none) const;
47
48 bool get(const char* key, std::vector<bool>& value) const;
49 bool get(const char* key, std::vector<int>& value) const;
50 bool get(const char* key, std::vector<float>& value) const;
51 bool get(const char* key, std::vector<std::string>& value) const;
52 bool get(const char* key, std::vector<unsigned int>& value) const;
53
54 bool get(const char* key, boost::optional<ReaderMapping>&) const;
55 bool get(const char* key, boost::optional<ReaderCollection>&) const;
56
57 bool get(const char* key, sexp::Value& value) const;
58
59 /** Read a custom data format, such an as enum. The data is stored
60 as string and converted to the custom type using the supplied
61 `from_string` convert function. Example:
62
63 mapping.get_custom("style", value, Style_from_string, Style::DEFAULT); */
64 template<typename C, typename F>
65 bool get_custom(const char* key, C& value, F from_string, boost::optional<decltype(C())> default_value = boost::none) const
66 {
67 std::string text;
68 if (!get(key, text))
69 {
70 if (default_value) {
71 value = *default_value;
72 }
73 return false;
74 }
75 else
76 {
77 value = from_string(text);
78 return true;
79 }
80 }
81
82 const sexp::Value& get_sexp() const { return m_sx; }
83 const ReaderDocument& get_doc() const { return m_doc; }
84
85private:
86 /** Returns pointer to (key value) */
87 const sexp::Value* get_item(const char* key) const;
88
89private:
90 const ReaderDocument& m_doc;
91 const sexp::Value& m_sx;
92 const std::vector<sexp::Value>& m_arr;
93};
94
95#endif
96
97/* EOF */
98