Dune Core Modules (2.9.0)

proxymemberaccess.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_PROXYMEMBERACCESS_HH
6 #define DUNE_COMMON_PROXYMEMBERACCESS_HH
7 
14 #include <type_traits>
15 #include <utility>
16 
17 namespace Dune {
18 
19  namespace Impl {
20 
21  // helper struct to store a temporary / proxy
22  // for the duration of the member access
23  template<typename T>
24  struct member_access_proxy_holder
25  {
26 
27  // only support moving the temporary into the holder object
28  member_access_proxy_holder(T&& t)
29  : _t(std::move(t))
30  {}
31 
32  // The object is fundamentally a temporary, i.e. an rvalue,
33  //
34  const T* operator->() const
35  {
36  return &_t;
37  }
38 
39  T _t;
40 
41  };
42 
43  } // end Impl namespace
44 
45 
46 #ifdef DOXYGEN
47 
49 
79  template<typename T>
80  pointer_or_proxy_holder
82 
83 #else // DOXYGEN
84 
85 
86  // This version matches lvalues (the C++ type deduction rules state that
87  // the T&& signature deduces to a reference iff the argument is an lvalue).
88  // As the argument is an lvalue, we do not have to worry about its lifetime
89  // and can just return its address.
90  template<typename T>
91  inline typename std::enable_if<
92  std::is_lvalue_reference<T>::value,
93  typename std::add_pointer<
94  typename std::remove_reference<
95  T
96  >::type
97  >::type
98  >::type
99  handle_proxy_member_access(T&& target)
100  {
101  return &target;
102  }
103 
104  // This version matches rvalues (the C++ type deduction rules state that
105  // the T&& signature deduces to a non-reference iff the argument is an rvalue).
106  // In this case, we have to capture the rvalue in a new object to make sure it
107  // is kept alive for the duration of the member access. For this purpose, we move
108  // it into a member_access_proxy_holder instance.
109  template<typename T>
110  inline typename std::enable_if<
111  !std::is_lvalue_reference<T>::value,
112  Impl::member_access_proxy_holder<T>
113  >::type
114  handle_proxy_member_access(T&& target)
115  {
116  return {std::forward<T>(target)};
117  }
118 
119 #endif // DOXYGEN
120 
121 } // namespace Dune
122 
123 #endif // DUNE_COMMON_PROXYMEMBERACCESS_HH
pointer_or_proxy_holder handle_proxy_member_access(T &&t)
Transparent support for providing member access to both lvalues and rvalues (temporary proxies).
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 2, 22:35, 2024)