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