supertux
reader_mapping.hpp
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 
24 namespace sexp {
25 class Value;
26 } // namespace sexp
27 
28 class ReaderDocument;
29 class ReaderCollection;
30 
31 class ReaderMapping final
32 {
33 public:
34  static bool s_translations_enabled;
35 
36 public:
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 
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 
85 private:
87  const sexp::Value* get_item(const char* key) const;
88 
89 private:
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 */
The ReaderIterator class is for backward compatibilty with old fileformats only, do not use it in new...
Definition: reader_iterator.hpp:33
bool get_custom(const char *key, C &value, F from_string, boost::optional< decltype(C())> default_value=boost::none) const
Read a custom data format, such an as enum.
Definition: reader_mapping.hpp:65
Definition: object_option.hpp:38
Definition: reader_mapping.hpp:31
The ReaderDocument holds a parsed document in memory, access to it&#39;s content is provided by get_root(...
Definition: reader_document.hpp:27
Definition: reader_collection.hpp:30