CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
exception.h
1
3#ifndef _aspose_system_exception_h_
4#define _aspose_system_exception_h_
5
6#include <system/details/is_forwarding_of.h>
7#include <system/object.h>
8#include <system/string.h>
9#include <system/shared_ptr.h>
10#include <system/runtime/serialization/streaming_context.h>
11#include <exception>
12#include <type_traits>
13#include <cstdint>
14#include <memory>
15
16namespace System
17{
18namespace Runtime { namespace Serialization {
19class SerializationInfo;
20}}
21
23
26
27template<class T>
29
34class ExceptionWrapperHelper
35{
36 template<class T> friend class System::ExceptionWrapper;
37
38 template <class T>
39 class BaseTypeProvider
40 {
41 public:
42 static typename T::BaseType Dummy;
43 using BaseType = decltype(ExceptionWrapperHelper::BaseTypeProvider<T>::Dummy);
44 };
45};
47
51class ASPOSECPP_SHARED_CLASS ExceptionWrapperBase : public ExceptionWrapperType, public std::exception
52{
53public:
55 ASPOSECPP_SHARED_API ExceptionWrapperBase(std::nullptr_t);
56
59 ASPOSECPP_SHARED_API ExceptionWrapperBase(const ExceptionPtr& ptr);
60
63 virtual ASPOSECPP_SHARED_API const char* what() const noexcept override;
64
67 ExceptionPtr Get() const { return m_ptr; }
68
71 ASPOSECPP_SHARED_API bool IsNull() const;
72
76 ASPOSECPP_SHARED_API bool operator == (const ExceptionWrapperBase &e) const;
77
80 ASPOSECPP_SHARED_API bool operator == (const std::nullptr_t) const;
81
84 ASPOSECPP_SHARED_API bool operator != (const std::nullptr_t) const;
85
89 ASPOSECPP_SHARED_API bool Equals(const ExceptionWrapperBase &e) const;
90
94 ASPOSECPP_SHARED_API bool Is(const System::TypeInfo &target) const;
95
98 ASPOSECPP_SHARED_API int GetHashCode() const;
99
102 ASPOSECPP_SHARED_API String ToString() const;
103
104protected:
106 ExceptionPtr m_ptr;
107};
109
111template <typename T>
112class ASPOSECPP_SHARED_CLASS ExceptionWrapper : public ExceptionWrapper<typename ExceptionWrapperHelper::BaseTypeProvider<T>::BaseType>
113{
114private:
115 using ExceptionBaseType = typename ExceptionWrapperHelper::BaseTypeProvider<T>::BaseType;
116
117public:
120 {
122 }
123
125 using ExceptionType = T;
126
127 static void* operator new(std::size_t) = delete;
128 static void* operator new[](std::size_t) = delete;
129
132 static const System::TypeInfo& Type() { return T::Type(); }
133
135 ExceptionWrapper(std::nullptr_t) : ExceptionWrapper<ExceptionBaseType>(nullptr) {}
136
139 ExceptionWrapper(const ExceptionPtr& ptr) : ExceptionWrapper<T>(static_cast<T*>(ptr.get())) {}
140
143 ExceptionWrapper(const ExceptionWrapper& other) : ExceptionWrapper<ExceptionBaseType>(other.m_ptr) {}
144
148 : ExceptionWrapper<ExceptionBaseType>(std::forward<ExceptionPtr>(other.m_ptr)) {}
149
154 {
155 if (this != &other)
157
158 return *this;
159 }
160
165 {
166 if (this != &other)
167 ExceptionWrapper<ExceptionBaseType>::m_ptr = std::move(other.m_ptr);
168
169 return *this;
170 }
171
174 template <typename ...Args,
175 typename = std::enable_if_t<
176 !System::Details::IsForwardingOf<ExceptionPtr, Args...>::value &&
177 !System::Details::IsForwardingOf<ExceptionWrapper<T>, Args...>::value>>
178 explicit ExceptionWrapper(Args&& ...args)
179 : ExceptionWrapper<ExceptionBaseType>(
181 T::MakeObject(
182 std::forward<Args>(args)...
183 )
184 )
185 )
186 {}
187
191 T* operator->() const
192 {
193 return static_cast<T*>(ExceptionWrapper<ExceptionBaseType>::m_ptr.GetObjectNotNull());
194 }
195
196protected:
199 ExceptionWrapper(T* ptr) : ExceptionWrapper<ExceptionBaseType>(static_cast<ExceptionBaseType*>(ptr)) {}
200};
201
203template <typename T>
204struct ASPOSECPP_SHARED_CLASS CastResult<ExceptionWrapper<T>>
205{
207 typedef ExceptionWrapper<T> type;
208};
210
213template <>
214class ASPOSECPP_SHARED_CLASS ExceptionWrapper<Details_Exception> : public ExceptionWrapperBase
215{
216public:
218 ASPOSECPP_SHARED_API operator SharedPtr<Object>();
219
221 using ExceptionType = Details_Exception;
222
224 static ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> NullException;
225
226 static void* operator new(std::size_t) = delete;
227 static void* operator new[](std::size_t) = delete;
228
231 static const ASPOSECPP_SHARED_API System::TypeInfo& Type();
232
234 ASPOSECPP_SHARED_API ExceptionWrapper(std::nullptr_t);
235
237 ASPOSECPP_SHARED_API ExceptionWrapper();
238
241 ASPOSECPP_SHARED_API ExceptionWrapper(const ExceptionPtr& ptr);
242
245 ASPOSECPP_SHARED_API ExceptionWrapper(const String& message);
246
250 ASPOSECPP_SHARED_API ExceptionWrapper(const String &message, const ExceptionWrapper<Details_Exception>& innerException);
251
256
260 ASPOSECPP_SHARED_API Details_Exception* operator->() const;
261
264 [[noreturn]] ASPOSECPP_SHARED_API void Throw() const;
265
266protected:
269 ASPOSECPP_SHARED_API ExceptionWrapper(Details_Exception* ptr);
270};
272
276class ASPOSECPP_SHARED_CLASS Details_Exception : public System::Object
277{
278 RTTI_INFO_NAMED(System::Details_Exception, "System::Exception", ::System::BaseTypesInfo<System::Object>);
279
280 template <typename T> friend class ExceptionWrapper;
281
282public:
283 // imitate to C# .Message property for correct translation
285 virtual ASPOSECPP_SHARED_API String get_Message() const;
286
288 virtual ASPOSECPP_SHARED_API String get_StackTrace() const;
289
291 virtual ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> get_InnerException() const;
292
294 virtual ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> GetBaseException() const;
295
297 ASPOSECPP_SHARED_API int32_t get_HResult() const;
298
300 ASPOSECPP_SHARED_API void set_HResult(int32_t value);
301
303 virtual ASPOSECPP_SHARED_API String ToString() const override;
304
310 virtual ASPOSECPP_SHARED_API const char* what() const noexcept;
311
314 [[noreturn]] virtual ASPOSECPP_SHARED_API void DoThrow(const ExceptionPtr& self) const;
315
316protected:
318 ASPOSECPP_SHARED_API Details_Exception();
319 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(), CODEPORTING_ARGS());
320
323 ASPOSECPP_SHARED_API Details_Exception(const String& message);
324 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(const String& message), CODEPORTING_ARGS(message));
325
329 ASPOSECPP_SHARED_API Details_Exception(const String &message, const ExceptionWrapper<Details_Exception>& innerException);
330 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(const String &message, const ExceptionWrapper<Details_Exception>& innerException), CODEPORTING_ARGS(message, innerException));
331
335 ASPOSECPP_SHARED_API Details_Exception(const System::SharedPtr<System::Runtime::Serialization::SerializationInfo>& info, System::Runtime::Serialization::StreamingContext context);
336 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception,
337 CODEPORTING_ARGS(const System::SharedPtr<System::Runtime::Serialization::SerializationInfo>& info, System::Runtime::Serialization::StreamingContext context),
338 CODEPORTING_ARGS(info, context)
339 );
340
342 String m_message;
345
347 mutable std::string m_std_message;
348
350 mutable int32_t m_HResult;
351
352 // Use it in derived classes
354 virtual ASPOSECPP_SHARED_API String ExtraDescription() const;
355
357 String DefaultMessage() const;
358
359private:
362 virtual ASPOSECPP_SHARED_API String FullMessage(const String& message) const;
363
364 Details_Exception(const Details_Exception&) = delete;
365 Details_Exception& operator=(const Details_Exception&) = delete;
366};
367
370
372ASPOSECPP_SHARED_API void PrintTo(const Details_Exception& exception, std::ostream* stream);
373
375template <typename T>
376void PrintTo(const ExceptionWrapper<T>& exception_wrapper, std::ostream* stream)
377{
378 T* exception = exception_wrapper.operator->();
379 if (exception == nullptr)
380 *stream << "nullptr";
381 else
382 PrintTo(*exception, stream);
383}
384
389inline std::ostream& operator<<(std::ostream& stream, const Exception& exception)
390{
391 if (exception != nullptr)
392 stream << exception->ToString();
393 return stream;
394}
395
400inline std::wostream& operator<<(std::wostream& stream, const Exception& exception)
401{
402 if (exception != nullptr)
403 stream << exception->ToString();
404 return stream;
405}
406
407} // namespace System
408
413ASPOSECPP_SHARED_API bool operator != (const System::ExceptionWrapperBase& e1, const System::ExceptionWrapperBase& e2);
414
418ASPOSECPP_SHARED_API bool operator == (std::nullptr_t, const System::ExceptionWrapperBase &e);
419
423ASPOSECPP_SHARED_API bool operator != (std::nullptr_t, const System::ExceptionWrapperBase &e);
424
425#endif // _aspose_system_exception_h_
Represents an exception. Never create instances of this class manually. Use the Exception class inste...
Definition: exception.h:277
virtual ExceptionWrapper< Details_Exception > get_InnerException() const
Returns a reference to the object representing the inner exception.
int32_t get_HResult() const
Returns a 32-bit integer value which is a HRESULT code associated with the exception reprsented by th...
virtual String get_Message() const
Returns the string containing the error destcription.
virtual ExceptionWrapper< Details_Exception > GetBaseException() const
Returns the copy of Exception object representing the inner-most exception.
virtual String get_StackTrace() const
Returns the string containing the stack trace.
virtual const char * what() const noexcept
Implements what() method which is called by ExceptionWrapper class. Despite of the fact that this cla...
void set_HResult(int32_t value)
Sets HRESULT, a coded numerical value that is assigned to a specific exception.
virtual String ToString() const override
Returns the string representation of the current object.
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:113
ExceptionWrapper & operator=(ExceptionWrapper &&other) noexcept
Move assignment operator.
Definition: exception.h:164
ExceptionWrapper(Args &&...args)
Constructor that forwards parameters to the Exception class constructors and creates smart pointer th...
Definition: exception.h:178
static const System::TypeInfo & Type()
Shortcut to get System::TypeInfo object for the Exception type.
Definition: exception.h:132
ExceptionWrapper(const ExceptionPtr &ptr)
Constructs a instance of ExceptionWrapper class that contains passed pointer.
Definition: exception.h:139
T ExceptionType
Used for casting functions.
Definition: exception.h:125
ExceptionWrapper(std::nullptr_t)
Constructs a null-instance of ExceptionWrapper class that does not represent any exception.
Definition: exception.h:135
ExceptionWrapper(const ExceptionWrapper &other)
Copy constructor.
Definition: exception.h:143
T * operator->() const
Allows to access members of the Exception object.
Definition: exception.h:191
ExceptionWrapper(ExceptionWrapper &&other) noexcept
Move constructor.
Definition: exception.h:147
ExceptionWrapper & operator=(const ExceptionWrapper &other)
Assignment operator.
Definition: exception.h:153
ExceptionWrapper(T *ptr)
Constructs a instance of ExceptionWrapper class that contains passed instance.
Definition: exception.h:199
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Dummy class to make StreamingContext-using translated classes compile. Do not manage instances of thi...
Definition: streaming_context.h:12
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Represents a particular type and provides information about it.
Definition: type_info.h:109
ExceptionType
Definition: xml_convert.h:133
Definition: db_command.h:9
void PrintTo(DateTime value, std::ostream *stream)
Prints value to ostream. Mostly used for debug.
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:157
SmartPtr< Y > static_pointer_cast(SmartPtr< X > const &x)
Casts smart pointers using static_cast.
Definition: smart_ptr.h:1669
bool Equals(const TA &a, const TB &b)
Determines the equality of two values applying operator==() to them.
Definition: primitive_types.h:77
std::enable_if<!IsSmartPtr< T >::value, SmartPtr< T > >::type MakeObject(Args &&... args)
Creates object on heap and returns shared pointer to it.
Definition: smart_ptr.h:1506
System::SmartPtr< Details_Exception > ExceptionPtr
Type alias used by exception wrappers.
Definition: exception.h:25
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
std::enable_if< std::is_scalar< T >::value, int >::type GetHashCode(const T &obj)
Returns a hash code for the specified scalar value.
Definition: get_hash_code.h:21
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:151