AkAssert.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #ifndef _AK_AKASSERT_H_
  21. #define _AK_AKASSERT_H_
  22. #if defined( _DEBUG ) && !(defined AK_DISABLE_ASSERTS)
  23. #ifndef AK_ENABLE_ASSERTS
  24. #define AK_ENABLE_ASSERTS
  25. #endif
  26. #endif
  27. #ifdef AK_ENABLE_ASSERTS
  28. #include <AK/SoundEngine/Common/AkSoundEngineExport.h>
  29. #ifndef AK_ASSERT_HOOK
  30. AK_CALLBACK( void, AkAssertHook)(
  31. const char * in_pszExpression, ///< Expression
  32. const char * in_pszFileName, ///< File Name
  33. int in_lineNumber ///< Line Number
  34. );
  35. #define AK_ASSERT_HOOK
  36. #endif
  37. #endif
  38. #if !defined( AKASSERT )
  39. #include <AK/SoundEngine/Common/AkTypes.h> //For AK_Fail/Success
  40. #if defined( AK_ENABLE_ASSERTS )
  41. extern AKSOUNDENGINE_API AkAssertHook g_pAssertHook;
  42. // These platforms use a built-in g_pAssertHook (and do not fall back to the regular assert macro)
  43. #define AKASSERT(Condition) ((Condition) ? ((void) 0) : g_pAssertHook( #Condition, __FILE__, __LINE__) )
  44. #define AKVERIFY AKASSERT
  45. #ifdef _DEBUG
  46. #define AKASSERTD AKASSERT
  47. #else
  48. #define AKASSERTD(Condition) ((void)0)
  49. #endif
  50. #else // defined( AK_ENABLE_ASSERTS )
  51. #define AKASSERT(Condition) do { switch ( 0 ) { case 0: break; default: if ( !( Condition ) ) {} } } while ( 0 )
  52. #define AKVERIFY(x) ((void)(x))
  53. #ifdef _DEBUG
  54. #define AKASSERTD AKASSERT
  55. #else
  56. #define AKASSERTD(Condition) ((void)0)
  57. #endif
  58. #endif // defined( AK_ENABLE_ASSERTS )
  59. #define AKASSERT_RANGE(Value, Min, Max) (AKASSERT(((Value) >= (Min)) && ((Value) <= (Max))))
  60. #define AKASSERTANDRETURN( __Expression, __ErrorCode )\
  61. if (!(__Expression))\
  62. {\
  63. AKASSERT(__Expression);\
  64. return __ErrorCode;\
  65. }\
  66. #define AKASSERTPOINTERORFAIL( __Pointer ) AKASSERTANDRETURN( __Pointer != NULL, AK_Fail )
  67. #define AKASSERTSUCCESSORRETURN( __akr ) AKASSERTANDRETURN( __akr == AK_Success, __akr )
  68. #define AKASSERTPOINTERORRETURN( __Pointer ) \
  69. if ((__Pointer) == NULL)\
  70. {\
  71. AKASSERT((__Pointer) == NULL);\
  72. return ;\
  73. }\
  74. #if defined( AK_WIN ) && ( _MSC_VER >= 1600 )
  75. // Compile-time assert
  76. #define AKSTATICASSERT( __expr__, __msg__ ) static_assert( (__expr__), (__msg__) )
  77. #else
  78. // Compile-time assert
  79. #define AKSTATICASSERT( __expr__, __msg__ ) typedef char __AKSTATICASSERT__[(__expr__)?1:-1]
  80. #endif
  81. #endif // ! defined( AKASSERT )
  82. #ifdef AK_ENABLE_ASSERTS
  83. //Do nothing. This is a dummy function, so that g_pAssertHook is never NULL.
  84. #define DEFINEDUMMYASSERTHOOK void AkAssertHookFunc( \
  85. const char* in_pszExpression,\
  86. const char* in_pszFileName,\
  87. int in_lineNumber)\
  88. {\
  89. \
  90. }\
  91. AkAssertHook g_pAssertHook = AkAssertHookFunc;
  92. #else
  93. #define DEFINEDUMMYASSERTHOOK
  94. #endif
  95. // Compile-time assert. Usage:
  96. // AkStaticAssert<[your boolean expression here]>::Assert();
  97. // Example:
  98. // AkStaticAssert<sizeof(MyStruct) == 20>::Assert(); //If you hit this, you changed the size of MyStruct!
  99. // Empty default template
  100. template <bool b>
  101. struct AkStaticAssert {};
  102. // Template specialized on true
  103. template <>
  104. struct AkStaticAssert<true>
  105. {
  106. static void Assert() {}
  107. };
  108. #endif