has_member.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Tencent is pleased to support the open source community by making Mars available.
  2. // Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. // Licensed under the MIT License (the "License"); you may not use this file except in
  4. // compliance with the License. You may obtain a copy of the License at
  5. // http://opensource.org/licenses/MIT
  6. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  7. // distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  8. // either express or implied. See the License for the specific language governing permissions and
  9. // limitations under the License.
  10. //============================================================================
  11. // Name : has_member.h
  12. // Author :
  13. // Version :
  14. // Copyright : Your copyright notice
  15. // Description : Hello World in C++, Ansi-style
  16. //============================================================================
  17. #ifndef COMM_HAS_MEMBER_H_
  18. #define COMM_HAS_MEMBER_H_
  19. template <bool TYPE>
  20. struct bool_type {
  21. };
  22. #define DEFINE_HAS_MEMBER_WITH_TYPE(member_name, member_type) \
  23. template <typename T>\
  24. class has_##member_name {\
  25. private:\
  26. struct yes_type { char x[1]; };\
  27. struct no_type { char x[2]; };\
  28. template <member_type (T::*)> struct tester;\
  29. template <typename U> static yes_type test(tester<&U::member_name>*);\
  30. template <typename U> static no_type test(...);\
  31. public:\
  32. static const bool value = (sizeof(test<T>(0)) == sizeof(yes_type));\
  33. };
  34. #define DEFINE_HAS_MEMBER(member_name) \
  35. template <typename T>\
  36. class has_##member_name {\
  37. private:\
  38. struct yes_type { char x[1]; };\
  39. struct no_type { char x[2]; };\
  40. template <int> struct tester;\
  41. template <typename U> static yes_type test(tester<sizeof(&U::member_name)>*);\
  42. template <typename U> static no_type test(...);\
  43. public:\
  44. static const bool value = (sizeof(test<T>(0)) == sizeof(yes_type));\
  45. };
  46. #endif