Beatmup
listing.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <string>
21 #include <map>
22 #include <vector>
23 #include <ostream>
24 
25 #include "input_stream.h"
26 
27 namespace Beatmup {
28 
29  /**
30  Parser of simple YAML-like listings.
31  Listing consists of chapters. Chapters are ordered sequences of blocks of key-value pairs:
32  chapter1:
33  - key: value
34  arg: another value
35  - key: stuff # comments are like this
36  param: number
37  chapter2:
38  - stuff: stuff
39  */
40  class Listing {
41  class Parser;
42  public:
43  /**
44  Set of key-value pairs
45  */
46  class Block {
47  friend class Parser;
48 
49  private:
50  std::map<std::string, std::string> mapping;
52 
53  protected:
55  public:
56  inline Block(): lineNumber(-1) {}
57  inline Block(const std::map<std::string, std::string>& mapping): mapping(mapping), lineNumber(-1) {}
58 
59  /**
60  Prints out the block to an output stream.
61  \param[in,out] stream The output stream
62  */
63  void printOut(std::ostream& stream);
64 
65  /**
66  Retrieves a value by key.
67  If not found, an exception is thrown.
68  \param[in] key The key
69  */
70  std::string operator[](const std::string& key) const;
71 
72  /**
73  Returns `true` if a value is defined for a specific key in the block.
74  \param[in] key The key
75  */
76  inline bool has(const std::string& key) const { return mapping.find(key) != mapping.cend(); }
77 
78  /**
79  Returns a value by key casted to a given type.
80  An exception is thrown if no value defined for the given key.
81  \tparam T The value type
82  \param[in] key The key
83  */
84  template<typename T>
85  inline T get(const std::string& key) const;
86 
87  /**
88  Returns a value by key casted to a given type.
89  \tparam T The value type
90  \param[in] key The key
91  \param[in] defaultValue Default value returned if no value is defined for the key.
92  */
93  template<typename T>
94  inline T get(const std::string& key, const T defaultValue) const {
95  return has(key) ? get<T>(key) : defaultValue;
96  }
97 
98  /**
99  Sets a value for a specific key.
100  \tparam T The value type
101  \param[in] key The key
102  \param[in] value The new value
103  */
104  template<typename T>
105  void set(const std::string& key, T value);
106 
107  /**
108  Returns line number the block starts at.
109  */
110  inline int getLineNumber() const { return lineNumber; }
111  };
112 
113  private:
114  std::map<std::string, std::vector<Block>> chapters;
115 
116  public:
117  Listing() {}
118  Listing(InputStream& stream);
119  Listing(std::istream& stream);
120 
121  /**
122  Prints out the listing to an output stream.
123  Respects its own format when printing, i.e., the printed output may be reparsed into a listing.
124  \param[in,out] stream The output stream
125  */
126  void printOut(std::ostream& stream);
127 
128  /**
129  Returns `true` if a specific chapter is present in the listing.
130  \param[in] key The chapter name
131  */
132  inline bool has(const std::string& key) const { return chapters.find(key) != chapters.cend(); }
133 
134  /**
135  Retrieves a chapter in the listing by its name.
136  If not found, an exception is thrown.
137  \param[in] key The chapter name
138  */
139  const std::vector<Block>& operator[](const std::string& key) const;
140 
141  /**
142  Adds a block to a chapter.
143  \param[in] key The chapter name
144  \param[in] block The block
145  */
146  void emplace(const std::string& key, Block&& block);
147  };
148 
149  template<>
150  inline std::string Listing::Block::get(const std::string& key) const {
151  return (*this)[key];
152  }
153 
154  template<>
155  inline int Listing::Block::get(const std::string& key) const {
156  return std::atoi((*this)[key].c_str());
157  }
158 
159  template<>
160  inline float Listing::Block::get(const std::string& key) const {
161  return std::atof((*this)[key].c_str());
162  }
163 
164  template<>
165  inline bool Listing::Block::get(const std::string& key) const {
166  return (*this)[key] == "true";
167  }
168 
169  template<>
170  inline void Listing::Block::set(const std::string& key, std::string value) {
171  mapping[key] = value;
172  }
173 
174  template<>
175  inline void Listing::Block::set(const std::string& key, float value) {
176  mapping[key] = std::to_string(value);
177  }
178 
179  template<>
180  inline void Listing::Block::set(const std::string& key, int value) {
181  mapping[key] = std::to_string(value);
182  }
183 }
Minimal input stream interface.
Definition: input_stream.h:27
Set of key-value pairs.
Definition: listing.h:46
T get(const std::string &key) const
Returns a value by key casted to a given type.
T get(const std::string &key, const T defaultValue) const
Returns a value by key casted to a given type.
Definition: listing.h:94
Block(int lineNumber)
Definition: listing.h:54
void set(const std::string &key, T value)
Sets a value for a specific key.
std::map< std::string, std::string > mapping
Definition: listing.h:50
void printOut(std::ostream &stream)
Prints out the block to an output stream.
Definition: listing.cpp:152
int getLineNumber() const
Returns line number the block starts at.
Definition: listing.h:110
Block(const std::map< std::string, std::string > &mapping)
Definition: listing.h:57
std::string operator[](const std::string &key) const
Retrieves a value by key.
Definition: listing.cpp:161
bool has(const std::string &key) const
Returns true if a value is defined for a specific key in the block.
Definition: listing.h:76
Parser of simple YAML-like listings.
Definition: listing.h:40
const std::vector< Block > & operator[](const std::string &key) const
Retrieves a chapter in the listing by its name.
Definition: listing.cpp:192
bool has(const std::string &key) const
Returns true if a specific chapter is present in the listing.
Definition: listing.h:132
void printOut(std::ostream &stream)
Prints out the listing to an output stream.
Definition: listing.cpp:184
std::map< std::string, std::vector< Block > > chapters
Definition: listing.h:114
void emplace(const std::string &key, Block &&block)
Adds a block to a chapter.
Definition: listing.cpp:200
std::string to_string(Beatmup::NNets::ActivationFunction function)
Beatmup::AffineMapping & mapping