AAX SDK 2.8.0
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-2024 Avid Technology, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the Avid AAX SDK.
7 *
8 * The AAX SDK is subject to commercial or open-source licensing.
9 *
10 * By using the AAX SDK, you agree to the terms of both the Avid AAX SDK License
11 * Agreement and Avid Privacy Policy.
12 *
13 * AAX SDK License: https://developer.avid.com/aax
14 * Privacy Policy: https://www.avid.com/legal/privacy-policy-statement
15 *
16 * Or: You may also use this code under the terms of the GPL v3 (see
17 * www.gnu.org/licenses).
18 *
19 * THE AAX SDK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 * EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 * DISCLAIMED.
22 */
23
29/*================================================================================================*/
30
31
32#ifndef AAXLibrary_AAX_Exception_h
33#define AAXLibrary_AAX_Exception_h
34
35#include "AAX_Assert.h"
36#include "AAX_StringUtilities.h"
37#include "AAX.h"
38
39#include <exception>
40#include <string>
41#include <set>
42
43
45#if 0
46#pragma mark -
47#pragma mark AAX::Exception
48#endif
50
51namespace AAX
52{
53 namespace Exception {
54 class Any;
55 }
56
59 inline std::string AsString(const char* inStr);
60 inline const std::string& AsString(const std::string& inStr);
61 inline const std::string& AsString(const Exception::Any& inStr);
62
63
71 namespace Exception
72 {
86 class Any
87 {
88 public:
89 virtual ~Any() {}
90
93 template <class C>
94 explicit Any(const C& inWhat)
95 : mDesc(AAX::AsString(inWhat))
96 , mFunction()
97 , mLine()
98 , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
99 {
100 }
101
104 template <class C1, class C2, class C3>
105 explicit Any(const C1& inWhat, const C2& inFunction, const C3& inLine)
106 : mDesc(AAX::AsString(inWhat))
107 , mFunction(AAX::AsString(inFunction))
108 , mLine(AAX::AsString(inLine))
109 , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
110 {
111 }
112
113 // copy constructor
114 Any(const Any& inOther)
115 : mDesc(inOther.mDesc)
116 , mFunction(inOther.mFunction)
117 , mLine(inOther.mLine)
118 , mWhat(inOther.mWhat)
119 {
120 }
121
122 // assignment operator
123 Any& operator=(const Any& inOther)
124 {
125 mDesc = inOther.mDesc;
126 mFunction = inOther.mFunction;
127 mLine = inOther.mLine;
128 mWhat = inOther.mWhat;
129 return *this;
130 }
131
134
135 public: // AAX::Exception::Any
136
137#ifndef AAX_CPP11_SUPPORT
138 // implicit conversion to std::string (mostly for AsString())
139 operator const std::string&(void) const { return mWhat; }
140#endif
141
142 const std::string& What() const { return mWhat; }
143 const std::string& Desc() const { return mDesc; }
144 const std::string& Function() const { return mFunction; }
145 const std::string& Line() const { return mLine; }
146
147 private:
148 static std::string CreateWhat(const std::string& inDesc, const std::string& inFunc, const std::string& inLine)
149 {
150 std::string whatStr(inDesc);
151 if (false == inFunc.empty()) { whatStr += (" func:" + inFunc); }
152 if (false == inLine.empty()) { whatStr += (" line:" + inLine); }
153 return whatStr;
154 }
155
156 private:
157 std::string mDesc;
158 std::string mFunction;
159 std::string mLine;
160 std::string mWhat;
161 };
162
165 class ResultError : public Any
166 {
167 public:
168 explicit ResultError(AAX_Result inWhatResult)
169 : Any(ResultError::FormatResult(inWhatResult))
170 , mResult(inWhatResult)
171 {
172 }
173
174 template <class C>
175 explicit ResultError(AAX_Result inWhatResult, const C& inFunction)
176 : Any(ResultError::FormatResult(inWhatResult), inFunction, (const char*)NULL)
177 , mResult(inWhatResult)
178 {
179 }
180
181 template <class C1, class C2>
182 explicit ResultError(AAX_Result inWhatResult, const C1& inFunction, const C2& inLine)
183 : Any(ResultError::FormatResult(inWhatResult), inFunction, inLine)
184 , mResult(inWhatResult)
185 {
186 }
187
188 // copy constructor
189 ResultError(const ResultError& inOther)
190 : Any(inOther)
191 , mResult(inOther.mResult)
192 {
193 }
194
195 static std::string FormatResult(AAX_Result inResult)
196 {
197 return std::string(AAX::AsStringResult(inResult) + " (" + AAX::AsStringInt32((int32_t)inResult) + ")");
198 }
199
200 AAX_Result Result() const { return mResult; }
201
202 private:
203 AAX_Result mResult;
204 };
205 }
206
207 std::string AsString(const char* inStr)
208 {
209 return inStr ? std::string(inStr) : std::string();
210 }
211
212 const std::string& AsString(const std::string& inStr)
213 {
214 return inStr;
215 }
216
217 const std::string& AsString(const Exception::Any& inStr)
218 {
219 return inStr.What();
220 }
221}
222
223
225#if 0
226#pragma mark -
227#endif
229
343{
344public:
346
347 /* non-virtual destructor */ ~AAX_CheckedResult() {}
348
351 : mCurResult(AAX_SUCCESS)
352 , mLastError(AAX_SUCCESS)
353 , mAcceptedResults()
354 {
355 Initialize();
356 }
357
361 : mCurResult(inResult)
362 , mLastError(AAX_SUCCESS)
363 , mAcceptedResults()
364 {
365 Initialize();
366 Check();
367 }
368
375 {
376 mAcceptedResults.insert(inResult);
377 }
378
380 {
381 mAcceptedResults.clear();
382 mAcceptedResults.insert(AAX_SUCCESS);
383 }
384
387 {
388 mCurResult = inResult;
389 Check();
390 return *this;
391 }
392
396 {
397 return this->operator=(inResult);
398 }
399
401 operator AAX_Result() const
402 {
403 return mCurResult;
404 }
405
408 void Clear()
409 {
410 mCurResult = AAX_SUCCESS;
411 mLastError = AAX_SUCCESS;
412 }
413
417 {
418 return mLastError;
419 }
420
421private:
422 void Initialize()
423 {
425 }
426
427 void Check()
428 {
429 const AAX_Result err = mCurResult;
430 if (0 == mAcceptedResults.count(err))
431 {
433
434 // error state is now captured in ex
435 mCurResult = AAX_SUCCESS;
436 mLastError = err;
437
438 AAX_TRACE_RELEASE(kAAX_Trace_Priority_Normal, "AAX_CheckedResult - throwing %s", ex.What().c_str());
439 AAX_STACKTRACE(kAAX_Trace_Priority_Lowest, ""); // stacktrace is only printed for debug plug-in builds
440 throw ex;
441 }
442 }
443
444private:
445 AAX_Result mCurResult;
446 AAX_Result mLastError;
447 std::set<AAX_Result> mAcceptedResults;
448};
449
450
452#if 0
453#pragma mark -
454#pragma mark AAX exception macros
455#endif
457
473#define AAX_SWALLOW(...) \
474 try { if(true) { ( __VA_ARGS__ ); } } \
475 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
476 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()); \
477 } do {} while (false)
478
501#define AAX_SWALLOW_MULT(...) \
502try { if(true) { __VA_ARGS__ } } \
503catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
504AAX_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()); \
505} do {} while (false)
506
528#define AAX_CAPTURE(X, ...) \
529try { if(true) { ( __VA_ARGS__ ); } } \
530catch (const AAX::Exception::ResultError& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
531AAX_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()); \
532(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
533} do {} while (false)
534
566#define AAX_CAPTURE_MULT(X, ...) \
567try { if(true) { __VA_ARGS__ } } \
568catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
569AAX_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()); \
570(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
571} do {} while (false)
572
573
575#if 0
576#pragma mark -
577#endif
579
618{
619public:
621
623 {
624 if (0 == mNumSucceeded && 0 < mNumFailed) {
625 try {
626 // do normal logging
627 this->Check();
628 }
629 catch(...)
630 {
631 // can't throw from a destructor
632 }
633 }
634 }
635
638 {
639 if (AAX_SUCCESS == inResult)
640 {
641 ++mNumSucceeded;
642 }
643 else
644 {
645 mLastFailure = inResult;
646 ++mNumFailed;
647 }
648
649 return *this;
650 }
651
653 operator AAX_Result()
654 {
655 AAX_Result const err = this->LastFailure();
656 this->Clear();
657 return err;
658 }
659
660 void Check() const { AAX_CheckedResult tempErr(mLastFailure); }
661 void Clear() {
662 mLastFailure = AAX_SUCCESS;
663 mNumFailed = 0;
664 mNumSucceeded = 0;
665 }
666
667 AAX_Result LastFailure() const { return mLastFailure; }
668 int NumFailed() const { return mNumFailed; }
669 int NumSucceeded() const { return mNumSucceeded; }
670 int NumAttempted() const { return mNumFailed+mNumSucceeded; }
671
672private:
673 AAX_Result mLastFailure{AAX_SUCCESS};
674 int mNumFailed{0};
675 int mNumSucceeded{0};
676};
677
678#endif
@ AAX_SUCCESS
Definition: AAX_Errors.h:49
Various utility definitions for AAX.
int32_t AAX_Result
Definition: AAX.h:347
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:241
#define kAAX_Trace_Priority_Normal
Definition: AAX_Assert.h:239
#define AAX_TRACE_RELEASE(iPriority,...)
Print a trace statement to the log.
Definition: AAX_Assert.h:245
#define AAX_STACKTRACE(iPriority,...)
Print a stack trace statement to the log (debug builds only)
Definition: AAX_Assert.h:290
Definition: AAX_EnvironmentUtilities.h:72
std::string AsStringResult(AAX_Result inResult)
Definition: AAX_StringUtilities.hpp:786
std::string AsString(const char *inStr)
Definition: AAX_Exception.h:207
std::string AsStringInt32(int32_t inInt32)
Definition: AAX_StringUtilities.hpp:340
Definition: AAX_Exception.h:87
const std::string & Desc() const
Definition: AAX_Exception.h:143
const std::string & Function() const
Definition: AAX_Exception.h:144
Any(const C &inWhat)
Definition: AAX_Exception.h:94
Any(const Any &inOther)
Definition: AAX_Exception.h:114
Any & operator=(const Any &inOther)
Definition: AAX_Exception.h:123
const std::string & What() const
Definition: AAX_Exception.h:142
Any(const C1 &inWhat, const C2 &inFunction, const C3 &inLine)
Definition: AAX_Exception.h:105
virtual ~Any()
Definition: AAX_Exception.h:89
const std::string & Line() const
Definition: AAX_Exception.h:145
Definition: AAX_Exception.h:166
ResultError(AAX_Result inWhatResult, const C1 &inFunction, const C2 &inLine)
Definition: AAX_Exception.h:182
static std::string FormatResult(AAX_Result inResult)
Definition: AAX_Exception.h:195
ResultError(const ResultError &inOther)
Definition: AAX_Exception.h:189
AAX_Result Result() const
Definition: AAX_Exception.h:200
ResultError(AAX_Result inWhatResult)
Definition: AAX_Exception.h:168
ResultError(AAX_Result inWhatResult, const C &inFunction)
Definition: AAX_Exception.h:175
Definition: AAX_Exception.h:343
AAX_CheckedResult(AAX_Result inResult)
Implicit conversion constructor from AAX_Result.
Definition: AAX_Exception.h:360
void ResetAcceptedResults()
Definition: AAX_Exception.h:379
AAX::Exception::ResultError Exception
Definition: AAX_Exception.h:345
AAX_CheckedResult & operator|=(AAX_Result inResult)
bitwise-or assignment to AAX_Result
Definition: AAX_Exception.h:395
AAX_CheckedResult()
Construct an AAX_CheckedResult in a success state.
Definition: AAX_Exception.h:350
void Clear()
Clears the current result state.
Definition: AAX_Exception.h:408
~AAX_CheckedResult()
Definition: AAX_Exception.h:347
AAX_CheckedResult & operator=(AAX_Result inResult)
Assignment to AAX_Result.
Definition: AAX_Exception.h:386
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:416
void AddAcceptedResult(AAX_Result inResult)
Add an expected result which will not result in a throw.
Definition: AAX_Exception.h:374
Definition: AAX_Exception.h:618
AAX_Result LastFailure() const
Definition: AAX_Exception.h:667
~AAX_AggregateResult()
Definition: AAX_Exception.h:622
AAX_AggregateResult & operator=(AAX_Result inResult)
Overloaded operator=() for conversion from AAX_Result.
Definition: AAX_Exception.h:637
void Check() const
Definition: AAX_Exception.h:660
void Clear()
Definition: AAX_Exception.h:661
int NumSucceeded() const
Definition: AAX_Exception.h:669
AAX_AggregateResult()=default
int NumFailed() const
Definition: AAX_Exception.h:668
int NumAttempted() const
Definition: AAX_Exception.h:670