GEOS
ReferenceWrapper.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_DATAREPOSITORY_REFERENCEWRAPPER_HPP_
21 #define GEOS_DATAREPOSITORY_REFERENCEWRAPPER_HPP_
22 
23 // System includes
24 #include <type_traits>
25 
26 namespace geos
27 {
28 
48 template< typename T >
50 {
51 public:
52 
57  m_ref( nullptr )
58  {}
59 
60 
65  ReferenceWrapper( T & source ) noexcept:
66  m_ref( &source )
67  {}
68 
69 
73  ~ReferenceWrapper() = default;
74 
80  m_ref( source.m_ref )
81  {}
82 
83 
89  m_ref( source.m_ref )
90  {
91  source.m_ref = nullptr;
92  }
93 
100  {
101  m_ref = source.m_ref;
102  return *this;
103  }
104 
105 
116  template< typename T_RHS, typename U=T >
117  inline
118  typename std::enable_if< !std::is_const< U >::value, ReferenceWrapper & >::type
119  operator=( T_RHS const & rhs )
120  {
121  *m_ref = rhs;
122  return *this;
123  }
124 
132  inline ReferenceWrapper & operator=( T && source )
133  {
134  *m_ref = std::move( source );
135  return *this;
136  }
137 
141  inline operator T & ()
142  {
143  return *m_ref;
144  }
145 
149  inline operator T const & () const
150  {
151  return *m_ref;
152  }
153 
158  inline void set( T & source )
159  {
160  m_ref = &source;
161  }
162 
167  inline void set( T * source )
168  {
169  m_ref = source;
170  }
171 
176  inline T & get()
177  {
178  return *m_ref;
179  }
180 
185  inline T const & get() const
186  {
187  return *m_ref;
188  }
189 
194  inline bool isValid() const
195  {
196  return m_ref;
197  }
198 
203  inline T const * getPtr() const
204  {
205  return m_ref;
206  }
207 
208  /*
209  * Unfortunately, Doxygen does not understand decltype in function return types.
210  * It does not generate documentation for the following two functions, but still
211  * emits a warning. So we just disable docs for these until issue fixed in Doxygen.
212  */
214 
221  template< typename INDEX_TYPE, typename U = T >
222  inline decltype( std::declval< U >()[1] )
223  operator[]( INDEX_TYPE const i )
224  {
225  return (*m_ref)[i];
226  }
227 
234  template< typename INDEX_TYPE, typename U = T >
235  inline decltype( std::declval< U const >()[1] )
236  operator[]( INDEX_TYPE const i ) const
237  {
238  return (*m_ref)[i];
239  }
240 
242 
249  template< typename ... ARGS >
250  inline typename std::result_of< T & (ARGS&&...) >::type
251  operator()( ARGS && ... args )
252  {
253  return m_ref->operator()( std::forward< ARGS >(args)... );
254  }
255 
262  template< typename ... ARGS >
263  inline typename std::result_of< T const&(ARGS&&...) >::type
264  operator()( ARGS && ... args ) const
265  {
266  return m_ref->operator()( std::forward< ARGS >(args)... );
267  }
268 
269 
270 private:
272  T * m_ref;
273 };
274 
275 
276 
277 } /* namespace geos */
278 
279 #endif /* GEOS_DATAREPOSITORY_REFERENCEWRAPPER_HPP_ */
T const * getPtr() const
Const accessor for m_ref.
std::enable_if< !std::is_const< U >::value, ReferenceWrapper & >::type operator=(T_RHS const &rhs)
Assignment operator.
ReferenceWrapper(ReferenceWrapper &&source)
Move constructor copies the source m_ref to the new m_ref.
bool isValid() const
Check if reference is initialized.
std::result_of< T &(ARGS &&...) >::type operator()(ARGS &&... args)
A pass thru parenthesis operator which calls underlying T::operator().
ReferenceWrapper & operator=(ReferenceWrapper const &source)
Copy assignment operator.
std::result_of< T const &(ARGS &&...) >::type operator()(ARGS &&... args) const
A pass thru parenthesis operator which calls underlying T::operator().
void set(T &source)
Set the address that m_ref points to.
~ReferenceWrapper()=default
Default destructor.
T & get()
Accessor for m_ref.
ReferenceWrapper(T &source) noexcept
Constructor that sets m_ref to address of input.
T const & get() const
Const accessor for m_ref.
void set(T *source)
Set the address that m_ref points to.
ReferenceWrapper & operator=(T &&source)
Move assignment operator.
ReferenceWrapper()
Default constructor sets m_ref to nullptr.
ReferenceWrapper(ReferenceWrapper const &source)
Copy constructor copies the source m_ref to the new m_ref.