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 <cstdint>
21 #include <optional>
22 
23 #include "util/reader_iterator.hpp"
24 
25 namespace sexp {
26 class Value;
27 } // namespace sexp
28 
29 class ReaderDocument;
30 class ReaderCollection;
31 
32 class ReaderMapping final
33 {
34 public:
35  static bool s_translations_enabled;
36 
37 public:
38  // sx should point to (section (name value)...)
39  ReaderMapping(const ReaderDocument& doc, const sexp::Value& sx);
40 
41  ReaderIterator get_iter() const;
42 
43  bool get(const char* key, bool& value, const std::optional<bool>& default_value = std::nullopt) const;
44  bool get(const char* key, int& value, const std::optional<int>& default_value = std::nullopt) const;
45  bool get(const char* key, uint32_t& value, const std::optional<uint32_t>& default_value = std::nullopt) const;
46  bool get(const char* key, float& value, const std::optional<float>& default_value = std::nullopt) const;
47  bool get(const char* key, std::string& value, const std::optional<const char*>& default_value = std::nullopt) const;
48 
49  bool get(const char* key, std::vector<bool>& value, const std::optional<std::vector<bool>>& default_value = std::nullopt) const;
50  bool get(const char* key, std::vector<int>& value, const std::optional<std::vector<int>>& default_value = std::nullopt) const;
51  bool get(const char* key, std::vector<float>& value, const std::optional<std::vector<float>>& default_value = std::nullopt) const;
52  bool get(const char* key, std::vector<std::string>& value, const std::optional<std::vector<std::string>>& default_value = std::nullopt) const;
53  bool get(const char* key, std::vector<unsigned int>& value, const std::optional<std::vector<unsigned int>>& default_value = std::nullopt) const;
54 
55  bool get(const char* key, std::optional<ReaderMapping>&) const;
56  bool get(const char* key, std::optional<ReaderCollection>&) const;
57 
58  bool get(const char* key, sexp::Value& value) const;
59 
65  template<typename C, typename F>
66  bool get_custom(const char* key, C& value, F from_string, std::optional<decltype(C())> default_value = std::nullopt) const
67  {
68  std::string text;
69  if (!get(key, text))
70  {
71  if (default_value) {
72  value = *default_value;
73  }
74  return false;
75  }
76  else
77  {
78  value = from_string(text);
79  return true;
80  }
81  }
82 
83  const sexp::Value& get_sexp() const { return m_sx; }
84  const ReaderDocument& get_doc() const { return m_doc; }
85 
86 private:
88  const sexp::Value* get_item(const char* key) const;
89 
90 private:
91  const ReaderDocument& m_doc;
92  const sexp::Value& m_sx;
93  const std::vector<sexp::Value>& m_arr;
94 };
95 
96 #endif
97 
98 /* EOF */
bool get_custom(const char *key, C &value, F from_string, std::optional< decltype(C())> default_value=std::nullopt) const
Read a custom data format, such an as enum.
Definition: reader_mapping.hpp:66
The ReaderIterator class is for backward compatibilty with old fileformats only, do not use it in new...
Definition: reader_iterator.hpp:33
Definition: object_option.hpp:39
Definition: reader_mapping.hpp:32
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