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