-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ab950a
commit c458482
Showing
5 changed files
with
207 additions
and
134 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
modules/common/chowdsp_core/Types/chowdsp_TypeHasCheckers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#pragma once | ||
|
||
/** | ||
* Creates a constexpr bool that checks if a class has the given static method. | ||
* | ||
* Usage: | ||
* @code {.cpp} | ||
* CHOWDSP_CHECK_HAS_STATIC_METHOD(HasToString, toString, std::declval<int>()) | ||
* static_assert(HasToString<MyClass>, "MyClass must have a static toString(int) method!"); | ||
* @endcode | ||
*/ | ||
#define CHOWDSP_CHECK_HAS_STATIC_METHOD(Name, Method, ...) \ | ||
template <typename _T> \ | ||
class Test_##Name \ | ||
{ \ | ||
using No = char; \ | ||
using Yes = long; \ | ||
static_assert (sizeof (No) != sizeof (Yes), "Yes and No have the same size on this platform, undefined behaviour will ensue!"); \ | ||
\ | ||
template <typename C> \ | ||
static Yes test (decltype (C::Method (__VA_ARGS__))*); \ | ||
\ | ||
template <typename C> \ | ||
static No test (...); \ | ||
\ | ||
public: \ | ||
enum \ | ||
{ \ | ||
value = sizeof (test<_T> (nullptr)) == sizeof (Yes) \ | ||
}; \ | ||
}; \ | ||
template <typename _T> \ | ||
static constexpr bool Name = Test_##Name<_T>::value; | ||
|
||
/** | ||
* Creates a constexpr bool that checks if a class has the given non-static method. | ||
* | ||
* Usage: | ||
* @code {.cpp} | ||
* CHOWDSP_CHECK_HAS_METHOD(HasGetAtIndex, getAtIndex, std::declval<int>()) | ||
* static_assert(HasGetAtIndex<MyClass>, "MyClass must have getAtIndex(int) method!"); | ||
* @endcode | ||
*/ | ||
#define CHOWDSP_CHECK_HAS_METHOD(Name, Method, ...) \ | ||
template <typename _T> \ | ||
class Test_##Name \ | ||
{ \ | ||
using No = char; \ | ||
using Yes = long; \ | ||
static_assert (sizeof (No) != sizeof (Yes), "Yes and No have the same size on this platform, undefined behaviour will ensue!"); \ | ||
\ | ||
template <typename C> \ | ||
static Yes test (decltype (std::declval<C>().Method (__VA_ARGS__))*); \ | ||
\ | ||
template <typename C> \ | ||
static No test (...); \ | ||
\ | ||
public: \ | ||
enum \ | ||
{ \ | ||
value = sizeof (test<_T> (nullptr)) == sizeof (Yes) \ | ||
}; \ | ||
}; \ | ||
template <typename _T> \ | ||
static constexpr bool Name = Test_##Name<_T>::value; | ||
|
||
/** | ||
* Creates a constexpr bool that checks if a class has the given static member variable. | ||
* | ||
* Usage: | ||
* @code {.cpp} | ||
* CHOWDSP_CHECK_HAS_STATIC_MEMBER(HasName, name) | ||
* static_assert(HasName<MyClass>, "MyClass must have a static name member!"); | ||
* @endcode | ||
*/ | ||
#define CHOWDSP_CHECK_HAS_STATIC_MEMBER(Name, Member) \ | ||
template <typename _T> \ | ||
class Test_##Name \ | ||
{ \ | ||
using No = char; \ | ||
using Yes = long; \ | ||
static_assert (sizeof (No) != sizeof (Yes), "Yes and No have the same size on this platform, undefined behaviour will ensue!"); \ | ||
\ | ||
template <typename C> \ | ||
static std::enable_if_t<std::is_member_pointer_v<decltype (&C::Member)>, No> test (decltype (C::Member)*); \ | ||
\ | ||
template <typename C> \ | ||
static std::enable_if_t<! std::is_member_pointer_v<decltype (&C::Member)>, Yes> test (decltype (C::Member)*); \ | ||
\ | ||
template <typename C> \ | ||
static No test (...); \ | ||
\ | ||
public: \ | ||
enum \ | ||
{ \ | ||
value = sizeof (test<_T> (nullptr)) == sizeof (Yes) \ | ||
}; \ | ||
}; \ | ||
template <typename _T> \ | ||
static constexpr bool Name = Test_##Name<_T>::value; | ||
|
||
/** | ||
* Creates a constexpr bool that checks if a class has the given non-static member variable. | ||
* | ||
* Usage: | ||
* @code {.cpp} | ||
* CHOWDSP_CHECK_HAS_MEMBER(HasName, name) | ||
* static_assert(HasName<MyClass>, "MyClass must have `name` member variable!"); | ||
* @endcode | ||
*/ | ||
#define CHOWDSP_CHECK_HAS_MEMBER(Name, Member) \ | ||
template <typename _T> \ | ||
class Test_##Name \ | ||
{ \ | ||
using No = char; \ | ||
using Yes = long; \ | ||
static_assert (sizeof (No) != sizeof (Yes), "Yes and No have the same size on this platform, undefined behaviour will ensue!"); \ | ||
\ | ||
template <typename C> \ | ||
static std::enable_if_t<std::is_member_pointer_v<decltype (&C::Member)>, Yes> test (decltype (std::declval<C>().Member)*); \ | ||
\ | ||
template <typename C> \ | ||
static std::enable_if_t<! std::is_member_pointer_v<decltype (&C::Member)>, No> test (decltype (std::declval<C>().Member)*); \ | ||
\ | ||
template <typename C> \ | ||
static No test (...); \ | ||
\ | ||
public: \ | ||
enum \ | ||
{ \ | ||
value = sizeof (test<_T> (nullptr)) == sizeof (Yes) \ | ||
}; \ | ||
}; \ | ||
template <typename _T> \ | ||
static constexpr bool Name = Test_##Name<_T>::value; |
Oops, something went wrong.