select_by_size.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. //
  7. // Intended as an alternative to type_traits::yes_type and type_traits::no_type.
  8. // Provides an arbitrary number of types (case_<0>, case_<1>, ...) for
  9. // determining the results of overload resultion using 'sizeof', plus a uniform
  10. // means of using the result. yes_type and no_type are typedefs for case_<1>
  11. // and case_<0>. A single case with negative argument, case_<-1>, is also
  12. // provided, for convenience.
  13. //
  14. // This header may be included any number of times, with
  15. // BOOST_SELECT_BY_SIZE_MAX_CASE defined to be the largest N such that case_<N>
  16. // is needed for a particular application. It defaults to 20.
  17. //
  18. // This header depends only on Boost.Config and Boost.Preprocessor. Dependence
  19. // on Type Traits or MPL was intentionally avoided, to leave open the
  20. // possibility that select_by_size could be used by these libraries.
  21. //
  22. // Example usage:
  23. //
  24. // #define BOOST_SELECT_BY_SIZE_MAX_CASE 7 // (Needed when default was 2)
  25. // #include <boost/utility/select_by_size.hpp>
  26. //
  27. // using namespace boost::utility;
  28. //
  29. // case_<0> helper(bool);
  30. // case_<1> helper(int);
  31. // case_<2> helper(unsigned);
  32. // case_<3> helper(long);
  33. // case_<4> helper(unsigned long);
  34. // case_<5> helper(float);
  35. // case_<6> helper(double);
  36. // case_<7> helper(const char*);
  37. //
  38. // struct test {
  39. // static const int value =
  40. // select_by_size< sizeof(helper(9876UL)) >::value;
  41. // BOOST_STATIC_ASSERT(value == 4);
  42. // };
  43. //
  44. // For compilers with integral constant expression problems, e.g. Borland 5.x,
  45. // one can also write
  46. //
  47. // struct test {
  48. // BOOST_SELECT_BY_SIZE(int, value, helper(9876UL));
  49. // };
  50. //
  51. // to define a static integral constant 'value' equal to
  52. //
  53. // select_by_size< sizeof(helper(9876UL)) >::value.
  54. //
  55. // Include guards surround all contents of this header except for explicit
  56. // specializations of select_by_size for case_<N> with N > 2.
  57. #ifndef BOOST_IOSTREAMS_DETAIL_SELECT_BY_SIZE_HPP_INCLUDED
  58. #define BOOST_IOSTREAMS_DETAIL_SELECT_BY_SIZE_HPP_INCLUDED
  59. // The lowest N for which select_by_size< sizeof(case_<N>) > has not been
  60. // specialized.
  61. #define SELECT_BY_SIZE_MAX_SPECIALIZED 20
  62. #include <boost/config.hpp> // BOOST_STATIC_CONSTANT.
  63. #include <boost/preprocessor/cat.hpp>
  64. #include <boost/preprocessor/iteration/local.hpp>
  65. /* Alternative implementation using max_align.
  66. #include <boost/type_traits/alignment_of.hpp>
  67. #include <boost/type_traits/type_with_alignment.hpp>
  68. namespace boost { namespace utility {
  69. template<int N>
  70. struct case_ { char c[(N + 1) * alignment_of<detail::max_align>::value]; };
  71. template<unsigned Size>
  72. struct select_by_size {
  73. BOOST_STATIC_CONSTANT(int, value =
  74. (Size / alignment_of<detail::max_align>::value - 1));
  75. };
  76. } } // End namespaces utility, boost.
  77. */ // End alternate implementation.
  78. namespace boost { namespace iostreams { namespace detail {
  79. //--------------Definition of case_-------------------------------------------//
  80. template<int N> struct case_ { char c1; case_<N - 1> c2; };
  81. template<> struct case_<-1> { char c; };
  82. typedef case_<true> yes_type;
  83. typedef case_<false> no_type;
  84. //--------------Declaration of select_by_size---------------------------------//
  85. template<unsigned Size> struct select_by_size;
  86. } } } // End namespaces detail, iostreams, boost.
  87. //--------------Definition of SELECT_BY_SIZE_SPEC-----------------------------//
  88. // Sepecializes select_by_size for sizeof(case<n-1>). The decrement is used
  89. // here because the preprocessor library doesn't handle negative integers.
  90. #define SELECT_BY_SIZE_SPEC(n) \
  91. namespace boost { namespace iostreams { namespace detail { \
  92. static const int BOOST_PP_CAT(sizeof_case_, n) = sizeof(case_<n - 1>); \
  93. template<> \
  94. struct select_by_size< BOOST_PP_CAT(sizeof_case_, n) > { \
  95. struct type { BOOST_STATIC_CONSTANT(int, value = n - 1); }; \
  96. BOOST_STATIC_CONSTANT(int, value = type::value); \
  97. }; \
  98. } } } \
  99. /**/
  100. //--------------Default specializations of select_by_size---------------------//
  101. #define BOOST_PP_LOCAL_MACRO(n) SELECT_BY_SIZE_SPEC(n)
  102. #define BOOST_PP_LOCAL_LIMITS (0, 20)
  103. #include BOOST_PP_LOCAL_ITERATE()
  104. #undef BOOST_PP_LOCAL_MACRO
  105. //--------------Definition of SELECT_BY_SIZE----------------------------------//
  106. #define BOOST_SELECT_BY_SIZE(type_, name, expr) \
  107. BOOST_STATIC_CONSTANT( \
  108. unsigned, \
  109. BOOST_PP_CAT(boost_select_by_size_temp_, name) = sizeof(expr) \
  110. ); \
  111. BOOST_STATIC_CONSTANT( \
  112. type_, \
  113. name = \
  114. ( ::boost::iostreams::detail::select_by_size< \
  115. BOOST_PP_CAT(boost_select_by_size_temp_, name) \
  116. >::value ) \
  117. ) \
  118. /**/
  119. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_SELECT_BY_SIZE_HPP_INCLUDED
  120. //----------Specializations of SELECT_BY_SIZE (outside main inclued guards)---//
  121. #if defined(BOOST_SELECT_BY_SIZE_MAX_CASE) && \
  122. BOOST_SELECT_BY_SIZE_MAX_CASE > SELECT_BY_SIZE_MAX_SPECIALIZED
  123. #define BOOST_PP_LOCAL_MACRO(n) SELECT_BY_SIZE_SPEC(n)
  124. #define BOOST_PP_LOCAL_LIMITS \
  125. (SELECT_BY_SIZE_MAX_SPECIALIZED, BOOST_SELECT_BY_SIZE_MAX_CASE) \
  126. /**/
  127. #include BOOST_PP_LOCAL_ITERATE()
  128. #undef BOOST_PP_LOCAL_MACRO
  129. #undef SELECT_BY_SIZE_MAX_SPECIALIZED
  130. #define SELECT_BY_SIZE_MAX_SPECIALIZED BOOST_SELECT_BY_SIZE_MAX_CASE
  131. #endif