AAX SDK 2.6.1
Avid Audio Extensions Development Kit
Loading...
Searching...
No Matches
AAX_Exception.h
Go to the documentation of this file.
1/*================================================================================================*/
2/*
3 * Copyright 2016-2017, 2023 Avid Technology, Inc.
4 * All rights reserved.
5 *
6 * CONFIDENTIAL: this document contains confidential information of Avid. Do
7 * not disclose to any third party. Use of the information contained in this
8 * document is subject to an Avid SDK license.
9 */
10
16/*================================================================================================*/
17
18
19#ifndef AAXLibrary_AAX_Exception_h
20#define AAXLibrary_AAX_Exception_h
21
22#include "AAX_Assert.h"
23#include "AAX_StringUtilities.h"
24#include "AAX.h"
25
26#include <exception>
27#include <string>
28#include <set>
29
30
32#if 0
33#pragma mark -
34#pragma mark AAX::Exception
35#endif
37
38namespace AAX
39{
40 namespace Exception {
41 class Any;
42 }
43
46 inline std::string AsString(const char* inStr);
47 inline const std::string& AsString(const std::string& inStr);
48 inline const std::string& AsString(const Exception::Any& inStr);
49
50
58 namespace Exception
59 {
73 class Any
74 {
75 public:
76 virtual ~Any() {}
77
80 template <class C>
81 explicit Any(const C& inWhat)
82 : mDesc(AAX::AsString(inWhat))
83 , mFunction()
84 , mLine()
85 , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
86 {
87 }
88
91 template <class C1, class C2, class C3>
92 explicit Any(const C1& inWhat, const C2& inFunction, const C3& inLine)
93 : mDesc(AAX::AsString(inWhat))
94 , mFunction(AAX::AsString(inFunction))
95 , mLine(AAX::AsString(inLine))
96 , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
97 {
98 }
99
100 // assignment operator
101 Any& operator=(const Any& inOther)
102 {
103 mDesc = inOther.mDesc;
104 mFunction = inOther.mFunction;
105 mLine = inOther.mLine;
106 mWhat = inOther.mWhat;
107 return *this;
108 }
109
112
113 public: // AAX::Exception::Any
114
115#ifndef AAX_CPP11_SUPPORT
116 // implicit conversion to std::string (mostly for AsString())
117 operator const std::string&(void) const { return mWhat; }
118#endif
119
120 const std::string& What() const { return mWhat; }
121 const std::string& Desc() const { return mDesc; }
122 const std::string& Function() const { return mFunction; }
123 const std::string& Line() const { return mLine; }
124
125 private:
126 static std::string CreateWhat(const std::string& inDesc, const std::string& inFunc, const std::string& inLine)
127 {
128 std::string whatStr(inDesc);
129 if (false == inFunc.empty()) { whatStr += (" func:" + inFunc); }
130 if (false == inLine.empty()) { whatStr += (" line:" + inLine); }
131 return whatStr;
132 }
133
134 private:
135 std::string mDesc;
136 std::string mFunction;
137 std::string mLine;
138 std::string mWhat;
139 };
140
143 class ResultError : public Any
144 {
145 public:
146 explicit ResultError(AAX_Result inWhatResult)
147 : Any(ResultError::FormatResult(inWhatResult))
148 , mResult(inWhatResult)
149 {
150 }
151
152 template <class C>
153 explicit ResultError(AAX_Result inWhatResult, const C& inFunction)
154 : Any(ResultError::FormatResult(inWhatResult), inFunction, (const char*)NULL)
155 , mResult(inWhatResult)
156 {
157 }
158
159 template <class C1, class C2>
160 explicit ResultError(AAX_Result inWhatResult, const C1& inFunction, const C2& inLine)
161 : Any(ResultError::FormatResult(inWhatResult), inFunction, inLine)
162 , mResult(inWhatResult)
163 {
164 }
165
166 static std::string FormatResult(AAX_Result inResult)
167 {
168 return std::string(AAX::AsStringResult(inResult) + " (" + AAX::AsStringInt32((int32_t)inResult) + ")");
169 }
170
171 AAX_Result Result() const { return mResult; }
172
173 private:
174 AAX_Result mResult;
175 };
176 }
177
178 std::string AsString(const char* inStr)
179 {
180 return inStr ? std::string(inStr) : std::string();
181 }
182
183 const std::string& AsString(const std::string& inStr)
184 {
185 return inStr;
186 }
187
188 const std::string& AsString(const Exception::Any& inStr)
189 {
190 return inStr.What();
191 }
192}
193
194
196#if 0
197#pragma mark -
198#endif
200
314{
315public:
317
318 /* non-virtual destructor */ ~AAX_CheckedResult() {}
319
322 : mCurResult(AAX_SUCCESS)
323 , mLastError(AAX_SUCCESS)
324 , mAcceptedResults()
325 {
326 Initialize();
327 }
328
332 : mCurResult(inResult)
333 , mLastError(AAX_SUCCESS)
334 , mAcceptedResults()
335 {
336 Initialize();
337 Check();
338 }
339
346 {
347 mAcceptedResults.insert(inResult);
348 }
349
351 {
352 mAcceptedResults.clear();
353 mAcceptedResults.insert(AAX_SUCCESS);
354 }
355
358 {
359 mCurResult = inResult;
360 Check();
361 return *this;
362 }
363
367 {
368 return this->operator=(inResult);
369 }
370
372 operator AAX_Result() const
373 {
374 return mCurResult;
375 }
376
379 void Clear()
380 {
381 mCurResult = AAX_SUCCESS;
382 mLastError = AAX_SUCCESS;
383 }
384
388 {
389 return mLastError;
390 }
391
392private:
393 void Initialize()
394 {
396 }
397
398 void Check()
399 {
400 const AAX_Result err = mCurResult;
401 if (0 == mAcceptedResults.count(err))
402 {
404
405 // error state is now captured in ex
406 mCurResult = AAX_SUCCESS;
407 mLastError = err;
408
409 AAX_TRACE_RELEASE(kAAX_Trace_Priority_Normal, "AAX_CheckedResult - throwing %s", ex.What().c_str());
410 AAX_STACKTRACE(kAAX_Trace_Priority_Lowest, ""); // stacktrace is only printed for debug plug-in builds
411 throw ex;
412 }
413 }
414
415private:
416 AAX_Result mCurResult;
417 AAX_Result mLastError;
418 std::set<AAX_Result> mAcceptedResults;
419};
420
421
423#if 0
424#pragma mark -
425#pragma mark AAX exception macros
426#endif
428
444#define AAX_SWALLOW(...) \
445 try { if(true) { ( __VA_ARGS__ ); } } \
446 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
447 AAX_TRACE_RELEASE(kAAX_Trace_Priority_High, "%s line %d (%s) exception caught: %s (swallowed)", __FILE__, __LINE__, __FUNCTION__, AAX_PREPROCESSOR_CONCAT(ex,__LINE__).What().c_str()); \
448 } do {} while (false)
449
472#define AAX_SWALLOW_MULT(...) \
473try { if(true) { __VA_ARGS__ } } \
474catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
475AAX_TRACE_RELEASE(kAAX_Trace_Priority_High, "%s line %d (%s) exception caught: %s (swallowed)", __FILE__, __LINE__, __FUNCTION__, AAX_PREPROCESSOR_CONCAT(ex,__LINE__).What().c_str()); \
476} do {} while (false)
477
499#define AAX_CAPTURE(X, ...) \
500try { if(true) { ( __VA_ARGS__ ); } } \
501catch (const AAX::Exception::ResultError& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
502AAX_TRACE_RELEASE(kAAX_Trace_Priority_High, "%s line %d (%s) exception caught: %s (captured)", __FILE__, __LINE__, __FUNCTION__, AAX_PREPROCESSOR_CONCAT(ex,__LINE__).What().c_str()); \
503(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
504} do {} while (false)
505
537#define AAX_CAPTURE_MULT(X, ...) \
538try { if(true) { __VA_ARGS__ } } \
539catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
540AAX_TRACE_RELEASE(kAAX_Trace_Priority_High, "%s line %d (%s) exception caught: %s (captured)", __FILE__, __LINE__, __FUNCTION__, AAX_PREPROCESSOR_CONCAT(ex,__LINE__).What().c_str()); \
541(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
542} do {} while (false)
543
544
546#if 0
547#pragma mark -
548#endif
550
589{
590public:
592
594 {
595 if (0 == mNumSucceeded && 0 < mNumFailed) {
596 try {
597 // do normal logging
598 this->Check();
599 }
600 catch(...)
601 {
602 // can't throw from a destructor
603 }
604 }
605 }
606
609 {
610 if (AAX_SUCCESS == inResult)
611 {
612 ++mNumSucceeded;
613 }
614 else
615 {
616 mLastFailure = inResult;
617 ++mNumFailed;
618 }
619
620 return *this;
621 }
622
624 operator AAX_Result()
625 {
626 AAX_Result const err = this->LastFailure();
627 this->Clear();
628 return err;
629 }
630
631 void Check() const { AAX_CheckedResult tempErr(mLastFailure); }
632 void Clear() {
633 mLastFailure = AAX_SUCCESS;
634 mNumFailed = 0;
635 mNumSucceeded = 0;
636 }
637
638 AAX_Result LastFailure() const { return mLastFailure; }
639 int NumFailed() const { return mNumFailed; }
640 int NumSucceeded() const { return mNumSucceeded; }
641 int NumAttempted() const { return mNumFailed+mNumSucceeded; }
642
643private:
644 AAX_Result mLastFailure{AAX_SUCCESS};
645 int mNumFailed{0};
646 int mNumSucceeded{0};
647};
648
649#endif
@ AAX_SUCCESS
Definition: AAX_Errors.h:36
Various utility definitions for AAX.
int32_t AAX_Result
Definition: AAX.h:334
Various string utility definitions for AAX Native.
Declarations for cross-platform AAX_ASSERT, AAX_TRACE and related facilities.
#define kAAX_Trace_Priority_Lowest
Definition: AAX_Assert.h:228
#define kAAX_Trace_Priority_Normal
Definition: AAX_Assert.h:226
#define AAX_TRACE_RELEASE(iPriority,...)
Print a trace statement to the log.
Definition: AAX_Assert.h:232
#define AAX_STACKTRACE(iPriority,...)
Print a stack trace statement to the log (debug builds only)
Definition: AAX_Assert.h:277
Definition: AAX_EnvironmentUtilities.h:59
std::string AsStringResult(AAX_Result inResult)
Definition: AAX_StringUtilities.hpp:773
std::string AsString(const char *inStr)
Definition: AAX_Exception.h:178
std::string AsStringInt32(int32_t inInt32)
Definition: AAX_StringUtilities.hpp:327
Definition: AAX_Exception.h:74
const std::string & Desc() const
Definition: AAX_Exception.h:121
const std::string & Function() const
Definition: AAX_Exception.h:122
Any(const C &inWhat)
Definition: AAX_Exception.h:81
Any & operator=(const Any &inOther)
Definition: AAX_Exception.h:101
const std::string & What() const
Definition: AAX_Exception.h:120
Any(const C1 &inWhat, const C2 &inFunction, const C3 &inLine)
Definition: AAX_Exception.h:92
virtual ~Any()
Definition: AAX_Exception.h:76
const std::string & Line() const
Definition: AAX_Exception.h:123
Definition: AAX_Exception.h:144
ResultError(AAX_Result inWhatResult, const C1 &inFunction, const C2 &inLine)
Definition: AAX_Exception.h:160
static std::string FormatResult(AAX_Result inResult)
Definition: AAX_Exception.h:166
AAX_Result Result() const
Definition: AAX_Exception.h:171
ResultError(AAX_Result inWhatResult)
Definition: AAX_Exception.h:146
ResultError(AAX_Result inWhatResult, const C &inFunction)
Definition: AAX_Exception.h:153
Definition: AAX_Exception.h:314
AAX_CheckedResult(AAX_Result inResult)
Implicit conversion constructor from AAX_Result.
Definition: AAX_Exception.h:331
void ResetAcceptedResults()
Definition: AAX_Exception.h:350
AAX::Exception::ResultError Exception
Definition: AAX_Exception.h:316
AAX_CheckedResult & operator|=(AAX_Result inResult)
bitwise-or assignment to AAX_Result
Definition: AAX_Exception.h:366
AAX_CheckedResult()
Construct an AAX_CheckedResult in a success state.
Definition: AAX_Exception.h:321
void Clear()
Clears the current result state.
Definition: AAX_Exception.h:379
~AAX_CheckedResult()
Definition: AAX_Exception.h:318
AAX_CheckedResult & operator=(AAX_Result inResult)
Assignment to AAX_Result.
Definition: AAX_Exception.h:357
AAX_Result LastError() const
Get the last non-success result which was stored in this object, or AAX_SUCCESS if no non-success res...
Definition: AAX_Exception.h:387
void AddAcceptedResult(AAX_Result inResult)
Add an expected result which will not result in a throw.
Definition: AAX_Exception.h:345
Definition: AAX_Exception.h:589
AAX_Result LastFailure() const
Definition: AAX_Exception.h:638
~AAX_AggregateResult()
Definition: AAX_Exception.h:593
AAX_AggregateResult & operator=(AAX_Result inResult)
Overloaded operator=() for conversion from AAX_Result.
Definition: AAX_Exception.h:608
void Check() const
Definition: AAX_Exception.h:631
void Clear()
Definition: AAX_Exception.h:632
int NumSucceeded() const
Definition: AAX_Exception.h:640
AAX_AggregateResult()=default
int NumFailed() const
Definition: AAX_Exception.h:639
int NumAttempted() const
Definition: AAX_Exception.h:641