any.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // See http://www.boost.org/libs/any for Documentation.
  2. #ifndef BOOST_ANY_INCLUDED
  3. #define BOOST_ANY_INCLUDED
  4. // what: variant type boost::any
  5. // who: contributed by Kevlin Henney,
  6. // with features contributed and bugs found by
  7. // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
  8. // when: July 2001
  9. // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
  10. #include <algorithm>
  11. #include <typeinfo>
  12. #include "boost/config.hpp"
  13. #include <boost/type_traits/remove_reference.hpp>
  14. #include <boost/type_traits/is_reference.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/static_assert.hpp>
  17. // See boost/python/type_id.hpp
  18. // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp
  19. # if (defined(__GNUC__) && __GNUC__ >= 3) \
  20. || defined(_AIX) \
  21. || ( defined(__sgi) && defined(__host_mips)) \
  22. || (defined(__hpux) && defined(__HP_aCC)) \
  23. || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
  24. # define BOOST_AUX_ANY_TYPE_ID_NAME
  25. #include <cstring>
  26. # endif
  27. namespace boost
  28. {
  29. class any
  30. {
  31. public: // structors
  32. any()
  33. : content(0)
  34. {
  35. }
  36. template<typename ValueType>
  37. any(const ValueType & value)
  38. : content(new holder<ValueType>(value))
  39. {
  40. }
  41. any(const any & other)
  42. : content(other.content ? other.content->clone() : 0)
  43. {
  44. }
  45. ~any()
  46. {
  47. delete content;
  48. }
  49. public: // modifiers
  50. any & swap(any & rhs)
  51. {
  52. std::swap(content, rhs.content);
  53. return *this;
  54. }
  55. template<typename ValueType>
  56. any & operator=(const ValueType & rhs)
  57. {
  58. any(rhs).swap(*this);
  59. return *this;
  60. }
  61. any & operator=(any rhs)
  62. {
  63. rhs.swap(*this);
  64. return *this;
  65. }
  66. public: // queries
  67. bool empty() const
  68. {
  69. return !content;
  70. }
  71. const std::type_info & type() const
  72. {
  73. return content ? content->type() : typeid(void);
  74. }
  75. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  76. private: // types
  77. #else
  78. public: // types (public so any_cast can be non-friend)
  79. #endif
  80. class placeholder
  81. {
  82. public: // structors
  83. virtual ~placeholder()
  84. {
  85. }
  86. public: // queries
  87. virtual const std::type_info & type() const = 0;
  88. virtual placeholder * clone() const = 0;
  89. };
  90. template<typename ValueType>
  91. class holder : public placeholder
  92. {
  93. public: // structors
  94. holder(const ValueType & value)
  95. : held(value)
  96. {
  97. }
  98. public: // queries
  99. virtual const std::type_info & type() const
  100. {
  101. return typeid(ValueType);
  102. }
  103. virtual placeholder * clone() const
  104. {
  105. return new holder(held);
  106. }
  107. public: // representation
  108. ValueType held;
  109. private: // intentionally left unimplemented
  110. holder & operator=(const holder &);
  111. };
  112. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  113. private: // representation
  114. template<typename ValueType>
  115. friend ValueType * any_cast(any *);
  116. template<typename ValueType>
  117. friend ValueType * unsafe_any_cast(any *);
  118. #else
  119. public: // representation (public so any_cast can be non-friend)
  120. #endif
  121. placeholder * content;
  122. };
  123. class bad_any_cast : public std::bad_cast
  124. {
  125. public:
  126. virtual const char * what() const throw()
  127. {
  128. return "boost::bad_any_cast: "
  129. "failed conversion using boost::any_cast";
  130. }
  131. };
  132. template<typename ValueType>
  133. ValueType * any_cast(any * operand)
  134. {
  135. return operand &&
  136. #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
  137. std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
  138. #else
  139. operand->type() == typeid(ValueType)
  140. #endif
  141. ? &static_cast<any::holder<ValueType> *>(operand->content)->held
  142. : 0;
  143. }
  144. template<typename ValueType>
  145. inline const ValueType * any_cast(const any * operand)
  146. {
  147. return any_cast<ValueType>(const_cast<any *>(operand));
  148. }
  149. template<typename ValueType>
  150. ValueType any_cast(any & operand)
  151. {
  152. typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
  153. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  154. // If 'nonref' is still reference type, it means the user has not
  155. // specialized 'remove_reference'.
  156. // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
  157. // to generate specialization of remove_reference for your class
  158. // See type traits library documentation for details
  159. BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
  160. #endif
  161. nonref * result = any_cast<nonref>(&operand);
  162. if(!result)
  163. boost::throw_exception(bad_any_cast());
  164. return *result;
  165. }
  166. template<typename ValueType>
  167. inline ValueType any_cast(const any & operand)
  168. {
  169. typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
  170. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  171. // The comment in the above version of 'any_cast' explains when this
  172. // assert is fired and what to do.
  173. BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
  174. #endif
  175. return any_cast<const nonref &>(const_cast<any &>(operand));
  176. }
  177. // Note: The "unsafe" versions of any_cast are not part of the
  178. // public interface and may be removed at any time. They are
  179. // required where we know what type is stored in the any and can't
  180. // use typeid() comparison, e.g., when our types may travel across
  181. // different shared libraries.
  182. template<typename ValueType>
  183. inline ValueType * unsafe_any_cast(any * operand)
  184. {
  185. return &static_cast<any::holder<ValueType> *>(operand->content)->held;
  186. }
  187. template<typename ValueType>
  188. inline const ValueType * unsafe_any_cast(const any * operand)
  189. {
  190. return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
  191. }
  192. }
  193. // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
  194. //
  195. // Distributed under the Boost Software License, Version 1.0. (See
  196. // accompanying file LICENSE_1_0.txt or copy at
  197. // http://www.boost.org/LICENSE_1_0.txt)
  198. #endif