Dune Core Modules (2.5.2)

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 #ifndef DUNE_COMMON_PROXYMEMBERACCESS_HH
4 #define DUNE_COMMON_PROXYMEMBERACCESS_HH
5 
11 #include <type_traits>
12 #include <utility>
13 
14 namespace Dune {
15 
16  namespace {
17 
18  // helper struct to store a temporary / proxy
19  // for the duration of the member access
20  template<typename T>
21  struct member_access_proxy_holder
22  {
23 
24  // only support moving the temporary into the holder object
25  member_access_proxy_holder(T&& t)
26  : _t(std::move(t))
27  {}
28 
29  // The object is fundamentally a temporary, i.e. an rvalue,
30  //
31  const T* operator->() const
32  {
33  return &_t;
34  }
35 
36  T _t;
37 
38  };
39 
40  } // anonymous namespace
41 
42 
43 #ifdef DOXYGEN
44 
46 
74  template<typename T>
75  pointer_or_proxy_holder
77 
78 #else // DOXYGEN
79 
80 
81  // This version matches lvalues (the C++ type deduction rules state that
82  // the T&& signature deduces to a reference iff the argument is an lvalue).
83  // As the argument is an lvalue, we do not have to worry about its lifetime
84  // and can just return its address.
85  template<typename T>
86  inline typename std::enable_if<
87  std::is_lvalue_reference<T>::value,
88  typename std::add_pointer<
89  typename std::remove_reference<
90  T
91  >::type
92  >::type
93  >::type
94  handle_proxy_member_access(T&& target)
95  {
96  return &target;
97  }
98 
99  // This version matches rvalues (the C++ type deduction rules state that
100  // the T&& signature deduces to a non-reference iff the argument is an rvalue).
101  // In this case, we have to capture the rvalue in a new object to make sure it
102  // is kept alive for the duration of the member access. For this purpose, we move
103  // it into a member_access_proxy_holder instance.
104  template<typename T>
105  inline typename std::enable_if<
106  !std::is_lvalue_reference<T>::value,
107  member_access_proxy_holder<T>
108  >::type
109  handle_proxy_member_access(T&& target)
110  {
111  return {std::forward<T>(target)};
112  }
113 
114 #endif // DOXYGEN
115 
116 } // namespace Dune
117 
118 #endif // DUNE_COMMON_PROXYMEMBERACCESS_HH
Dune namespace.
Definition: alignment.hh:11
pointer_or_proxy_holder handle_proxy_member_access(T &&t)
Transparent support for providing member access to both lvalues and rvalues (temporary proxies).
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)