Dune Core Modules (2.7.1)

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