libstdc++
typeindex
Go to the documentation of this file.
00001 // C++0x typeindex -*- C++ -*-
00002 
00003 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/typeindex
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_TYPEINDEX
00030 #define _GLIBCXX_TYPEINDEX 1
00031 
00032 #pragma GCC system_header
00033 
00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <typeinfo>
00039 
00040 namespace std _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00044   /**
00045      @brief The class type_index provides a simple wrapper for type_info
00046      which can be used as an index type in associative containers (23.6)
00047      and in unordered associative containers (23.7).
00048    */
00049   struct type_index
00050   {
00051     type_index(const type_info& __rhs) noexcept
00052     : _M_target(&__rhs) { }
00053 
00054     bool
00055     operator==(const type_index& __rhs) const noexcept
00056     { return *_M_target == *__rhs._M_target; }
00057 
00058     bool
00059     operator!=(const type_index& __rhs) const noexcept
00060     { return *_M_target != *__rhs._M_target; }
00061 
00062     bool
00063     operator<(const type_index& __rhs) const noexcept
00064     { return _M_target->before(*__rhs._M_target); }
00065 
00066     bool
00067     operator<=(const type_index& __rhs) const noexcept
00068     { return !__rhs._M_target->before(*_M_target); }
00069 
00070     bool
00071     operator>(const type_index& __rhs) const noexcept
00072     { return __rhs._M_target->before(*_M_target); }
00073 
00074     bool
00075     operator>=(const type_index& __rhs) const noexcept
00076     { return !_M_target->before(*__rhs._M_target); }
00077 
00078     size_t
00079     hash_code() const noexcept
00080     { return _M_target->hash_code(); }
00081 
00082     const char*
00083     name() const
00084     { return _M_target->name(); }
00085 
00086   private:
00087     const type_info* _M_target;
00088   };
00089 
00090   template<typename _Tp> struct hash;
00091 
00092   /// std::hash specialization for type_index.
00093   template<>
00094     struct hash<type_index>
00095     {
00096       typedef size_t        result_type;
00097       typedef type_index  argument_type;
00098 
00099       size_t
00100       operator()(const type_index& __ti) const noexcept
00101       { return __ti.hash_code(); }
00102     };
00103 
00104 _GLIBCXX_END_NAMESPACE_VERSION
00105 } // namespace
00106 
00107 #endif  // __GXX_EXPERIMENTAL_CXX0X__
00108 
00109 #endif  // _GLIBCXX_TYPEINDEX