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
22namespace Collections { namespace Generic {
23template<typename, typename>
24class IDictionary;
25}}
26
27class Details_Exception;
28
31
32template<class T>
34
39class ExceptionWrapperHelper
40{
41 template<class T> friend class System::ExceptionWrapper;
42
43 template <class T>
44 class BaseTypeProvider
45 {
46 public:
47 static typename T::BaseType Dummy;
48 using BaseType = decltype(ExceptionWrapperHelper::BaseTypeProvider<T>::Dummy);
49 };
50};
52
56class ASPOSECPP_SHARED_CLASS ExceptionWrapperBase : public ExceptionWrapperType, public std::exception
57{
58public:
60 ASPOSECPP_SHARED_API ExceptionWrapperBase(std::nullptr_t);
61
64 ASPOSECPP_SHARED_API ExceptionWrapperBase(const ExceptionPtr& ptr);
65
68 virtual ASPOSECPP_SHARED_API const char* what() const noexcept override;
69
72 ExceptionPtr Get() const { return m_ptr; }
73
76 ASPOSECPP_SHARED_API bool IsNull() const;
77
81 ASPOSECPP_SHARED_API bool operator == (const ExceptionWrapperBase &e) const;
82
85 ASPOSECPP_SHARED_API bool operator == (const std::nullptr_t) const;
86
89 ASPOSECPP_SHARED_API bool operator != (const std::nullptr_t) const;
90
94 ASPOSECPP_SHARED_API bool Equals(const ExceptionWrapperBase &e) const;
95
99 ASPOSECPP_SHARED_API bool Is(const System::TypeInfo &target) const;
100
103 ASPOSECPP_SHARED_API int GetHashCode() const;
104
107 ASPOSECPP_SHARED_API String ToString() const;
108
109protected:
111 ExceptionPtr m_ptr;
112};
114
116template <typename T>
117class ASPOSECPP_SHARED_CLASS ExceptionWrapper : public ExceptionWrapper<typename ExceptionWrapperHelper::BaseTypeProvider<T>::BaseType>
118{
119private:
120 using ExceptionBaseType = typename ExceptionWrapperHelper::BaseTypeProvider<T>::BaseType;
121
122public:
125 {
127 }
128
130 using ExceptionType = T;
131
132 static void* operator new(std::size_t) = delete;
133 static void* operator new[](std::size_t) = delete;
134
137 static const System::TypeInfo& Type() { return T::Type(); }
138
140 ExceptionWrapper(std::nullptr_t) : ExceptionWrapper<ExceptionBaseType>(nullptr) {}
141
144 ExceptionWrapper(const ExceptionPtr& ptr) : ExceptionWrapper<T>(static_cast<T*>(ptr.get())) {}
145
148 ExceptionWrapper(const ExceptionWrapper& other) : ExceptionWrapper<ExceptionBaseType>(other.m_ptr) {}
149
153 : ExceptionWrapper<ExceptionBaseType>(std::forward<ExceptionPtr>(other.m_ptr)) {}
154
159 {
160 if (this != &other)
162
163 return *this;
164 }
165
170 {
171 if (this != &other)
172 ExceptionWrapper<ExceptionBaseType>::m_ptr = std::move(other.m_ptr);
173
174 return *this;
175 }
176
179 template <typename ...Args,
180 typename = std::enable_if_t<
181 !System::Details::IsForwardingOf<ExceptionPtr, Args...>::value &&
182 !System::Details::IsForwardingOf<ExceptionWrapper<T>, Args...>::value>>
183 explicit ExceptionWrapper(Args&& ...args)
184 : ExceptionWrapper<ExceptionBaseType>(
186 T::MakeObject(
187 std::forward<Args>(args)...
188 )
189 )
190 )
191 {}
192
196 T* operator->() const
197 {
198 return static_cast<T*>(ExceptionWrapper<ExceptionBaseType>::m_ptr.GetObjectNotNull());
199 }
200
201protected:
204 ExceptionWrapper(T* ptr) : ExceptionWrapper<ExceptionBaseType>(static_cast<ExceptionBaseType*>(ptr)) {}
205};
206
208template <typename T>
209struct ASPOSECPP_SHARED_CLASS CastResult<ExceptionWrapper<T>>
210{
212 typedef ExceptionWrapper<T> type;
213};
215
218template <>
219class ASPOSECPP_SHARED_CLASS ExceptionWrapper<Details_Exception> : public ExceptionWrapperBase
220{
221public:
223 ASPOSECPP_SHARED_API operator SharedPtr<Object>();
224
226 using ExceptionType = Details_Exception;
227
229 static ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> NullException;
230
231 static void* operator new(std::size_t) = delete;
232 static void* operator new[](std::size_t) = delete;
233
236 static const ASPOSECPP_SHARED_API System::TypeInfo& Type();
237
239 ASPOSECPP_SHARED_API ExceptionWrapper(std::nullptr_t);
240
242 ASPOSECPP_SHARED_API ExceptionWrapper();
243
246 ASPOSECPP_SHARED_API ExceptionWrapper(const ExceptionPtr& ptr);
247
250 ASPOSECPP_SHARED_API ExceptionWrapper(const String& message);
251
255 ASPOSECPP_SHARED_API ExceptionWrapper(const String &message, const ExceptionWrapper<Details_Exception>& innerException);
256
261
265 ASPOSECPP_SHARED_API Details_Exception* operator->() const;
266
269 [[noreturn]] ASPOSECPP_SHARED_API void Throw() const;
270
271protected:
274 ASPOSECPP_SHARED_API ExceptionWrapper(Details_Exception* ptr);
275};
277
281class ASPOSECPP_SHARED_CLASS Details_Exception : public System::Object
282{
283 RTTI_INFO_NAMED(System::Details_Exception, "System::Exception", ::System::BaseTypesInfo<System::Object>);
284
285 template <typename T> friend class ExceptionWrapper;
286
287public:
288 // imitate to C# .Message property for correct translation
290 virtual ASPOSECPP_SHARED_API String get_Message() const;
291
293 virtual ASPOSECPP_SHARED_API String get_StackTrace() const;
294
296 virtual ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> get_InnerException() const;
297
299 virtual ASPOSECPP_SHARED_API ExceptionWrapper<Details_Exception> GetBaseException() const;
300
302 ASPOSECPP_SHARED_API int32_t get_HResult() const;
303
305 ASPOSECPP_SHARED_API void set_HResult(int32_t value);
306
309
311 virtual ASPOSECPP_SHARED_API String ToString() const override;
312
318 virtual ASPOSECPP_SHARED_API const char* what() const noexcept;
319
322 [[noreturn]] virtual ASPOSECPP_SHARED_API void DoThrow(const ExceptionPtr& self) const;
323
324protected:
326 ASPOSECPP_SHARED_API Details_Exception();
327 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(), CODEPORTING_ARGS());
328
331 ASPOSECPP_SHARED_API Details_Exception(const String& message);
332 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(const String& message), CODEPORTING_ARGS(message));
333
337 ASPOSECPP_SHARED_API Details_Exception(const String &message, const ExceptionWrapper<Details_Exception>& innerException);
338 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception, CODEPORTING_ARGS(const String &message, const ExceptionWrapper<Details_Exception>& innerException), CODEPORTING_ARGS(message, innerException));
339
343 ASPOSECPP_SHARED_API Details_Exception(const System::SharedPtr<System::Runtime::Serialization::SerializationInfo>& info, System::Runtime::Serialization::StreamingContext context);
344 MEMBER_FUNCTION_MAKE_OBJECT(Details_Exception,
345 CODEPORTING_ARGS(const System::SharedPtr<System::Runtime::Serialization::SerializationInfo>& info, System::Runtime::Serialization::StreamingContext context),
346 CODEPORTING_ARGS(info, context)
347 );
348
350 String m_message;
354 SharedPtr<Collections::Generic::IDictionary<SharedPtr<Object>, SharedPtr<Object>>> m_data;
355
357 mutable std::string m_std_message;
358
360 mutable int32_t m_HResult;
361
362 // Use it in derived classes
364 virtual ASPOSECPP_SHARED_API String ExtraDescription() const;
365
367 String DefaultMessage() const;
368
369private:
372 virtual ASPOSECPP_SHARED_API String FullMessage(const String& message) const;
373
374 Details_Exception(const Details_Exception&) = delete;
375 Details_Exception& operator=(const Details_Exception&) = delete;
376};
377
380
382ASPOSECPP_SHARED_API void PrintTo(const Details_Exception& exception, std::ostream* stream);
383
385template <typename T>
386void PrintTo(const ExceptionWrapper<T>& exception_wrapper, std::ostream* stream)
387{
388 T* exception = exception_wrapper.operator->();
389 if (exception == nullptr)
390 *stream << "nullptr";
391 else
392 PrintTo(*exception, stream);
393}
394
399inline std::ostream& operator<<(std::ostream& stream, const Exception& exception)
400{
401 if (exception != nullptr)
402 stream << exception->ToString();
403 return stream;
404}
405
410inline std::wostream& operator<<(std::wostream& stream, const Exception& exception)
411{
412 if (exception != nullptr)
413 stream << exception->ToString();
414 return stream;
415}
416
417} // namespace System
418
423ASPOSECPP_SHARED_API bool operator != (const System::ExceptionWrapperBase& e1, const System::ExceptionWrapperBase& e2);
424
428ASPOSECPP_SHARED_API bool operator == (std::nullptr_t, const System::ExceptionWrapperBase &e);
429
433ASPOSECPP_SHARED_API bool operator != (std::nullptr_t, const System::ExceptionWrapperBase &e);
434
435#endif // _aspose_system_exception_h_
Represents an exception. Never create instances of this class manually. Use the Exception class inste...
Definition: exception.h:282
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.
virtual SharedPtr< Collections::Generic::IDictionary< SharedPtr< Object >, SharedPtr< Object > > > get_Data()
Returns dictionary with custom exception data.
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:118
ExceptionWrapper & operator=(ExceptionWrapper &&other) noexcept
Move assignment operator.
Definition: exception.h:169
ExceptionWrapper(Args &&...args)
Constructor that forwards parameters to the Exception class constructors and creates smart pointer th...
Definition: exception.h:183
static const System::TypeInfo & Type()
Shortcut to get System::TypeInfo object for the Exception type.
Definition: exception.h:137
ExceptionWrapper(const ExceptionPtr &ptr)
Constructs a instance of ExceptionWrapper class that contains passed pointer.
Definition: exception.h:144
T ExceptionType
Used for casting functions.
Definition: exception.h:130
ExceptionWrapper(std::nullptr_t)
Constructs a null-instance of ExceptionWrapper class that does not represent any exception.
Definition: exception.h:140
ExceptionWrapper(const ExceptionWrapper &other)
Copy constructor.
Definition: exception.h:148
T * operator->() const
Allows to access members of the Exception object.
Definition: exception.h:196
ExceptionWrapper(ExceptionWrapper &&other) noexcept
Move constructor.
Definition: exception.h:152
ExceptionWrapper & operator=(const ExceptionWrapper &other)
Assignment operator.
Definition: exception.h:158
ExceptionWrapper(T *ptr)
Constructs a instance of ExceptionWrapper class that contains passed instance.
Definition: exception.h:204
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
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:125
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:156
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:30
auto Get(const ValueTuple< Args... > &tuple)
Gets N-th element of value tuple.
Definition: value_tuple.h:304
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:150