• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //-----------------------------------------------------------------------------
2 // boost variant/recursive_wrapper.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2002-2003
7 // Eric Friedman, Itay Maman
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 
13 #ifndef BOOST_VARIANT_RECURSIVE_WRAPPER_HPP
14 #define BOOST_VARIANT_RECURSIVE_WRAPPER_HPP
15 
16 #include <boost/variant/recursive_wrapper_fwd.hpp>
17 #include <boost/variant/detail/move.hpp>
18 #include <boost/checked_delete.hpp>
19 
20 namespace boost {
21 
22 //////////////////////////////////////////////////////////////////////////
23 // class template recursive_wrapper
24 //
25 // See docs and recursive_wrapper_fwd.hpp for more information.
26 //
27 
28 template <typename T>
29 class recursive_wrapper
30 {
31 public: // typedefs
32 
33     typedef T type;
34 
35 private: // representation
36 
37     T* p_;
38 
39 public: // structors
40 
41     ~recursive_wrapper();
42     recursive_wrapper();
43 
44     recursive_wrapper(const recursive_wrapper& operand);
45     recursive_wrapper(const T& operand);
46 
47 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
48     recursive_wrapper(recursive_wrapper&& operand);
49     recursive_wrapper(T&& operand);
50 #endif
51 
52 private: // helpers, for modifiers (below)
53 
54     void assign(const T& rhs);
55 
56 public: // modifiers
57 
operator =(const recursive_wrapper & rhs)58     recursive_wrapper& operator=(const recursive_wrapper& rhs)
59     {
60         assign( rhs.get() );
61         return *this;
62     }
63 
operator =(const T & rhs)64     recursive_wrapper& operator=(const T& rhs)
65     {
66         assign( rhs );
67         return *this;
68     }
69 
swap(recursive_wrapper & operand)70     void swap(recursive_wrapper& operand) BOOST_NOEXCEPT
71     {
72         T* temp = operand.p_;
73         operand.p_ = p_;
74         p_ = temp;
75     }
76 
77 
78 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
operator =(recursive_wrapper && rhs)79     recursive_wrapper& operator=(recursive_wrapper&& rhs) BOOST_NOEXCEPT
80     {
81         swap(rhs);
82         return *this;
83     }
84 
operator =(T && rhs)85     recursive_wrapper& operator=(T&& rhs)
86     {
87         get() = detail::variant::move(rhs);
88         return *this;
89     }
90 #endif
91 
92 public: // queries
93 
get()94     T& get() { return *get_pointer(); }
get() const95     const T& get() const { return *get_pointer(); }
96 
get_pointer()97     T* get_pointer() { return p_; }
get_pointer() const98     const T* get_pointer() const { return p_; }
99 
100 };
101 
102 template <typename T>
~recursive_wrapper()103 recursive_wrapper<T>::~recursive_wrapper()
104 {
105     boost::checked_delete(p_);
106 }
107 
108 template <typename T>
recursive_wrapper()109 recursive_wrapper<T>::recursive_wrapper()
110     : p_(new T)
111 {
112 }
113 
114 template <typename T>
recursive_wrapper(const recursive_wrapper & operand)115 recursive_wrapper<T>::recursive_wrapper(const recursive_wrapper& operand)
116     : p_(new T( operand.get() ))
117 {
118 }
119 
120 template <typename T>
recursive_wrapper(const T & operand)121 recursive_wrapper<T>::recursive_wrapper(const T& operand)
122     : p_(new T(operand))
123 {
124 }
125 
126 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
127 template <typename T>
recursive_wrapper(recursive_wrapper && operand)128 recursive_wrapper<T>::recursive_wrapper(recursive_wrapper&& operand)
129     : p_(new T( detail::variant::move(operand.get()) ))
130 {
131 }
132 
133 template <typename T>
recursive_wrapper(T && operand)134 recursive_wrapper<T>::recursive_wrapper(T&& operand)
135     : p_(new T( detail::variant::move(operand) ))
136 {
137 }
138 #endif
139 
140 template <typename T>
assign(const T & rhs)141 void recursive_wrapper<T>::assign(const T& rhs)
142 {
143     this->get() = rhs;
144 }
145 
146 // function template swap
147 //
148 // Swaps two recursive_wrapper<T> objects of the same type T.
149 //
150 template <typename T>
swap(recursive_wrapper<T> & lhs,recursive_wrapper<T> & rhs)151 inline void swap(recursive_wrapper<T>& lhs, recursive_wrapper<T>& rhs) BOOST_NOEXCEPT
152 {
153     lhs.swap(rhs);
154 }
155 
156 } // namespace boost
157 
158 #endif // BOOST_VARIANT_RECURSIVE_WRAPPER_HPP
159