is_smart_ptr_base_of.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_IS_SMART_PTR_BASE_OF_H_
  32. #define _QX_IS_SMART_PTR_BASE_OF_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file is_smart_ptr_base_of.h
  38. * \author Lionel Marty
  39. * \ingroup QxTraits
  40. * \brief qx::trait::is_smart_ptr_base_of<B, D>::value : return true if B and D are smart-pointers of boost, Qt or QxOrm libraries and if (*B) is a base class of (*D), otherwise return false
  41. */
  42. #include <QxTraits/is_smart_ptr.h>
  43. #define qx_smart_ptr_base_of_test_0() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr(b, d)) == sizeof(char))
  44. #define qx_smart_ptr_base_of_test_1() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_boost_scoped_ptr), d)) == sizeof(char))
  45. #define qx_smart_ptr_base_of_test_2() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_boost_shared_ptr), d)) == sizeof(char))
  46. #define qx_smart_ptr_base_of_test_3() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_boost_weak_ptr), d)) == sizeof(char))
  47. #define qx_smart_ptr_base_of_test_4() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_boost_intrusive_ptr), d)) == sizeof(char))
  48. #define qx_smart_ptr_base_of_test_5() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_qt_shared_data_ptr), d)) == sizeof(char))
  49. #define qx_smart_ptr_base_of_test_6() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_qt_shared_ptr), d)) == sizeof(char))
  50. #define qx_smart_ptr_base_of_test_7() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_qt_weak_ptr), d)) == sizeof(char))
  51. #define qx_smart_ptr_base_of_test_8() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_qx_dao_ptr), d)) == sizeof(char))
  52. #define qx_smart_ptr_base_of_test_9() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_std_unique_ptr), d)) == sizeof(char))
  53. #define qx_smart_ptr_base_of_test_10() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_std_shared_ptr), d)) == sizeof(char))
  54. #define qx_smart_ptr_base_of_test_11() (sizeof(qx::trait::is_smart_ptr_base_of<B, D>::removeSmartPtr((* b_std_weak_ptr), d)) == sizeof(char))
  55. #define qx_smart_ptr_base_of_all_test() \
  56. qx_smart_ptr_base_of_test_1() || qx_smart_ptr_base_of_test_2() || qx_smart_ptr_base_of_test_3() || \
  57. qx_smart_ptr_base_of_test_4() || qx_smart_ptr_base_of_test_5() || qx_smart_ptr_base_of_test_6() || \
  58. qx_smart_ptr_base_of_test_7() || qx_smart_ptr_base_of_test_8() || qx_smart_ptr_base_of_test_9() || \
  59. qx_smart_ptr_base_of_test_10() || qx_smart_ptr_base_of_test_11()
  60. namespace qx {
  61. namespace trait {
  62. /*!
  63. * \ingroup QxTraits
  64. * \brief qx::trait::is_smart_ptr_base_of<B, D>::value : return true if B and D are smart-pointers of boost, Qt or QxOrm libraries and if (*B) is a base class of (*D), otherwise return false
  65. */
  66. template <typename B, typename D>
  67. class is_smart_ptr_base_of
  68. {
  69. private:
  70. #ifdef _QX_ENABLE_BOOST
  71. template <typename V, typename W>
  72. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const boost::scoped_ptr<V> &, const boost::scoped_ptr<W> &);
  73. template <typename V, typename W>
  74. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const boost::shared_ptr<V> &, const boost::shared_ptr<W> &);
  75. template <typename V, typename W>
  76. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const boost::weak_ptr<V> &, const boost::weak_ptr<W> &);
  77. template <typename V, typename W>
  78. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const boost::intrusive_ptr<V> &, const boost::intrusive_ptr<W> &);
  79. #endif // _QX_ENABLE_BOOST
  80. template <typename V, typename W>
  81. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const QSharedDataPointer<V> &, const QSharedDataPointer<W> &);
  82. template <typename V, typename W>
  83. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const QSharedPointer<V> &, const QSharedPointer<W> &);
  84. template <typename V, typename W>
  85. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const QWeakPointer<V> &, const QWeakPointer<W> &);
  86. template <typename V, typename W>
  87. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const qx::dao::ptr<V> &, const qx::dao::ptr<W> &);
  88. template <typename V, typename W>
  89. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const std::unique_ptr<V> &, const std::unique_ptr<W> &);
  90. template <typename V, typename W>
  91. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const std::shared_ptr<V> &, const std::shared_ptr<W> &);
  92. template <typename V, typename W>
  93. static typename std::conditional<std::is_base_of<V, W>::value, char, int>::type removeSmartPtr(const std::weak_ptr<V> &, const std::weak_ptr<W> &);
  94. static int removeSmartPtr(...);
  95. static B b;
  96. static D d;
  97. #ifdef _QX_ENABLE_BOOST
  98. static boost::scoped_ptr<B> * b_boost_scoped_ptr;
  99. static boost::shared_ptr<B> * b_boost_shared_ptr;
  100. static boost::weak_ptr<B> * b_boost_weak_ptr;
  101. static boost::intrusive_ptr<B> * b_boost_intrusive_ptr;
  102. #endif // _QX_ENABLE_BOOST
  103. static QSharedDataPointer<B> * b_qt_shared_data_ptr;
  104. static QSharedPointer<B> * b_qt_shared_ptr;
  105. static QWeakPointer<B> * b_qt_weak_ptr;
  106. static qx::dao::ptr<B> * b_qx_dao_ptr;
  107. static std::unique_ptr<B> * b_std_unique_ptr;
  108. static std::shared_ptr<B> * b_std_shared_ptr;
  109. static std::weak_ptr<B> * b_std_weak_ptr;
  110. enum { value_0 = (qx::trait::is_smart_ptr<D>::value) };
  111. enum { value_1 = (qx::trait::is_smart_ptr<B>::value) };
  112. enum { value_2 = ((value_0 && value_1) ? qx_smart_ptr_base_of_test_0() : 0) };
  113. enum { value_3 = ((value_0 && ! value_1) ? qx_smart_ptr_base_of_all_test() : 0) };
  114. public:
  115. enum { value = (qx::trait::is_smart_ptr_base_of<B, D>::value_2 || qx::trait::is_smart_ptr_base_of<B, D>::value_3) };
  116. typedef typename std::conditional<qx::trait::is_smart_ptr_base_of<B, D>::value, std::true_type, std::false_type>::type type;
  117. };
  118. } // namespace trait
  119. } // namespace qx
  120. #endif // _QX_IS_SMART_PTR_BASE_OF_H_