blob: 6b25fd53627e46a439e45ca603893f9af4d4fd6a [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2009-2012.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8// See http://www.boost.org/libs/move for documentation.
9//
10//////////////////////////////////////////////////////////////////////////////
11
12//! \file
13
Jeff Thompson3d613fd2013-10-15 15:39:04 -070014#ifndef NDNBOOST_MOVE_MOVE_TRAITS_HPP
15#define NDNBOOST_MOVE_MOVE_TRAITS_HPP
Jeff Thompsona28eed82013-08-22 16:21:10 -070016
17#include <ndnboost/move/detail/config_begin.hpp>
18#include <ndnboost/type_traits/has_trivial_destructor.hpp>
19#include <ndnboost/move/detail/meta_utils.hpp>
20
Jeff Thompson3d613fd2013-10-15 15:39:04 -070021#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070022#include <ndnboost/move/core.hpp>
23#endif
24
25namespace ndnboost {
26
27//! If this trait yields to true
28//! (<i>has_trivial_destructor_after_move &lt;T&gt;::value == true</i>)
29//! means that if T is used as argument of a move construction/assignment,
30//! there is no need to call T's destructor.
31//! This optimization tipically is used to improve containers' performance.
32//!
33//! By default this trait is true if the type has trivial destructor,
34//! every class should specialize this trait if it wants to improve performance
35//! when inserted in containers.
36template <class T>
37struct has_trivial_destructor_after_move
38 : ::ndnboost::has_trivial_destructor<T>
39{};
40
41//! By default this traits returns false. Classes with non-throwing move constructor
42//! and assignment can specialize this trait to obtain some performance improvements.
43template <class T>
44struct has_nothrow_move
45 : public ::ndnboost::move_detail::integral_constant<bool, false>
46{};
47
48namespace move_detail {
49
50// Code from Jeffrey Lee Hellrung, many thanks
51
Jeff Thompson3d613fd2013-10-15 15:39:04 -070052#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070053 template< class T> struct forward_type { typedef T type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070054#else // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070055 template< class T>
56 struct forward_type
57 { typedef const T &type; };
58
59 template< class T>
60 struct forward_type< ndnboost::rv<T> >
61 { typedef T type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070062#endif // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070063
64template< class T > struct is_rvalue_reference : ::ndnboost::move_detail::integral_constant<bool, false> { };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070065#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070066 template< class T > struct is_rvalue_reference< T&& > : ::ndnboost::move_detail::integral_constant<bool, true> { };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070067#else // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070068 template< class T > struct is_rvalue_reference< ndnboost::rv<T>& >
69 : ::ndnboost::move_detail::integral_constant<bool, true>
70 {};
71
72 template< class T > struct is_rvalue_reference< const ndnboost::rv<T>& >
73 : ::ndnboost::move_detail::integral_constant<bool, true>
74 {};
Jeff Thompson3d613fd2013-10-15 15:39:04 -070075#endif // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070076
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070078 template< class T > struct add_rvalue_reference { typedef T&& type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -070079#else // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -070080 namespace detail_add_rvalue_reference
81 {
82 template< class T
83 , bool emulation = ::ndnboost::has_move_emulation_enabled<T>::value
84 , bool rv = ::ndnboost::move_detail::is_rv<T>::value >
85 struct add_rvalue_reference_impl { typedef T type; };
86
87 template< class T, bool emulation>
88 struct add_rvalue_reference_impl< T, emulation, true > { typedef T & type; };
89
90 template< class T, bool rv >
91 struct add_rvalue_reference_impl< T, true, rv > { typedef ::ndnboost::rv<T>& type; };
92 } // namespace detail_add_rvalue_reference
93
94 template< class T >
95 struct add_rvalue_reference
96 : detail_add_rvalue_reference::add_rvalue_reference_impl<T>
97 { };
98
99 template< class T >
100 struct add_rvalue_reference<T &>
101 { typedef T & type; };
102
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700103#endif // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -0700104
105template< class T > struct remove_rvalue_reference { typedef T type; };
106
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700107#ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -0700108 template< class T > struct remove_rvalue_reference< T&& > { typedef T type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109#else // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -0700110 template< class T > struct remove_rvalue_reference< rv<T> > { typedef T type; };
111 template< class T > struct remove_rvalue_reference< const rv<T> > { typedef T type; };
112 template< class T > struct remove_rvalue_reference< volatile rv<T> > { typedef T type; };
113 template< class T > struct remove_rvalue_reference< const volatile rv<T> > { typedef T type; };
114 template< class T > struct remove_rvalue_reference< rv<T>& > { typedef T type; };
115 template< class T > struct remove_rvalue_reference< const rv<T>& > { typedef T type; };
116 template< class T > struct remove_rvalue_reference< volatile rv<T>& > { typedef T type; };
117 template< class T > struct remove_rvalue_reference< const volatile rv<T>& >{ typedef T type; };
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700118#endif // #ifndef NDNBOOST_NO_CXX11_RVALUE_REFERENCES
Jeff Thompsona28eed82013-08-22 16:21:10 -0700119
120template <typename T>
121typename ndnboost::move_detail::add_rvalue_reference<T>::type declval();
122
123} //move_detail {
124
125// Ideas from Boost.Move review, Jeffrey Lee Hellrung:
126//
127//- TypeTraits metafunctions is_lvalue_reference, add_lvalue_reference, and remove_lvalue_reference ?
128// Perhaps add_reference and remove_reference can be modified so that they behave wrt emulated rvalue
129// references the same as wrt real rvalue references, i.e., add_reference< rv<T>& > -> T& rather than
130// rv<T>& (since T&& & -> T&).
131//
132//- Add'l TypeTraits has_[trivial_]move_{constructor,assign}...?
133//
134//- An as_lvalue(T& x) function, which amounts to an identity operation in C++0x, but strips emulated
135// rvalue references in C++03. This may be necessary to prevent "accidental moves".
136
137
138} //namespace ndnboost {
139
140#include <ndnboost/move/detail/config_end.hpp>
141
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700142#endif //#ifndef NDNBOOST_MOVE_MOVE_TRAITS_HPP