get_base_class.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_GET_BASE_CLASS_H_
  32. #define _QX_GET_BASE_CLASS_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file get_base_class.h
  38. * \author Lionel Marty
  39. * \ingroup QxTraits
  40. * \brief qx::trait::get_base_class<T>::type : retrieve base class of type T registered into QxOrm context and return qx::trait::no_base_class_defined if no base class defined
  41. */
  42. #include <QxTraits/get_class_name.h>
  43. namespace qx {
  44. namespace trait {
  45. class no_base_class_defined
  46. { public: no_base_class_defined() { ; }; virtual ~no_base_class_defined() { ; }; virtual void dummy() = 0; };
  47. /*!
  48. * \ingroup QxTraits
  49. * \brief qx::trait::get_base_class<T>::type : retrieve base class of type T registered into QxOrm context and return qx::trait::no_base_class_defined if no base class defined
  50. */
  51. template <class T>
  52. class get_base_class
  53. { public: typedef qx::trait::no_base_class_defined type; };
  54. template <class T>
  55. class is_base_class_defined
  56. { public: enum { value = (std::is_same<typename qx::trait::get_base_class<T>::type, qx::trait::no_base_class_defined>::value ? 0 : 1) }; };
  57. template <class T>
  58. class get_base_class_2
  59. {
  60. private: typedef typename qx::trait::get_base_class<T>::type type_base;
  61. private: enum { is_base_ok = (std::is_same<type_base, qx::trait::no_base_class_defined>::value ? 0 : 1) };
  62. public: typedef typename std::conditional<is_base_ok, type_base, T>::type type;
  63. };
  64. } // namespace trait
  65. } // namespace qx
  66. QX_REGISTER_CLASS_NAME(qx::trait::no_base_class_defined)
  67. #define QX_REGISTER_BASE_CLASS(derivedClass, baseClass) \
  68. namespace qx { namespace trait { \
  69. template <> \
  70. class get_base_class< derivedClass > \
  71. { public: typedef baseClass type; }; \
  72. } } // namespace qx::trait
  73. #define QX_GET_BASE_CLASS(T) qx::trait::get_base_class< T >::type
  74. #define QX_GET_BASE_CLASS_WITH_TYPENAME(T) qx::trait::get_base_class< typename T >::type
  75. #define QX_IS_BASE_CLASS_DEFINED(T) qx::trait::is_base_class_defined< T >::value
  76. #define QX_IS_BASE_CLASS_DEFINED_WITH_TYPENAME(T) qx::trait::is_base_class_defined< typename T >::value
  77. #define QX_GET_BASE_CLASS_2(T) qx::trait::get_base_class_2< T >::type
  78. #define QX_GET_BASE_CLASS_2_WITH_TYPENAME(T) qx::trait::get_base_class_2< typename T >::type
  79. #endif // _QX_GET_BASE_CLASS_H_