1 /*
2 * Copyright 2002, The libsigc++ Development Team
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19 #ifndef _SIGC_REFERENCE_WRAPPER_H_
20 #define _SIGC_REFERENCE_WRAPPER_H_
21
22 namespace sigc {
23
24 /** Reference wrapper.
25 * Use sigc::ref() to create a reference wrapper.
26 */
27 template <class T_type>
28 struct reference_wrapper
29 {
reference_wrapperreference_wrapper30 explicit reference_wrapper(T_type& v)
31 : value_(v) {}
32
33 operator T_type& () const
34 { return value_; }
35
36 T_type& value_;
37 };
38
39 /** Const reference wrapper.
40 * Use sigc::ref() to create a const reference wrapper.
41 */
42 template <class T_type>
43 struct const_reference_wrapper
44 {
const_reference_wrapperconst_reference_wrapper45 explicit const_reference_wrapper(const T_type& v)
46 : value_(v) {}
47
48 operator const T_type& () const
49 { return value_; }
50
51 const T_type& value_;
52 };
53
54 /** Creates a reference wrapper.
55 * Passing an object throught sigc::ref() makes libsigc++ adaptors
56 * like, e.g., sigc::bind store references to the object instead of copies.
57 * If the object type inherits from sigc::trackable this will ensure
58 * automatic invalidation of the adaptors when the object is deleted
59 * or overwritten.
60 *
61 * @param v Reference to store.
62 * @return A reference wrapper.
63 */
64 template <class T_type>
ref(T_type & v)65 reference_wrapper<T_type> ref(T_type& v)
66 { return reference_wrapper<T_type>(v); }
67
68 /** Creates a const reference wrapper.
69 * Passing an object throught sigc::ref() makes libsigc++ adaptors
70 * like, e.g., sigc::bind store references to the object instead of copies.
71 * If the object type inherits from sigc::trackable this will ensure
72 * automatic invalidation of the adaptors when the object is deleted
73 * or overwritten.
74 *
75 * @param v Reference to store.
76 * @return A reference wrapper.
77 */
78 template <class T_type>
ref(const T_type & v)79 const_reference_wrapper<T_type> ref(const T_type& v)
80 { return const_reference_wrapper<T_type>(v); }
81
82 template <class T_type>
83 struct unwrap_reference
84 {
85 typedef T_type type;
86 };
87
88 template <class T_type>
89 struct unwrap_reference<reference_wrapper<T_type> >
90 {
91 typedef T_type& type;
92 };
93
94 template <class T_type>
95 struct unwrap_reference<const_reference_wrapper<T_type> >
96 {
97 typedef const T_type& type;
98 };
99
100 template <class T_type>
101 T_type& unwrap(const reference_wrapper<T_type>& v)
102 { return v; }
103
104 template <class T_type>
105 const T_type& unwrap(const const_reference_wrapper<T_type>& v)
106 { return v; }
107
108 } /* namespace sigc */
109
110 #endif /* _SIGC_REFERENCE_WRAPPER_H_ */
111