]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/include/tr1/functional
functional: Fix License to GPL with exception.
[gcc.git] / libstdc++-v3 / include / tr1 / functional
1 // TR1 functional header -*- C++ -*-
2
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file
31 * This is a TR1 C++ Library header.
32 */
33
34 #ifndef _TR1_FUNCTIONAL
35 #define _TR1_FUNCTIONAL 1
36
37 #include "../functional"
38 #include <string> // for std::tr1::hash
39
40 namespace std
41 {
42 namespace tr1
43 {
44 template<typename _Tp>
45 class reference_wrapper
46 {
47 _Tp* _M_data;
48 public:
49 typedef _Tp type;
50 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
51 { }
52
53 reference_wrapper(const reference_wrapper<_Tp>& __inref):
54 _M_data(__inref._M_data)
55 { }
56
57 reference_wrapper&
58 operator=(const reference_wrapper<_Tp>& __inref)
59 {
60 _M_data = __inref._M_data;
61 return *this;
62 }
63
64 operator _Tp&() const
65 { return this->get(); }
66
67 _Tp&
68 get() const
69 { return *_M_data; }
70 };
71
72 // Denotes a reference should be taken to a variable.
73 template<typename _Tp>
74 reference_wrapper<_Tp>
75 ref(_Tp& __t)
76 { return reference_wrapper<_Tp>(__t); }
77
78 // Denotes a const reference should be taken to a variable.
79 template<typename _Tp>
80 reference_wrapper<const _Tp>
81 cref(const _Tp& __t)
82 { return reference_wrapper<const _Tp>(__t); }
83
84 template<typename _Tp>
85 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
86 { return ref(__t.get()); }
87
88 template<typename _Tp>
89 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
90 { return cref(__t.get()); }
91
92
93 // Definition of default hash function std::tr1::hash<>. The types for
94 // which std::tr1::hash<T> is defined is in clause 6.3.3. of the PDTR.
95
96 template <typename T> struct hash;
97
98 #define tr1_hashtable_define_trivial_hash(T) \
99 template <> struct hash<T> { \
100 std::size_t operator()(T val) const { return static_cast<std::size_t>(val); } \
101 } \
102
103 tr1_hashtable_define_trivial_hash(bool);
104 tr1_hashtable_define_trivial_hash(char);
105 tr1_hashtable_define_trivial_hash(signed char);
106 tr1_hashtable_define_trivial_hash(unsigned char);
107 tr1_hashtable_define_trivial_hash(wchar_t);
108 tr1_hashtable_define_trivial_hash(short);
109 tr1_hashtable_define_trivial_hash(int);
110 tr1_hashtable_define_trivial_hash(long);
111 tr1_hashtable_define_trivial_hash(unsigned short);
112 tr1_hashtable_define_trivial_hash(unsigned int);
113 tr1_hashtable_define_trivial_hash(unsigned long);
114
115 tr1_hashtable_define_trivial_hash(float);
116 tr1_hashtable_define_trivial_hash(double);
117 tr1_hashtable_define_trivial_hash(long double);
118
119 #undef tr1_hashtable_define_trivial_hash
120
121 template <typename T>
122 struct hash<T*> {
123 std::size_t operator()(T* p) const {
124 return reinterpret_cast<std::size_t>(p);
125 }
126 };
127
128 // ??? We can probably find a better hash function than this (i.e. one
129 // that vectorizes better and that produces a more uniform distribution).
130
131 // XXX String hash probably shouldn't be an inline member function,
132 // since it's nontrivial. Once we have the framework for TR1 .cc
133 // files, this should go in one.
134
135 template <>
136 struct hash<std::string>
137 {
138 std::size_t operator()(const std::string& s) const
139 {
140 std::size_t result = 0;
141 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
142 result = (result * 131) + *i;
143 return result;
144 }
145 };
146
147 #ifdef _GLIBCXX_USE_WCHAR_T
148 template <>
149 struct hash<std::wstring>
150 {
151 std::size_t operator()(const std::wstring& s) const
152 {
153 std::size_t result = 0;
154 for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
155 result = (result * 131) + *i;
156 return result;
157 }
158 };
159 #endif
160
161 }
162 }
163
164 #endif
This page took 0.043569 seconds and 5 git commands to generate.