is_dereferenceable.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2005-2007 Jonathan Turkanis
  3. // (C) Copyright David Abrahams 2004.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  6. // See http://www.boost.org/libs/iostreams for documentation.
  7. #ifndef BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED
  9. # include <boost/type_traits/detail/bool_trait_def.hpp>
  10. # include <boost/type_traits/detail/template_arity_spec.hpp>
  11. # include <boost/type_traits/remove_cv.hpp>
  12. # include <boost/mpl/aux_/lambda_support.hpp>
  13. # include <boost/mpl/bool.hpp>
  14. # include <boost/detail/workaround.hpp>
  15. namespace boost { namespace iostreams { namespace detail {
  16. // is_dereferenceable<T> metafunction
  17. //
  18. // Requires: Given x of type T&, if the expression *x is well-formed
  19. // it must have complete type; otherwise, it must neither be ambiguous
  20. // nor violate access.
  21. // This namespace ensures that ADL doesn't mess things up.
  22. namespace is_dereferenceable_
  23. {
  24. // a type returned from operator* when no increment is found in the
  25. // type's own namespace
  26. struct tag {};
  27. // any soaks up implicit conversions and makes the following
  28. // operator* less-preferred than any other such operator that
  29. // might be found via ADL.
  30. struct any { template <class T> any(T const&); };
  31. // This is a last-resort operator* for when none other is found
  32. tag operator*(any const&);
  33. # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202)) \
  34. || BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  35. # define BOOST_comma(a,b) (a)
  36. # else
  37. // In case an operator++ is found that returns void, we'll use ++x,0
  38. tag operator,(tag,int);
  39. # define BOOST_comma(a,b) (a,b)
  40. # endif
  41. // two check overloads help us identify which operator++ was picked
  42. char (& check_increment(tag) )[2];
  43. template <class T>
  44. char check_increment(T const&);
  45. template <class T>
  46. struct impl
  47. {
  48. static typename boost::remove_cv<T>::type& x;
  49. BOOST_STATIC_CONSTANT(
  50. bool
  51. , value = sizeof(is_dereferenceable_::check_increment(BOOST_comma(*x,0))) == 1
  52. );
  53. };
  54. }
  55. # undef BOOST_comma
  56. template<typename T>
  57. struct is_dereferenceable
  58. BOOST_TT_AUX_BOOL_C_BASE(is_dereferenceable_::impl<T>::value)
  59. {
  60. BOOST_TT_AUX_BOOL_TRAIT_VALUE_DECL(is_dereferenceable_::impl<T>::value)
  61. BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_dereferenceable,(T))
  62. };
  63. } }
  64. BOOST_TT_AUX_TEMPLATE_ARITY_SPEC(1, ::boost::iostreams::detail::is_dereferenceable)
  65. } // End namespaces detail, iostreams, boost.
  66. #endif // BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED