1 // TR1 functional header -*- C++ -*-
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
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)
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.
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,
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.
31 * This is a TR1 C++ Library header.
34 #ifndef _TR1_FUNCTIONAL
35 #define _TR1_FUNCTIONAL 1
37 #include "../functional"
38 #include <string> // for std::tr1::hash
44 template<typename _Tp>
45 class reference_wrapper
50 explicit reference_wrapper(_Tp& __indata): _M_data(&__indata)
53 reference_wrapper(const reference_wrapper<_Tp>& __inref):
54 _M_data(__inref._M_data)
58 operator=(const reference_wrapper<_Tp>& __inref)
60 _M_data = __inref._M_data;
65 { return this->get(); }
72 // Denotes a reference should be taken to a variable.
73 template<typename _Tp>
74 reference_wrapper<_Tp>
76 { return reference_wrapper<_Tp>(__t); }
78 // Denotes a const reference should be taken to a variable.
79 template<typename _Tp>
80 reference_wrapper<const _Tp>
82 { return reference_wrapper<const _Tp>(__t); }
84 template<typename _Tp>
85 reference_wrapper<_Tp> ref(reference_wrapper<_Tp> __t)
86 { return ref(__t.get()); }
88 template<typename _Tp>
89 reference_wrapper<const _Tp> cref(reference_wrapper<_Tp> __t)
90 { return cref(__t.get()); }
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.
96 template <typename T> struct hash;
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); } \
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);
115 tr1_hashtable_define_trivial_hash(float);
116 tr1_hashtable_define_trivial_hash(double);
117 tr1_hashtable_define_trivial_hash(long double);
119 #undef tr1_hashtable_define_trivial_hash
121 template <typename T>
123 std::size_t operator()(T* p) const {
124 return reinterpret_cast<std::size_t>(p);
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).
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.
136 struct hash<std::string>
138 std::size_t operator()(const std::string& s) const
140 std::size_t result = 0;
141 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
142 result = (result * 131) + *i;
147 #ifdef _GLIBCXX_USE_WCHAR_T
149 struct hash<std::wstring>
151 std::size_t operator()(const std::wstring& s) const
153 std::size_t result = 0;
154 for (std::wstring::const_iterator i = s.begin(); i != s.end(); ++i)
155 result = (result * 131) + *i;