| AAX SDK 2.8.0 Avid Audio Extensions Development Kit

- Welcome
- Manual
- Classes
- Files
- Lists
- Support

- File List
- File Members

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
51 namespace 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
132 AAX_DEFAULT_MOVE_CTOR(Any);
133 AAX_DEFAULT_MOVE_OPER(Any);
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
342 class AAX_CheckedResult
343{
344 public:
345 typedef AAX::Exception::ResultError Exception;
346
347 /* non-virtual destructor */ ~AAX_CheckedResult() {}
348
350 AAX_CheckedResult()
351 : mCurResult(AAX_SUCCESS)
352 , mLastError(AAX_SUCCESS)
353 , mAcceptedResults()
354 {
355 Initialize();
356 }
357
360 AAX_CheckedResult(AAX_Result inResult)
361 : mCurResult(inResult)
362 , mLastError(AAX_SUCCESS)
363 , mAcceptedResults()
364 {
365 Initialize();
366 Check();
367 }
368
374 void AddAcceptedResult(AAX_Result inResult)
375 {
376 mAcceptedResults.insert(inResult);
377 }
378
379 void ResetAcceptedResults()
380 {
381 mAcceptedResults.clear();
382 mAcceptedResults.insert(AAX_SUCCESS);
383 }
384
386 AAX_CheckedResult& operator=(AAX_Result inResult)
387 {
388 mCurResult = inResult;
389 Check();
390 return *this;
391 }
392
395 AAX_CheckedResult& operator|=(AAX_Result inResult)
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
416 AAX_Result LastError() const
417 {
418 return mLastError;
419 }
420
421 private:
422 void Initialize()
423 {
424 ResetAcceptedResults();
425 }
426
427 void Check()
428 {
429 const AAX_Result err = mCurResult;
430 if (0 == mAcceptedResults.count(err))
431 {
432 AAX_CheckedResult::Exception ex(err);
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
444 private:
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(...) \
502 try { if(true) { __VA_ARGS__ } } \
503 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
504 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()); \
505} do {} while (false)
506
528#define AAX_CAPTURE(X, ...) \
529 try { if(true) { ( __VA_ARGS__ ); } } \
530 catch (const AAX::Exception::ResultError& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
531 AAX_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, ...) \
567 try { if(true) { __VA_ARGS__ } } \
568 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
569 AAX_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
617 class AAX_AggregateResult
618{
619 public:
620 AAX_AggregateResult() = default;
621
622 ~AAX_AggregateResult()
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
637 AAX_AggregateResult& operator=(AAX_Result inResult)
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
672 private:
673 AAX_Result mLastFailure{AAX_SUCCESS};
674 int mNumFailed{0};
675 int mNumSucceeded{0};
676};
677
678#endif
AAX_SUCCESS
@ AAX_SUCCESS
Definition: AAX_Errors.h:49
AAX.h
Various utility definitions for AAX.
AAX_Result
int32_t AAX_Result
Definition: AAX.h:347
AAX_StringUtilities.h
Various string utility definitions for AAX Native.
AAX_Assert.h
Declarations for cross-platform AAX_ASSERT, AAX_TRACE and related facilities.
kAAX_Trace_Priority_Lowest
#define kAAX_Trace_Priority_Lowest
Definition: AAX_Assert.h:241
kAAX_Trace_Priority_Normal
#define kAAX_Trace_Priority_Normal
Definition: AAX_Assert.h:239
AAX_TRACE_RELEASE
#define AAX_TRACE_RELEASE(iPriority,...)
Print a trace statement to the log.
Definition: AAX_Assert.h:245
AAX_STACKTRACE
#define AAX_STACKTRACE(iPriority,...)
Print a stack trace statement to the log (debug builds only)
Definition: AAX_Assert.h:290
AAX
Definition: AAX_EnvironmentUtilities.h:72
AAX::AsStringResult
std::string AsStringResult(AAX_Result inResult)
Definition: AAX_StringUtilities.hpp:786
AAX::AsString
std::string AsString(const char *inStr)
Definition: AAX_Exception.h:207
AAX::AsStringInt32
std::string AsStringInt32(int32_t inInt32)
Definition: AAX_StringUtilities.hpp:340
AAX::Exception::Any
Definition: AAX_Exception.h:87
AAX::Exception::Any::Desc
const std::string & Desc() const
Definition: AAX_Exception.h:143
AAX::Exception::Any::Function
const std::string & Function() const
Definition: AAX_Exception.h:144
AAX::Exception::Any::Any
Any(const C &inWhat)
Definition: AAX_Exception.h:94
AAX::Exception::Any::AAX_DEFAULT_MOVE_OPER
AAX_DEFAULT_MOVE_OPER(Any)
AAX::Exception::Any::Any
Any(const Any &inOther)
Definition: AAX_Exception.h:114
AAX::Exception::Any::operator=
Any & operator=(const Any &inOther)
Definition: AAX_Exception.h:123
AAX::Exception::Any::What
const std::string & What() const
Definition: AAX_Exception.h:142
AAX::Exception::Any::Any
Any(const C1 &inWhat, const C2 &inFunction, const C3 &inLine)
Definition: AAX_Exception.h:105
AAX::Exception::Any::AAX_DEFAULT_MOVE_CTOR
AAX_DEFAULT_MOVE_CTOR(Any)
AAX::Exception::Any::~Any
virtual ~Any()
Definition: AAX_Exception.h:89
AAX::Exception::Any::Line
const std::string & Line() const
Definition: AAX_Exception.h:145
AAX::Exception::ResultError
Definition: AAX_Exception.h:166
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult, const C1 &inFunction, const C2 &inLine)
Definition: AAX_Exception.h:182
AAX::Exception::ResultError::FormatResult
static std::string FormatResult(AAX_Result inResult)
Definition: AAX_Exception.h:195
AAX::Exception::ResultError::ResultError
ResultError(const ResultError &inOther)
Definition: AAX_Exception.h:189
AAX::Exception::ResultError::Result
AAX_Result Result() const
Definition: AAX_Exception.h:200
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult)
Definition: AAX_Exception.h:168
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult, const C &inFunction)
Definition: AAX_Exception.h:175
AAX_CheckedResult
Definition: AAX_Exception.h:343
AAX_CheckedResult::AAX_CheckedResult
AAX_CheckedResult(AAX_Result inResult)
Implicit conversion constructor from AAX_Result.
Definition: AAX_Exception.h:360
AAX_CheckedResult::ResetAcceptedResults
void ResetAcceptedResults()
Definition: AAX_Exception.h:379
AAX_CheckedResult::Exception
AAX::Exception::ResultError Exception
Definition: AAX_Exception.h:345
AAX_CheckedResult::operator|=
AAX_CheckedResult & operator|=(AAX_Result inResult)
bitwise-or assignment to AAX_Result
Definition: AAX_Exception.h:395
AAX_CheckedResult::AAX_CheckedResult
AAX_CheckedResult()
Construct an AAX_CheckedResult in a success state.
Definition: AAX_Exception.h:350
AAX_CheckedResult::Clear
void Clear()
Clears the current result state.
Definition: AAX_Exception.h:408
AAX_CheckedResult::~AAX_CheckedResult
~AAX_CheckedResult()
Definition: AAX_Exception.h:347
AAX_CheckedResult::operator=
AAX_CheckedResult & operator=(AAX_Result inResult)
Assignment to AAX_Result.
Definition: AAX_Exception.h:386
AAX_CheckedResult::LastError
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
AAX_CheckedResult::AddAcceptedResult
void AddAcceptedResult(AAX_Result inResult)
Add an expected result which will not result in a throw.
Definition: AAX_Exception.h:374
AAX_AggregateResult
Definition: AAX_Exception.h:618
AAX_AggregateResult::LastFailure
AAX_Result LastFailure() const
Definition: AAX_Exception.h:667
AAX_AggregateResult::~AAX_AggregateResult
~AAX_AggregateResult()
Definition: AAX_Exception.h:622
AAX_AggregateResult::operator=
AAX_AggregateResult & operator=(AAX_Result inResult)
Overloaded operator=() for conversion from AAX_Result.
Definition: AAX_Exception.h:637
AAX_AggregateResult::Check
void Check() const
Definition: AAX_Exception.h:660
AAX_AggregateResult::Clear
void Clear()
Definition: AAX_Exception.h:661
AAX_AggregateResult::NumSucceeded
int NumSucceeded() const
Definition: AAX_Exception.h:669
AAX_AggregateResult::AAX_AggregateResult
AAX_AggregateResult()=default
AAX_AggregateResult::NumFailed
int NumFailed() const
Definition: AAX_Exception.h:668
AAX_AggregateResult::NumAttempted
int NumAttempted() const
Definition: AAX_Exception.h:670