ge211
ge211_error.h
1 #pragma once
2 
3 #include "ge211_forward.h"
4 
5 #include <exception>
6 #include <memory>
7 #include <sstream>
8 #include <string>
9 
10 namespace ge211 {
11 
13 namespace exceptions {
14 
22 class Exception_base : public std::exception
23 {
24 public:
29  const char* what() const noexcept override;
30 
31 private:
32  explicit Exception_base(const std::string& message);
33 
35  friend Client_logic_error;
36  friend Environment_error;
37 
38  std::shared_ptr<const std::string> message_;
39 };
40 
47 {
48 public:
50  explicit Client_logic_error(const std::string& message);
51 };
52 
59 {
60  explicit Environment_error(const std::string& message);
61 
63  friend Window;
64 
66  friend Ge211_logic_error;
67  friend Host_error;
68 };
69 
74 {
75  explicit Ge211_logic_error(const std::string& message);
76 
78  friend detail::Render_sprite;
79  friend Mixer;
80  friend Text_sprite;
81 };
82 
88 {
89  explicit Host_error(const std::string& extra_message = "");
90 
92  friend File_error;
93  friend Font_error;
94  friend Image_error;
95  friend Mixer_error;
96 
98  friend Text_sprite;
99  friend Window;
100  friend detail::Renderer;
101  friend detail::Render_sprite;
102  friend detail::Texture;
103 };
104 
106 class File_error final : public Host_error
107 {
108  explicit File_error(const std::string& message);
109  static File_error could_not_open(const std::string& filename);
110 
112  friend detail::File_resource;
113 };
114 
116 class Font_error final : public Host_error
117 {
118  explicit Font_error(const std::string& message);
119  static Font_error could_not_load(const std::string& filename);
120 
122  friend Font;
123 };
124 
126 class Image_error final : public Host_error
127 {
128  explicit Image_error(const std::string& message);
129  static Image_error could_not_load(const std::string& filename);
130 
132  friend Image_sprite;
133 };
134 
137 class Mixer_error : public Host_error
138 {
139  Mixer_error(const std::string& message);
140  static Mixer_error could_not_load(const std::string& filename);
141  static Mixer_error out_of_channels();
142 
144  friend Mixer;
145  friend Music_track;
146  friend Sound_effect;
147 };
148 
149 } // end namespace exception
150 
151 namespace detail {
152 
153 enum class Log_level
154 {
155  debug,
156  info,
157  warn,
158  fatal,
159 };
160 
161 // Right now a Logger just keeps track of the current log
162 // level, and there's only one Logger (Singleton Pattern).
163 class Logger
164 {
165 public:
166  using Level = Log_level;
167 
168  Level level() const noexcept { return level_; }
169  void level(Level level) noexcept { level_ = level; }
170 
171  static Logger& instance() noexcept;
172 
173 private:
174  Logger() noexcept = default;
175 
176  Level level_ = Level::warn;
177 };
178 
179 // A Log_message accumulate information and then prints it all at
180 // once when it's going to be destroyed.
181 class Log_message
182 {
183 public:
184  using Level = Log_level;
185 
186  explicit Log_message(Level level = Level::debug);
187  explicit Log_message(std::string reason,
188  Level level = Level::debug) noexcept;
189 
190  template <class T>
191  Log_message& operator<<(const T& value)
192  {
193  if (active_) message_ << value;
194  return *this;
195  }
196 
197  // A Log_message has important work to do when it's destroyed.
198  virtual ~Log_message();
199 
200  // A Log_message should not be copied.
201  Log_message(const Log_message&) = delete;
202  Log_message& operator=(const Log_message&) = delete;
203 
204  // But it can be moved.
205  Log_message(Log_message&&) = default;
206  Log_message& operator=(Log_message&&) = default;
207 
208 private:
209  std::string reason_;
210  std::ostringstream message_;
211  bool active_;
212 };
213 
214 Log_message debug(std::string reason = "");
215 Log_message info(std::string reason = "");
216 Log_message warn(std::string reason = "");
217 Log_message fatal(std::string reason = "");
218 
219 Log_message info_sdl();
220 Log_message warn_sdl();
221 Log_message fatal_sdl();
222 
223 } // end namespace detail
224 
225 }
Indicates an error in the mixer, which could include the inability to understand an audio file format...
Definition: ge211_error.h:137
A Sprite that displays a bitmap image.
Indicates an error loading an image from an already-open file.
Definition: ge211_error.h:126
The game engine namespace.
Definition: ge211.h:17
A music track, which can be attached to the Mixer and played.
Definition: ge211_audio.h:34
A sound effect track, which can be attached to a Mixer channel and played.
Definition: ge211_audio.h:76
Represents a font that can be used to render a sprites::Text_sprite.
std::ostream & operator<<(std::ostream &os, Mouse_button button)
Prints a Mouse_button on a std::ostream.
Definition: ge211_event.cpp:85
Client_logic_error(const std::string &message)
Constructs the error, given the provided error message.
Definition: ge211_error.cpp:26
Indicates an exception from the host environment being passed along by ge211.
Definition: ge211_error.h:87
Indicates a condition unexpected by ge211, that appears to break its invariants.
Definition: ge211_error.h:73
Provides access to the game window and its properties.
Definition: ge211_window.h:12
The entity that coordinates playing all audio tracks.
Definition: ge211_audio.h:142
A Sprite that displays text.
An exception that indicates that a logic error was performed by the client.
Definition: ge211_error.h:46
The root of the ge211 exception hierarchy.
Definition: ge211_error.h:22
const char * what() const noexcept override
The error message associated with the exception.
Definition: ge211_error.cpp:13
Indicates an error loading a font front an already-open file.
Definition: ge211_error.h:116
Indicates that an error was encountered by the game engine or in the client&#39;s environment.
Definition: ge211_error.h:58
Indicates an error opening a file.
Definition: ge211_error.h:106