QxAny.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_ANY_H_
  32. #define _QX_ANY_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxAny.h
  38. * \author Lionel Marty
  39. * \ingroup QxCommon
  40. * \brief qx::any : basic implementation of boost::any (written by Kevlin Henney) when boost dependency is not available
  41. */
  42. #ifndef _QX_NO_RTTI
  43. #include <typeinfo>
  44. #define QX_TYPE_ID(T) typeid(T)
  45. #else // _QX_NO_RTTI
  46. #include <QxTraits/get_class_name.h>
  47. #include <QxTraits/get_class_name_primitive.h>
  48. #define QX_TYPE_ID(T) std::string(qx::trait::get_class_name< T >::get())
  49. #endif // _QX_NO_RTTI
  50. #ifndef Q_OS_WIN
  51. #if (__GNUC__ >= 4)
  52. #define QX_ANY_FORCE_HIDDEN_VISIBILITY __attribute__ ((visibility("hidden"))) // To avoid a GCC warning : 'qx::any::holder<T>' declared with greater visibility than the type of its field 'qx::any::holder<T>::held' [-Wattributes]
  53. #endif // (__GNUC__ >= 4)
  54. #endif // Q_OS_WIN
  55. #ifndef QX_ANY_FORCE_HIDDEN_VISIBILITY
  56. #define QX_ANY_FORCE_HIDDEN_VISIBILITY /* Nothing */
  57. #endif // QX_ANY_FORCE_HIDDEN_VISIBILITY
  58. namespace qx {
  59. class any;
  60. template <typename ValueType> ValueType * any_cast(any *);
  61. template <typename ValueType> ValueType * unsafe_any_cast(any *);
  62. class any
  63. {
  64. template <typename ValueType> friend ValueType * qx::any_cast(any *);
  65. template <typename ValueType> friend ValueType * qx::unsafe_any_cast(any *);
  66. public:
  67. #ifndef _QX_NO_RTTI
  68. typedef const std::type_info & type_check;
  69. #else // _QX_NO_RTTI
  70. typedef std::string type_check;
  71. #endif // _QX_NO_RTTI
  72. any() : content(NULL) { ; }
  73. any(const any & other) : content(other.content ? other.content->clone() : NULL) { ; }
  74. ~any() { if (content) { delete content; } }
  75. template <typename ValueType>
  76. any(const ValueType & value) : content(new holder<typename std::remove_cv<typename std::decay<const ValueType>::type>::type>(value)) { ; }
  77. any & swap(any & other) { std::swap(content, other.content); return (* this); }
  78. template <typename ValueType>
  79. any & operator=(const ValueType & other) { any(other).swap(* this); return (* this); }
  80. any & operator=(any other) { any(other).swap(* this); return (* this); }
  81. bool empty() const { return (! content); }
  82. void clear() { any().swap(* this); }
  83. type_check type() const { return (content ? content->type() : QX_TYPE_ID(void)); }
  84. private:
  85. struct placeholder
  86. {
  87. virtual ~placeholder() { ; }
  88. virtual type_check type() const = 0;
  89. virtual placeholder * clone() const = 0;
  90. };
  91. template <typename ValueType>
  92. struct QX_ANY_FORCE_HIDDEN_VISIBILITY holder : public placeholder
  93. {
  94. holder(const ValueType & value) : held(value) { ; }
  95. virtual type_check type() const { return QX_TYPE_ID(ValueType); }
  96. virtual placeholder * clone() const { return new holder(held); }
  97. ValueType held;
  98. private:
  99. holder & operator=(const holder &);
  100. };
  101. placeholder * content;
  102. };
  103. inline void swap(any & lhs, any & other) { lhs.swap(other); }
  104. struct bad_any_cast : public std::exception
  105. { virtual const char * what() const throw() { return "qx::bad_any_cast : failed conversion using qx::any_cast"; } };
  106. template <typename ValueType>
  107. ValueType * any_cast(any * operand)
  108. { return ((operand && (operand->type() == QX_TYPE_ID(ValueType))) ? (& static_cast<any::holder<typename std::remove_cv<ValueType>::type> *>(operand->content)->held) : NULL); }
  109. template <typename ValueType>
  110. const ValueType * any_cast(const any * operand)
  111. { return any_cast<ValueType>(const_cast<any *>(operand)); }
  112. template <typename ValueType>
  113. ValueType any_cast(any & operand)
  114. {
  115. typedef typename std::remove_reference<ValueType>::type nonref;
  116. nonref * result = any_cast<nonref>(& operand);
  117. if (! result) { throw qx::bad_any_cast(); }
  118. return static_cast<ValueType>(* result);
  119. }
  120. template <typename ValueType>
  121. ValueType any_cast(const any & operand)
  122. {
  123. typedef typename std::remove_reference<ValueType>::type nonref;
  124. return any_cast<const nonref &>(const_cast<any &>(operand));
  125. }
  126. template <typename ValueType>
  127. ValueType * unsafe_any_cast(any * operand)
  128. { return (& static_cast<any::holder<ValueType> *>(operand->content)->held); }
  129. template <typename ValueType>
  130. const ValueType * unsafe_any_cast(const any * operand)
  131. { return unsafe_any_cast<ValueType>(const_cast<any *>(operand)); }
  132. } // namespace qx
  133. #endif // _QX_ANY_H_