TuringSim
C++ framework to simulate abstract computing models
messageException.h
1 #pragma once
2 
3 #include <optional>
4 #include <exception>
5 
6 namespace TuringSim::Utils {
12  class MessageException : public std::exception {
13  public:
16  MessageException() = delete;
17 
21  MessageException(std::string message) noexcept : msg(message) {}
22 
26  MessageException(const MessageException& e) = default;
27 
32 
35  ~MessageException() noexcept override = default;
36 
40  std::string getMessage() const {
41  ensureFullMessage();
42  return *fullMsg;
43  }
44 
48  const char* what() const noexcept override {
49  ensureFullMessage();
50  return fullMsg->c_str();
51  }
52 
56  explicit operator std::string() const {
57  ensureFullMessage();
58  return getMessage();
59  }
60 
61  private:
62  void ensureFullMessage() const {
63  if(!fullMsg) {
64  fullMsg = this->makeFullMessage();
65  }
66  }
67 
68  protected:
74  virtual std::string makeFullMessage() const {
75  return msg;
76  }
77 
78  std::string msg;
79  mutable std::optional<std::string> fullMsg;
80  };
81 
89  template<typename CharT, typename Traits>
90  std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const MessageException& e) {
91  return os << e.getMessage();
92  }
93 
94 }
TuringSim::Utils::MessageException::MessageException
MessageException()=delete
a message exception must have an explicative message.
TuringSim::Utils::MessageException::~MessageException
~MessageException() noexcept override=default
Destructor.
TuringSim::Utils::MessageException::getMessage
std::string getMessage() const
Get the full error message.
Definition: messageException.h:40
TuringSim::Utils::MessageException::msg
std::string msg
The error message.
Definition: messageException.h:78
TuringSim::Utils::MessageException::what
const char * what() const noexcept override
Get the full error message.
Definition: messageException.h:48
TuringSim::Utils::MessageException
The Base class for all custom exceptions.
Definition: messageException.h:12
TuringSim::Utils::operator<<
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const MessageException &e)
Print a MessageException.
Definition: messageException.h:90
TuringSim::Utils::MessageException::MessageException
MessageException(std::string message) noexcept
Builds a MessageException with a message.
Definition: messageException.h:21
TuringSim::Utils::MessageException::fullMsg
std::optional< std::string > fullMsg
The optional full message. Empty until first request.
Definition: messageException.h:79
TuringSim::Utils
The namespace for basic function, not specific to TuringSim.
TuringSim::Utils::MessageException::makeFullMessage
virtual std::string makeFullMessage() const
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
Definition: messageException.h:74
TuringSim::Utils::MessageException::MessageException
MessageException(MessageException &&e)=default
Move constructor.
TuringSim::Utils::MessageException::MessageException
MessageException(const MessageException &e)=default
Copy constructor.