xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdClFwd.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
3 // Author: Krzysztof Jamrog <krzysztof.piotr.jamrog@cern.ch>,
4 // Michal Simon <michal.simon@cern.ch>
5 //------------------------------------------------------------------------------
6 // This file is part of the XRootD software suite.
7 //
8 // XRootD is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // XRootD is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // In applying this licence, CERN does not waive the privileges and immunities
22 // granted to it by virtue of its status as an Intergovernmental Organization
23 // or submit itself to any jurisdiction.
24 //------------------------------------------------------------------------------
25 
26 #ifndef SRC_XRDCL_XRDCLFWD_HH_
27 #define SRC_XRDCL_XRDCLFWD_HH_
28 
29 #include <memory>
30 #include <stdexcept>
31 
32 namespace XrdCl
33 {
34  //----------------------------------------------------------------------------
40  //----------------------------------------------------------------------------
41  template<typename T>
42  struct FwdStorage
43  {
44  //--------------------------------------------------------------------------
46  //--------------------------------------------------------------------------
47  FwdStorage() : ptr( nullptr ) { }
48 
49  //--------------------------------------------------------------------------
52  //--------------------------------------------------------------------------
53  FwdStorage( const T &value ) : ptr( new( &storage.memory ) T( value ) )
54  {
55  }
56 
57  //--------------------------------------------------------------------------
60  //--------------------------------------------------------------------------
61  FwdStorage& operator=( const T &value )
62  {
63  ptr = new( &storage.memory ) T( value );
64  return *this;
65  }
66 
67  //--------------------------------------------------------------------------
70  //--------------------------------------------------------------------------
71  FwdStorage( T && value ) : ptr( new( &storage.memory ) T( std::move( value ) ) )
72  {
73  }
74 
75  //--------------------------------------------------------------------------
78  //--------------------------------------------------------------------------
79  FwdStorage& operator=( T && value )
80  {
81  ptr = new( &storage.memory ) T( std::move( value ) );
82  return *this;
83  }
84 
85  //--------------------------------------------------------------------------
87  //--------------------------------------------------------------------------
89  {
90  if( ptr ) ptr->~T();
91  }
92 
93  //--------------------------------------------------------------------------
95  //--------------------------------------------------------------------------
96  union Memory
97  {
98  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
101  Memory() { }
102 
103  //------------------------------------------------------------------------
105  //------------------------------------------------------------------------
106  ~Memory() { }
107 
108  //------------------------------------------------------------------------
110  //------------------------------------------------------------------------
112  };
113 
114  //--------------------------------------------------------------------------
116  //--------------------------------------------------------------------------
118 
119  //--------------------------------------------------------------------------
121  //--------------------------------------------------------------------------
122  T *ptr;
123  };
124 
125  //----------------------------------------------------------------------------
131  //----------------------------------------------------------------------------
132  template<typename T>
133  struct Fwd : protected std::shared_ptr<FwdStorage<T>>
134  {
135  //------------------------------------------------------------------------
140  //------------------------------------------------------------------------
141  Fwd() : std::shared_ptr<FwdStorage<T>>( std::make_shared<FwdStorage<T>>() )
142  {
143  }
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  Fwd( const Fwd &fwd ) : std::shared_ptr<FwdStorage<T>>( fwd )
149  {
150  }
151 
152  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
155  Fwd( Fwd && fwd ) : std::shared_ptr<FwdStorage<T>>( std::move( fwd ) )
156  {
157  }
158 
159  //------------------------------------------------------------------------
161  //------------------------------------------------------------------------
162  Fwd( std::shared_ptr<FwdStorage<T>> && ptr ) : std::shared_ptr<FwdStorage<T>>( std::move( ptr ) )
163  {
164  }
165 
166  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  explicit Fwd( const T &value )
170  {
171  *this->get() = value;
172  }
173 
174  //------------------------------------------------------------------------
176  //------------------------------------------------------------------------
177  explicit Fwd( T &&value )
178  {
179  *this->get() = std::move( value );
180  }
181 
182  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  Fwd& operator=( const T &value )
189  {
190  *this->get() = value;
191  return *this;
192  }
193 
194  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  Fwd& operator=( T && value )
201  {
202  *this->get() = std::move( value );
203  return *this;
204  }
205 
206  //------------------------------------------------------------------------
212  //------------------------------------------------------------------------
213  T& operator*() const
214  {
215  if( !bool( this->get()->ptr ) ) throw std::logic_error( "XrdCl::Fwd contains no value!" );
216  return *this->get()->ptr;
217  }
218 
219  //------------------------------------------------------------------------
225  //------------------------------------------------------------------------
226  T* operator->() const
227  {
228  if( !bool( this->get()->ptr ) ) throw std::logic_error( "XrdCl::Fwd contains no value!" );
229  return this->get()->ptr;
230  }
231 
232  //------------------------------------------------------------------------
234  //------------------------------------------------------------------------
235  bool Valid() const
236  {
237  return bool( this->get()->ptr );
238  }
239  };
240 
241  //--------------------------------------------------------------------------
242  // Utility function for creating forwardable objects
243  //--------------------------------------------------------------------------
244  template<typename T, typename... Args>
245  inline std::shared_ptr<FwdStorage<T>> make_fwd( Args&&... args )
246  {
247  return std::make_shared<FwdStorage<T>>( std::forward<Args>( args )... );
248  }
249 }
250 
251 
252 #endif /* SRC_XRDCL_XRDCLFWD_HH_ */
~Memory()
Make sure the destrutor of T won&#39;t be called.
Definition: XrdClFwd.hh:106
T * ptr
Pointer to the forwarded value.
Definition: XrdClFwd.hh:122
Memory for the value.
Definition: XrdClFwd.hh:96
bool Valid() const
Check if it contains a valid value.
Definition: XrdClFwd.hh:235
T * operator->() const
Definition: XrdClFwd.hh:226
Memory()
Make sure the default constructor of T won&#39;t be called.
Definition: XrdClFwd.hh:101
Fwd()
Definition: XrdClFwd.hh:141
Fwd(const Fwd &fwd)
Copy constructor.
Definition: XrdClFwd.hh:148
FwdStorage()
Default constructor.
Definition: XrdClFwd.hh:47
Fwd & operator=(const T &value)
Definition: XrdClFwd.hh:188
std::shared_ptr< FwdStorage< T > > make_fwd(Args &&...args)
Definition: XrdClFwd.hh:245
FwdStorage(const T &value)
Definition: XrdClFwd.hh:53
Memory storage
The memory for storying forwarded value.
Definition: XrdClFwd.hh:117
Fwd(Fwd &&fwd)
Move constructor.
Definition: XrdClFwd.hh:155
T & operator*() const
Definition: XrdClFwd.hh:213
Fwd(T &&value)
Move construct from value.
Definition: XrdClFwd.hh:177
Fwd & operator=(T &&value)
Definition: XrdClFwd.hh:200
T memory
The memory for storing forwarded value.
Definition: XrdClFwd.hh:111
Fwd(const T &value)
Constructor from value.
Definition: XrdClFwd.hh:169
Fwd(std::shared_ptr< FwdStorage< T >> &&ptr)
Initialize from shared_ptr.
Definition: XrdClFwd.hh:162
FwdStorage(T &&value)
Definition: XrdClFwd.hh:71
FwdStorage & operator=(T &&value)
Definition: XrdClFwd.hh:79
Definition: XrdClFwd.hh:42
Definition: XrdClFwd.hh:133
FwdStorage & operator=(const T &value)
Definition: XrdClFwd.hh:61
~FwdStorage()
Destructor.
Definition: XrdClFwd.hh:88