From b0e788cc09b06d66038cc6cbe2082e832a7ebefd Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Fri, 11 Jun 2010 16:20:24 +0000 Subject: [PATCH] shared_ptr.h (hash): Add. 2010-06-11 Paolo Carlini * include/bits/shared_ptr.h (hash): Add. * include/bits/shared_ptr_base.h (hash<__shared_ptr>): Likewise. * include/bits/unique_ptr.h (hash): Likewise. * testsuite/20_util/shared_ptr/hash/1.cc: New. * testsuite/20_util/unique_ptr/hash/1.cc: Likewise. From-SVN: r160621 --- libstdc++-v3/ChangeLog | 8 ++++ libstdc++-v3/include/bits/shared_ptr.h | 10 ++++ libstdc++-v3/include/bits/shared_ptr_base.h | 10 ++++ libstdc++-v3/include/bits/unique_ptr.h | 15 +++++- .../testsuite/20_util/shared_ptr/hash/1.cc | 48 +++++++++++++++++++ .../testsuite/20_util/unique_ptr/hash/1.cc | 48 +++++++++++++++++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/20_util/shared_ptr/hash/1.cc create mode 100644 libstdc++-v3/testsuite/20_util/unique_ptr/hash/1.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index e2a1c9ae4c1..96dd53a6a47 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2010-06-11 Paolo Carlini + + * include/bits/shared_ptr.h (hash): Add. + * include/bits/shared_ptr_base.h (hash<__shared_ptr>): Likewise. + * include/bits/unique_ptr.h (hash): Likewise. + * testsuite/20_util/shared_ptr/hash/1.cc: New. + * testsuite/20_util/unique_ptr/hash/1.cc: Likewise. + 2010-06-11 Jonathan Wakely * doc/xml/manual/appendix_contributing.xml: Indent code examples diff --git a/libstdc++-v3/include/bits/shared_ptr.h b/libstdc++-v3/include/bits/shared_ptr.h index f01630e5347..4f87dd459cf 100644 --- a/libstdc++-v3/include/bits/shared_ptr.h +++ b/libstdc++-v3/include/bits/shared_ptr.h @@ -514,6 +514,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std) std::forward<_Args>(__args)...); } + /// std::hash specialization for shared_ptr. + template + struct hash> + : public std::unary_function, size_t> + { + size_t + operator()(const shared_ptr<_Tp>& __s) const + { return std::hash<_Tp*>()(__s.get()); } + }; + // @} group pointer_abstractions _GLIBCXX_END_NAMESPACE diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index d4c8c8fb824..0a69d2b2411 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -1164,6 +1164,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std) std::forward<_Args>(__args)...); } + /// std::hash specialization for __shared_ptr. + template + struct hash<__shared_ptr<_Tp, _Lp>> + : public std::unary_function<__shared_ptr<_Tp, _Lp>, size_t> + { + size_t + operator()(const __shared_ptr<_Tp, _Lp>& __s) const + { return std::hash<_Tp*>()(__s.get()); } + }; + _GLIBCXX_END_NAMESPACE #endif // _SHARED_PTR_BASE_H diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 77d3f607dfd..ac62ff7f304 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -233,7 +233,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) // [unique.ptr.runtime] // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 740 - omit specialization for array objects with a compile time length - template + template class unique_ptr<_Tp[], _Tp_Deleter> { typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type; @@ -444,6 +444,19 @@ _GLIBCXX_BEGIN_NAMESPACE(std) const unique_ptr<_Up, _Up_Deleter>& __y) { return !(__x.get() < __y.get()); } + /// std::hash specialization for unique_ptr. + template + struct hash> + : public std::unary_function, size_t> + { + size_t + operator()(const unique_ptr<_Tp, _Tp_Deleter>& __u) const + { + typedef unique_ptr<_Tp, _Tp_Deleter> _UP; + return std::hash()(__u.get()); + } + }; + // @} group pointer_abstractions _GLIBCXX_END_NAMESPACE diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/hash/1.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/hash/1.cc new file mode 100644 index 00000000000..93f4739d782 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/hash/1.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++0x" } + +// 2010-06-11 Paolo Carlini + +// Copyright (C) 2010 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + struct T { }; + + std::shared_ptr s0(new T); + std::hash> hs0; + std::hash hp0; + + VERIFY( hs0(s0) == hp0(s0.get()) ); + + std::__shared_ptr s1(new T); + std::hash> hs1; + std::hash hp1; + + VERIFY( hs1(s1) == hp1(s1.get()) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/hash/1.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/hash/1.cc new file mode 100644 index 00000000000..53ece26ecff --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/hash/1.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++0x" } + +// 2010-06-11 Paolo Carlini + +// Copyright (C) 2010 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + struct T { }; + + std::unique_ptr u0(new T); + std::hash> hu0; + std::hash::pointer> hp0; + + VERIFY( hu0(u0) == hp0(u0.get()) ); + + std::unique_ptr u1(new T[10]); + std::hash> hu1; + std::hash::pointer> hp1; + + VERIFY( hu1(u1) == hp1(u1.get()) ); +} + +int main() +{ + test01(); + return 0; +}