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 TotalEnergies
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 #include <utility>
26 
27 namespace geos
28 {
29 
49 template< typename T >
51 {
52 public:
53 
58  m_ref( nullptr )
59  {}
60 
61 
66  ReferenceWrapper( T & source ) noexcept:
67  m_ref( &source )
68  {}
69 
70 
74  ~ReferenceWrapper() = default;
75 
81  m_ref( source.m_ref )
82  {}
83 
84 
90  m_ref( source.m_ref )
91  {
92  source.m_ref = nullptr;
93  }
94 
101  {
102  m_ref = source.m_ref;
103  return *this;
104  }
105 
106 
117  template< typename T_RHS, typename U=T >
118  inline
119  typename std::enable_if< !std::is_const< U >::value, ReferenceWrapper & >::type
120  operator=( T_RHS const & rhs )
121  {
122  *m_ref = rhs;
123  return *this;
124  }
125 
133  inline ReferenceWrapper & operator=( T && source )
134  {
135  *m_ref = std::move( source );
136  return *this;
137  }
138 
142  inline operator T & ()
143  {
144  return *m_ref;
145  }
146 
150  inline operator T const & () const
151  {
152  return *m_ref;
153  }
154 
159  inline void set( T & source )
160  {
161  m_ref = &source;
162  }
163 
168  inline void set( T * source )
169  {
170  m_ref = source;
171  }
172 
177  inline T & get()
178  {
179  return *m_ref;
180  }
181 
186  inline T const & get() const
187  {
188  return *m_ref;
189  }
190 
195  inline bool isValid() const
196  {
197  return m_ref;
198  }
199 
204  inline T const * getPtr() const
205  {
206  return m_ref;
207  }
208 
209  /*
210  * Unfortunately, Doxygen does not understand decltype in function return types.
211  * It does not generate documentation for the following two functions, but still
212  * emits a warning. So we just disable docs for these until issue fixed in Doxygen.
213  */
215 
222  template< typename INDEX_TYPE, typename U = T >
223  inline decltype( std::declval< U >()[1] )
224  operator[]( INDEX_TYPE const i )
225  {
226  return (*m_ref)[i];
227  }
228 
235  template< typename INDEX_TYPE, typename U = T >
236  inline decltype( std::declval< U const >()[1] )
237  operator[]( INDEX_TYPE const i ) const
238  {
239  return (*m_ref)[i];
240  }
241 
243 
250  template< typename ... ARGS >
251  inline typename std::invoke_result< T &, ARGS &&... >::type
252  operator()( ARGS && ... args )
253  {
254  return m_ref->operator()( std::forward< ARGS >(args)... );
255  }
256 
263  template< typename ... ARGS >
264  inline typename std::invoke_result< T const &, ARGS &&... >::type
265  operator()( ARGS && ... args ) const
266  {
267  return m_ref->operator()( std::forward< ARGS >(args)... );
268  }
269 
270 
271 private:
273  T * m_ref;
274 };
275 
276 
277 
278 } /* namespace geos */
279 
280 #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.
std::invoke_result< T const &, ARGS &&... >::type operator()(ARGS &&... args) const
A pass thru parenthesis operator which calls underlying T::operator().
bool isValid() const
Check if reference is initialized.
ReferenceWrapper & operator=(ReferenceWrapper const &source)
Copy assignment operator.
void set(T &source)
Set the address that m_ref points to.
~ReferenceWrapper()=default
Default destructor.
T & get()
Accessor for m_ref.
std::invoke_result< T &, ARGS &&... >::type operator()(ARGS &&... args)
A pass thru parenthesis operator which calls underlying T::operator().
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.