double_object.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // Contains the definition of the class template
  7. // boost::iostreams::detail::double_object, which is similar to compressed pair
  8. // except that both members of the pair have the same type, and
  9. // compression occurs only if requested using a boolean template
  10. // parameter.
  11. #ifndef BOOST_IOSTREAMS_DETAIL_DOUBLE_OBJECT_HPP_INCLUDED
  12. #define BOOST_IOSTREAMS_DETAIL_DOUBLE_OBJECT_HPP_INCLUDED
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <algorithm> // swap.
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #if BOOST_WORKAROUND(__MWERKS__, > 0x3003)
  20. # include <msl_utility>
  21. #else
  22. # include <boost/call_traits.hpp>
  23. #endif
  24. namespace boost { namespace iostreams { namespace detail {
  25. template<typename T>
  26. class single_object_holder {
  27. public:
  28. #if BOOST_WORKAROUND(__MWERKS__, > 0x3003)
  29. typedef Metrowerks::call_traits<T> traits_type;
  30. #else
  31. typedef boost::call_traits<T> traits_type;
  32. #endif
  33. typedef typename traits_type::param_type param_type;
  34. typedef typename traits_type::reference reference;
  35. typedef typename traits_type::const_reference const_reference;
  36. single_object_holder() { }
  37. single_object_holder(param_type t) : first_(t) { }
  38. reference first() { return first_; }
  39. const_reference first() const { return first_; }
  40. reference second() { return first_; }
  41. const_reference second() const { return first_; }
  42. void swap(single_object_holder& o)
  43. { std::swap(first_, o.first_); }
  44. private:
  45. T first_;
  46. };
  47. template<typename T>
  48. struct double_object_holder {
  49. public:
  50. #if BOOST_WORKAROUND(__MWERKS__, > 0x3003)
  51. typedef Metrowerks::call_traits<T> traits_type;
  52. #else
  53. typedef boost::call_traits<T> traits_type;
  54. #endif
  55. typedef typename traits_type::param_type param_type;
  56. typedef typename traits_type::reference reference;
  57. typedef typename traits_type::const_reference const_reference;
  58. double_object_holder() { }
  59. double_object_holder(param_type t1, param_type t2)
  60. : first_(t1), second_(t2) { }
  61. reference first() { return first_; }
  62. const_reference first() const { return first_; }
  63. reference second() { return second_; }
  64. const_reference second() const { return second_; }
  65. void swap(double_object_holder& d)
  66. {
  67. std::swap(first_, d.first_);
  68. std::swap(second_, d.second_);
  69. }
  70. private:
  71. T first_, second_;
  72. };
  73. template<typename T, typename IsDouble>
  74. class double_object
  75. : public mpl::if_<
  76. IsDouble,
  77. double_object_holder<T>,
  78. single_object_holder<T>
  79. >::type
  80. {
  81. private:
  82. typedef typename
  83. mpl::if_<
  84. IsDouble,
  85. double_object_holder<T>,
  86. single_object_holder<T>
  87. >::type base_type;
  88. public:
  89. #if BOOST_WORKAROUND(__MWERKS__, > 0x3003)
  90. typedef Metrowerks::call_traits<T> traits_type;
  91. #else
  92. typedef boost::call_traits<T> traits_type;
  93. #endif
  94. typedef typename traits_type::param_type param_type;
  95. typedef typename traits_type::reference reference;
  96. typedef typename traits_type::const_reference const_reference;
  97. double_object() : base_type() {}
  98. double_object(param_type t1, param_type t2)
  99. : base_type(t1, t2) { }
  100. bool is_double() const { return IsDouble::value; }
  101. };
  102. } } } // End namespaces detail, iostreams, boost.
  103. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DOUBLE_OBJECT_HPP_INCLUDED