| AAX SDK 2.6.1 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 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
38 namespace 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
110 AAX_DEFAULT_MOVE_CTOR(Any);
111 AAX_DEFAULT_MOVE_OPER(Any);
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
313 class AAX_CheckedResult
314{
315 public:
316 typedef AAX::Exception::ResultError Exception;
317
318 /* non-virtual destructor */ ~AAX_CheckedResult() {}
319
321 AAX_CheckedResult()
322 : mCurResult(AAX_SUCCESS)
323 , mLastError(AAX_SUCCESS)
324 , mAcceptedResults()
325 {
326 Initialize();
327 }
328
331 AAX_CheckedResult(AAX_Result inResult)
332 : mCurResult(inResult)
333 , mLastError(AAX_SUCCESS)
334 , mAcceptedResults()
335 {
336 Initialize();
337 Check();
338 }
339
345 void AddAcceptedResult(AAX_Result inResult)
346 {
347 mAcceptedResults.insert(inResult);
348 }
349
350 void ResetAcceptedResults()
351 {
352 mAcceptedResults.clear();
353 mAcceptedResults.insert(AAX_SUCCESS);
354 }
355
357 AAX_CheckedResult& operator=(AAX_Result inResult)
358 {
359 mCurResult = inResult;
360 Check();
361 return *this;
362 }
363
366 AAX_CheckedResult& operator|=(AAX_Result inResult)
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
387 AAX_Result LastError() const
388 {
389 return mLastError;
390 }
391
392 private:
393 void Initialize()
394 {
395 ResetAcceptedResults();
396 }
397
398 void Check()
399 {
400 const AAX_Result err = mCurResult;
401 if (0 == mAcceptedResults.count(err))
402 {
403 AAX_CheckedResult::Exception ex(err);
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
415 private:
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(...) \
473 try { if(true) { __VA_ARGS__ } } \
474 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
475 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()); \
476} do {} while (false)
477
499#define AAX_CAPTURE(X, ...) \
500 try { if(true) { ( __VA_ARGS__ ); } } \
501 catch (const AAX::Exception::ResultError& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
502 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()); \
503(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
504} do {} while (false)
505
537#define AAX_CAPTURE_MULT(X, ...) \
538 try { if(true) { __VA_ARGS__ } } \
539 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
540 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()); \
541(X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
542} do {} while (false)
543
544
546#if 0
547#pragma mark -
548#endif
550
588 class AAX_AggregateResult
589{
590 public:
591 AAX_AggregateResult() = default;
592
593 ~AAX_AggregateResult()
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
608 AAX_AggregateResult& operator=(AAX_Result inResult)
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
643 private:
644 AAX_Result mLastFailure{AAX_SUCCESS};
645 int mNumFailed{0};
646 int mNumSucceeded{0};
647};
648
649#endif
AAX_SUCCESS
@ AAX_SUCCESS
Definition: AAX_Errors.h:36
AAX.h
Various utility definitions for AAX.
AAX_Result
int32_t AAX_Result
Definition: AAX.h:334
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:228
kAAX_Trace_Priority_Normal
#define kAAX_Trace_Priority_Normal
Definition: AAX_Assert.h:226
AAX_TRACE_RELEASE
#define AAX_TRACE_RELEASE(iPriority,...)
Print a trace statement to the log.
Definition: AAX_Assert.h:232
AAX_STACKTRACE
#define AAX_STACKTRACE(iPriority,...)
Print a stack trace statement to the log (debug builds only)
Definition: AAX_Assert.h:277
AAX
Definition: AAX_EnvironmentUtilities.h:59
AAX::AsStringResult
std::string AsStringResult(AAX_Result inResult)
Definition: AAX_StringUtilities.hpp:773
AAX::AsString
std::string AsString(const char *inStr)
Definition: AAX_Exception.h:178
AAX::AsStringInt32
std::string AsStringInt32(int32_t inInt32)
Definition: AAX_StringUtilities.hpp:327
AAX::Exception::Any
Definition: AAX_Exception.h:74
AAX::Exception::Any::Desc
const std::string & Desc() const
Definition: AAX_Exception.h:121
AAX::Exception::Any::Function
const std::string & Function() const
Definition: AAX_Exception.h:122
AAX::Exception::Any::Any
Any(const C &inWhat)
Definition: AAX_Exception.h:81
AAX::Exception::Any::AAX_DEFAULT_MOVE_OPER
AAX_DEFAULT_MOVE_OPER(Any)
AAX::Exception::Any::operator=
Any & operator=(const Any &inOther)
Definition: AAX_Exception.h:101
AAX::Exception::Any::What
const std::string & What() const
Definition: AAX_Exception.h:120
AAX::Exception::Any::Any
Any(const C1 &inWhat, const C2 &inFunction, const C3 &inLine)
Definition: AAX_Exception.h:92
AAX::Exception::Any::AAX_DEFAULT_MOVE_CTOR
AAX_DEFAULT_MOVE_CTOR(Any)
AAX::Exception::Any::~Any
virtual ~Any()
Definition: AAX_Exception.h:76
AAX::Exception::Any::Line
const std::string & Line() const
Definition: AAX_Exception.h:123
AAX::Exception::ResultError
Definition: AAX_Exception.h:144
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult, const C1 &inFunction, const C2 &inLine)
Definition: AAX_Exception.h:160
AAX::Exception::ResultError::FormatResult
static std::string FormatResult(AAX_Result inResult)
Definition: AAX_Exception.h:166
AAX::Exception::ResultError::Result
AAX_Result Result() const
Definition: AAX_Exception.h:171
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult)
Definition: AAX_Exception.h:146
AAX::Exception::ResultError::ResultError
ResultError(AAX_Result inWhatResult, const C &inFunction)
Definition: AAX_Exception.h:153
AAX_CheckedResult
Definition: AAX_Exception.h:314
AAX_CheckedResult::AAX_CheckedResult
AAX_CheckedResult(AAX_Result inResult)
Implicit conversion constructor from AAX_Result.
Definition: AAX_Exception.h:331
AAX_CheckedResult::ResetAcceptedResults
void ResetAcceptedResults()
Definition: AAX_Exception.h:350
AAX_CheckedResult::Exception
AAX::Exception::ResultError Exception
Definition: AAX_Exception.h:316
AAX_CheckedResult::operator|=
AAX_CheckedResult & operator|=(AAX_Result inResult)
bitwise-or assignment to AAX_Result
Definition: AAX_Exception.h:366
AAX_CheckedResult::AAX_CheckedResult
AAX_CheckedResult()
Construct an AAX_CheckedResult in a success state.
Definition: AAX_Exception.h:321
AAX_CheckedResult::Clear
void Clear()
Clears the current result state.
Definition: AAX_Exception.h:379
AAX_CheckedResult::~AAX_CheckedResult
~AAX_CheckedResult()
Definition: AAX_Exception.h:318
AAX_CheckedResult::operator=
AAX_CheckedResult & operator=(AAX_Result inResult)
Assignment to AAX_Result.
Definition: AAX_Exception.h:357
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:387
AAX_CheckedResult::AddAcceptedResult
void AddAcceptedResult(AAX_Result inResult)
Add an expected result which will not result in a throw.
Definition: AAX_Exception.h:345
AAX_AggregateResult
Definition: AAX_Exception.h:589
AAX_AggregateResult::LastFailure
AAX_Result LastFailure() const
Definition: AAX_Exception.h:638
AAX_AggregateResult::~AAX_AggregateResult
~AAX_AggregateResult()
Definition: AAX_Exception.h:593
AAX_AggregateResult::operator=
AAX_AggregateResult & operator=(AAX_Result inResult)
Overloaded operator=() for conversion from AAX_Result.
Definition: AAX_Exception.h:608
AAX_AggregateResult::Check
void Check() const
Definition: AAX_Exception.h:631
AAX_AggregateResult::Clear
void Clear()
Definition: AAX_Exception.h:632
AAX_AggregateResult::NumSucceeded
int NumSucceeded() const
Definition: AAX_Exception.h:640
AAX_AggregateResult::AAX_AggregateResult
AAX_AggregateResult()=default
AAX_AggregateResult::NumFailed
int NumFailed() const
Definition: AAX_Exception.h:639
AAX_AggregateResult::NumAttempted
int NumAttempted() const
Definition: AAX_Exception.h:641