PLCnext API Documentation  22.9.0.33
Exception.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include <memory>
9 #include <iostream>
10 
11 namespace Arp
12 {
13 
15 class Exception
16 {
17 public: // typedefs
19  typedef std::shared_ptr<Exception> Ptr;
20 
21 public: // construction/destruction (policies)
25  template<typename... Args>
26  Exception(const char* message, const Args& ... args);
29  Exception(const String& message);
32  Exception(String&& message);
36  Exception(const String& message, const Exception& innerException);
40  Exception(String&& message, Exception&& innerException);
43  Exception(const Exception& arg) = default;
46  Exception(Exception&& arg) = default;
48  virtual ~Exception(void) = default;
49 
50 protected: //construction
54  Exception(String&& message, const Exception::Ptr& innerExceptionPtr);
55 
56 public: // setter/getter properties
60  uint32 GetTypeCode(void)const;
63  const String& GetMessage(void)const;
66  bool HasInnerException(void)const;
70 
71 public: // operations
75  String ToString(void)const;
76 
77 protected: // operations to override
80  virtual Exception::Ptr Clone(void)const;
85  virtual String Format(int indentLevel, bool withInnerException)const;
89  virtual uint32 GetTypeCodeInternal(void)const;
90 
91 private: // deleted methods
92  Exception& operator=(const Exception& arg) = delete;
93 
94 private: // fields
95  String message;
96  Exception::Ptr innerExceptionPtr;
97 
98 protected: // static fields
99  const static int indentSize = 3;
100  const static int indentChar = ' ';
101  const static int innerIndentChar = '-';
102 };
103 
105 // inline methods of class Exception
106 template<typename... Args>
107 inline Exception::Exception(const char* messageArg, const Args& ... args)
108  : message(String::Format(messageArg, args...))
109 {
110 }
111 
112 inline Exception::Exception(const String& messageArg)
113  : message(messageArg)
114 {
115 }
116 
117 inline Exception::Exception(String&& messageArg)
118  : message(std::move(messageArg))
119 {
120 }
121 
122 inline Exception::Exception(const String& messageArg, const Exception& innerException)
123  : message(messageArg), innerExceptionPtr(innerException.Clone())
124 {
125 }
126 
127 inline Exception::Exception(String&& messageArg, Exception&& innerException)
128  : message(std::move(messageArg)), innerExceptionPtr(innerException.Clone())
129 {
130 }
131 
132 inline Exception::Exception(String&& messageArg, const Exception::Ptr& innerException)
133  : message(std::move(messageArg)), innerExceptionPtr(innerException)
134 {
135 }
136 
137 inline const String& Exception::GetMessage()const
138 {
139  return this->message;
140 }
141 
143 {
144  return (bool)this->innerExceptionPtr;
145 }
146 
148 {
149  return this->innerExceptionPtr;
150 }
151 
153 {
154  return this->GetTypeCodeInternal();
155 }
156 
158 // global operators of class Exception
159 
164 inline std::ostream& operator<<(std::ostream& os, const Exception& e)
165 {
166  os << e.ToString();
167  return os;
168 }
169 
170 } // end of namesapce Arp
This is the base class of all Arp exception classes.
Definition: Exception.hpp:16
Exception::Ptr GetInnerException(void) const
Gets the inner exception of this exception.
Definition: Exception.hpp:147
uint32 GetTypeCode(void) const
Gets the type code of this exception.
Definition: Exception.hpp:152
const String & GetMessage(void) const
Gets the error message of this exception.
Definition: Exception.hpp:137
std::shared_ptr< Exception > Ptr
The smart pointer tpye of this class.
Definition: Exception.hpp:19
virtual Exception::Ptr Clone(void) const
Clones this instance.
Exception(Exception &&arg)=default
Constructs an instance of class Exception.
String ToString(void) const
Gets a reasonable string representation of this exception.
static const int indentChar
The indentation character (blank)
Definition: Exception.hpp:100
virtual uint32 GetTypeCodeInternal(void) const
Get the type code of this exception. Must be overridden by derived classes.
bool HasInnerException(void) const
Determines if this exception has an inner exception.
Definition: Exception.hpp:142
static const int indentSize
The indentation size (count of blanks)
Definition: Exception.hpp:99
virtual String Format(int indentLevel, bool withInnerException) const
Formats this exception using the given indent level.
Exception(const Exception &arg)=default
Constructs an instance of class Exception.
Exception(const char *message, const Args &... args)
Constructs an instance of class Exception.
Definition: Exception.hpp:107
static const int innerIndentChar
The indentation character to indent inner exceptions.
Definition: Exception.hpp:101
virtual ~Exception(void)=default
Destructs this instance virually.
std::ostream & operator<<(std::ostream &os, const BasicString< CharType, Alloc > &right)
Streams the right string to the outstream os .
Definition: BasicString.hxx:1708
std::uint32_t uint32
The Arp unsigned integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:35
Root namespace for the PLCnext API
Namespace of the C++ standard library