PLCnext API Documentation 25.0.2.69
Exception.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7
8#ifndef ARP_USE_ARP_SYSTEM_CORE
9
10#include "Arp/Base/Core/Exception.hpp"
11
12#else
13
14#include "Arp/System/Core/Arp.h"
15#include <memory>
16#include <iostream>
17
18namespace Arp
19{
20
22class Exception
23{
24public: // typedefs
26 typedef std::shared_ptr<Exception> Ptr;
27
28public: // construction/destruction (policies)
32 template<typename... Args>
33 Exception(const char* message, const Args& ... args);
36 Exception(const String& message);
39 Exception(String&& message);
43 Exception(const String& message, const Exception& innerException);
47 Exception(String&& message, Exception&& innerException);
50 Exception(const Exception& arg) = default;
53 Exception(Exception&& arg) = default;
55 virtual ~Exception(void) = default;
56
57protected: //construction
61 Exception(String&& message, const Exception::Ptr& innerExceptionPtr);
62
63public: // setter/getter properties
67 uint32 GetTypeCode(void)const;
70 const String& GetMessage(void)const;
73 bool HasInnerException(void)const;
77
78public: // operations
82 String ToString(void)const;
83
84protected: // operations to override
87 virtual Exception::Ptr Clone(void)const;
92 virtual String Format(int indentLevel, bool withInnerException)const;
96 virtual uint32 GetTypeCodeInternal(void)const;
97
98private: // deleted methods
99 Exception& operator=(const Exception& arg) = delete;
100
101private: // fields
102 String message;
103 Exception::Ptr innerExceptionPtr;
104
105protected: // static fields
106 const static int indentSize = 3;
107 const static int indentChar = ' ';
108 const static int innerIndentChar = '-';
109};
110
112// inline methods of class Exception
113template<typename... Args>
114inline Exception::Exception(const char* messageArg, const Args& ... args)
115 : message(String::Format(messageArg, args...))
116{
117}
118
119inline Exception::Exception(const String& messageArg)
120 : message(messageArg)
121{
122}
123
124inline Exception::Exception(String&& messageArg)
125 : message(std::move(messageArg))
126{
127}
128
129inline Exception::Exception(const String& messageArg, const Exception& innerException)
130 : message(messageArg), innerExceptionPtr(innerException.Clone())
131{
132}
133
134inline Exception::Exception(String&& messageArg, Exception&& innerException)
135 : message(std::move(messageArg)), innerExceptionPtr(innerException.Clone())
136{
137}
138
139inline Exception::Exception(String&& messageArg, const Exception::Ptr& innerException)
140 : message(std::move(messageArg)), innerExceptionPtr(innerException)
141{
142}
143
144inline const String& Exception::GetMessage()const
145{
146 return this->message;
147}
148
149inline bool Exception::HasInnerException()const
150{
151 return (bool)this->innerExceptionPtr;
152}
153
154inline Exception::Ptr Exception::GetInnerException()const
155{
156 return this->innerExceptionPtr;
157}
158
159inline uint32 Exception::GetTypeCode()const
160{
161 return this->GetTypeCodeInternal();
162}
163
165// global operators of class Exception
166
171inline std::ostream& operator<<(std::ostream& os, const Exception& e)
172{
173 os << e.ToString();
174 return os;
175}
176
177} // end of namesapce Arp
178
180// template specialization of Exception formatter
181template<> struct fmt::formatter<Arp::Exception> : public fmt::ostream_formatter {};
182
183#endif // ndef ARP_USE_ARP_SYSTEM_CORE
184
const Exception & GetInnerException(void) const
Gets the inner exception of this exception.
Definition: Exception.cpp:154
virtual Exception::Ptr Clone(void) const
This operation clones this instance polymorphically as a deep copy.
Definition: Exception.cpp:172
bool HasInnerException(void) const
Determines if this exception has an inner exception.
Definition: Exception.cpp:147
std::unique_ptr< Exception > Ptr
The smart pointer type of this class.
Definition: Exception.hpp:27
virtual String Format(bool withInnerException) const
Formats this exception using the given indent level.
Definition: Exception.cpp:180
virtual ~Exception(void)
Default destructor.
Exception(void)
Constructs an empty instance of class Exception.
Definition: Exception.cpp:47
virtual uint32 GetTypeCodeInternal(void) const
DEPRECATED.
Definition: Exception.cpp:220
String ToString(void) const
This operation should be used for logging and returns a reasonable description of the exception cause...
Definition: Exception.cpp:165
Exception & operator=(const Exception &arg)
Default copy-assignment operator.
TypeCode GetTypeCode(void) const
Gets the type code of this exception.
Definition: Exception.cpp:140
const String & GetMessage(void) const
Gets the error message of this exception.
Definition: Exception.cpp:118
std::uint32_t uint32
The Arp unsigned integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:33
Root namespace for the PLCnext API
Namespace of the C++ standard library