AAX SDK  2.4.1
Avid Audio Extensions Development Kit
AAX_Exception.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  * Copyright 2016-2017 by Avid Technology, Inc.
4  * All rights reserved.
5  *
6  * CONFIDENTIAL: This document contains confidential information. Do not
7  * read or examine this document unless you are an Avid Technology employee
8  * or have signed a non-disclosure agreement with Avid Technology which protects
9  * the confidentiality of this document. DO NOT DISCLOSE ANY INFORMATION
10  * CONTAINED IN THIS DOCUMENT TO ANY THIRD-PARTY WITHOUT THE PRIOR WRITTEN CONSENT
11  * OF Avid Technology, INC.
12  */
13 
19 /*================================================================================================*/
20 
21 
22 #ifndef AAXLibrary_AAX_Exception_h
23 #define AAXLibrary_AAX_Exception_h
24 
25 #include "AAX_Assert.h"
26 #include "AAX_StringUtilities.h"
27 #include "AAX.h"
28 
29 #include <exception>
30 #include <string>
31 #include <set>
32 
33 
35 #if 0
36 #pragma mark -
37 #pragma mark AAX::Exception
38 #endif
40 
41 namespace AAX
42 {
43  namespace Exception {
44  class Any;
45  }
46 
49  inline std::string AsString(const char* inStr);
50  inline const std::string& AsString(const std::string& inStr);
51  inline const std::string& AsString(const Exception::Any& inStr);
52 
53 
61  namespace Exception
62  {
76  class Any
77  {
78  public:
79  virtual ~Any() {}
80 
83  template <class C>
84  explicit Any(const C& inWhat)
85  : mDesc(AAX::AsString(inWhat))
86  , mFunction()
87  , mLine()
88  , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
89  {
90  }
91 
94  template <class C1, class C2, class C3>
95  explicit Any(const C1& inWhat, const C2& inFunction, const C3& inLine)
96  : mDesc(AAX::AsString(inWhat))
97  , mFunction(AAX::AsString(inFunction))
98  , mLine(AAX::AsString(inLine))
99  , mWhat(AAX::Exception::Any::CreateWhat(mDesc, mFunction, mLine))
100  {
101  }
102 
103  // assignment operator
104  Any& operator=(const Any& inOther)
105  {
106  mDesc = inOther.mDesc;
107  mFunction = inOther.mFunction;
108  mLine = inOther.mLine;
109  mWhat = inOther.mWhat;
110  return *this;
111  }
112 
115 
116  public: // AAX::Exception::Any
117 
118 #ifndef AAX_CPP11_SUPPORT
119  // implicit conversion to std::string (mostly for AsString())
120  operator const std::string&(void) const { return mWhat; }
121 #endif
122 
123  const std::string& What() const { return mWhat; }
124  const std::string& Desc() const { return mDesc; }
125  const std::string& Function() const { return mFunction; }
126  const std::string& Line() const { return mLine; }
127 
128  private:
129  static std::string CreateWhat(const std::string& inDesc, const std::string& inFunc, const std::string& inLine)
130  {
131  std::string whatStr(inDesc);
132  if (false == inFunc.empty()) { whatStr += (" func:" + inFunc); }
133  if (false == inLine.empty()) { whatStr += (" line:" + inLine); }
134  return whatStr;
135  }
136 
137  private:
138  std::string mDesc;
139  std::string mFunction;
140  std::string mLine;
141  std::string mWhat;
142  };
143 
146  class ResultError : public Any
147  {
148  public:
149  explicit ResultError(AAX_Result inWhatResult)
150  : Any(ResultError::FormatResult(inWhatResult))
151  , mResult(inWhatResult)
152  {
153  }
154 
155  template <class C>
156  explicit ResultError(AAX_Result inWhatResult, const C& inFunction)
157  : Any(ResultError::FormatResult(inWhatResult), inFunction, (const char*)NULL)
158  , mResult(inWhatResult)
159  {
160  }
161 
162  template <class C1, class C2>
163  explicit ResultError(AAX_Result inWhatResult, const C1& inFunction, const C2& inLine)
164  : Any(ResultError::FormatResult(inWhatResult), inFunction, inLine)
165  , mResult(inWhatResult)
166  {
167  }
168 
169  static std::string FormatResult(AAX_Result inResult)
170  {
171  return std::string(AAX::AsStringResult(inResult) + " (" + AAX::AsStringInt32((int32_t)inResult) + ")");
172  }
173 
174  AAX_Result Result() const { return mResult; }
175 
176  private:
177  AAX_Result mResult;
178  };
179  }
180 
181  std::string AsString(const char* inStr)
182  {
183  return inStr ? std::string(inStr) : std::string();
184  }
185 
186  const std::string& AsString(const std::string& inStr)
187  {
188  return inStr;
189  }
190 
191  const std::string& AsString(const Exception::Any& inStr)
192  {
193  return inStr.What();
194  }
195 }
196 
197 
199 #if 0
200 #pragma mark -
201 #endif
203 
317 {
318 public:
320 
321  /* non-virtual destructor */ ~AAX_CheckedResult() {}
322 
325  : mCurResult(AAX_SUCCESS)
326  , mLastError(AAX_SUCCESS)
327  , mAcceptedResults()
328  {
329  Initialize();
330  }
331 
335  : mCurResult(inResult)
336  , mLastError(AAX_SUCCESS)
337  , mAcceptedResults()
338  {
339  Initialize();
340  Check();
341  }
342 
349  {
350  mAcceptedResults.insert(inResult);
351  }
352 
354  {
355  mAcceptedResults.clear();
356  mAcceptedResults.insert(AAX_SUCCESS);
357  }
358 
361  {
362  mCurResult = inResult;
363  Check();
364  return *this;
365  }
366 
370  {
371  return this->operator=(inResult);
372  }
373 
375  operator AAX_Result() const
376  {
377  return mCurResult;
378  }
379 
382  void Clear()
383  {
384  mCurResult = AAX_SUCCESS;
385  mLastError = AAX_SUCCESS;
386  }
387 
391  {
392  return mLastError;
393  }
394 
395 private:
396  void Initialize()
397  {
399  }
400 
401  void Check()
402  {
403  const AAX_Result err = mCurResult;
404  if (0 == mAcceptedResults.count(err))
405  {
407 
408  // error state is now captured in ex
409  mCurResult = AAX_SUCCESS;
410  mLastError = err;
411 
412  AAX_TRACE_RELEASE(kAAX_Trace_Priority_Normal, "AAX_CheckedResult - throwing %s", ex.What().c_str());
413  AAX_STACKTRACE(kAAX_Trace_Priority_Lowest, ""); // stacktrace is only printed for debug plug-in builds
414  throw ex;
415  }
416  }
417 
418 private:
419  AAX_Result mCurResult;
420  AAX_Result mLastError;
421  std::set<AAX_Result> mAcceptedResults;
422 };
423 
424 
426 #if 0
427 #pragma mark -
428 #pragma mark AAX exception macros
429 #endif
431 
447 #define AAX_SWALLOW(...) \
448  try { if(true) { ( __VA_ARGS__ ); } } \
449  catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
450  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()); \
451  } do {} while (false)
452 
475 #define AAX_SWALLOW_MULT(...) \
476 try { if(true) { __VA_ARGS__ } } \
477 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
478 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()); \
479 } do {} while (false)
480 
502 #define AAX_CAPTURE(X, ...) \
503 try { if(true) { ( __VA_ARGS__ ); } } \
504 catch (const AAX::Exception::ResultError& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
505 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()); \
506 (X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
507 } do {} while (false)
508 
540 #define AAX_CAPTURE_MULT(X, ...) \
541 try { if(true) { __VA_ARGS__ } } \
542 catch (const AAX_CheckedResult::Exception& AAX_PREPROCESSOR_CONCAT(ex,__LINE__)) { \
543 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()); \
544 (X) = AAX_PREPROCESSOR_CONCAT(ex,__LINE__).Result(); \
545 } do {} while (false)
546 
547 
549 #if 0
550 #pragma mark -
551 #endif
553 
592 {
593 public:
595  : mLastFailure(AAX_SUCCESS)
596  , mNumFailed(0)
597  , mNumSucceeded(0)
598  {
599  }
600 
602  {
603  if (0 == mNumSucceeded && 0 < mNumFailed)
604  AAX_CheckedResult tempErr(mLastFailure);
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 
623  AAX_Result LastFailure() const { return mLastFailure; }
624  int NumFailed() const { return mNumFailed; }
625  int NumSucceeded() const { return mNumSucceeded; }
626  int NumAttempted() const { return mNumFailed+mNumSucceeded; }
627 
628 private:
629  AAX_Result mLastFailure;
630  int mNumFailed;
631  int mNumSucceeded;
632 };
633 
634 #endif
Various utility definitions for AAX.
int32_t AAX_Result
Definition: AAX.h:337
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
@ AAX_SUCCESS
Definition: AAX_Errors.h:39
Various string utility definitions for AAX Native.
Definition: AAX_Exception.h:42
std::string AsStringResult(AAX_Result inResult)
Definition: AAX_StringUtilities.hpp:536
std::string AsString(const char *inStr)
Definition: AAX_Exception.h:181
std::string AsStringInt32(int32_t inInt32)
Definition: AAX_StringUtilities.hpp:294
Definition: AAX_Exception.h:77
const std::string & Function() const
Definition: AAX_Exception.h:125
Any & operator=(const Any &inOther)
Definition: AAX_Exception.h:104
Any(const C &inWhat)
Definition: AAX_Exception.h:84
const std::string & What() const
Definition: AAX_Exception.h:123
const std::string & Desc() const
Definition: AAX_Exception.h:124
Any(const C1 &inWhat, const C2 &inFunction, const C3 &inLine)
Definition: AAX_Exception.h:95
virtual ~Any()
Definition: AAX_Exception.h:79
const std::string & Line() const
Definition: AAX_Exception.h:126
Definition: AAX_Exception.h:147
ResultError(AAX_Result inWhatResult, const C1 &inFunction, const C2 &inLine)
Definition: AAX_Exception.h:163
static std::string FormatResult(AAX_Result inResult)
Definition: AAX_Exception.h:169
AAX_Result Result() const
Definition: AAX_Exception.h:174
ResultError(AAX_Result inWhatResult)
Definition: AAX_Exception.h:149
ResultError(AAX_Result inWhatResult, const C &inFunction)
Definition: AAX_Exception.h:156
Definition: AAX_Exception.h:317
AAX_CheckedResult(AAX_Result inResult)
Implicit conversion constructor from AAX_Result.
Definition: AAX_Exception.h:334
void ResetAcceptedResults()
Definition: AAX_Exception.h:353
AAX::Exception::ResultError Exception
Definition: AAX_Exception.h:319
AAX_CheckedResult & operator|=(AAX_Result inResult)
bitwise-or assignment to AAX_Result
Definition: AAX_Exception.h:369
AAX_CheckedResult()
Construct an AAX_CheckedResult in a success state.
Definition: AAX_Exception.h:324
void Clear()
Clears the current result state.
Definition: AAX_Exception.h:382
~AAX_CheckedResult()
Definition: AAX_Exception.h:321
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:390
void AddAcceptedResult(AAX_Result inResult)
Add an expected result which will not result in a throw.
Definition: AAX_Exception.h:348
AAX_CheckedResult & operator=(AAX_Result inResult)
Assignment to AAX_Result.
Definition: AAX_Exception.h:360
Definition: AAX_Exception.h:592
AAX_Result LastFailure() const
Definition: AAX_Exception.h:623
~AAX_AggregateResult()
Definition: AAX_Exception.h:601
AAX_AggregateResult & operator=(AAX_Result inResult)
Overloaded operator=() for conversion from AAX_Result.
Definition: AAX_Exception.h:608
AAX_AggregateResult()
Definition: AAX_Exception.h:594
int NumSucceeded() const
Definition: AAX_Exception.h:625
int NumFailed() const
Definition: AAX_Exception.h:624
int NumAttempted() const
Definition: AAX_Exception.h:626