libstdc++
typeindex
Go to the documentation of this file.
00001 // C++11 <typeindex> -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2015 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 #if __cplusplus < 201103L
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 Class type_index
00046    * @ingroup utilities
00047    *
00048    *  The class type_index provides a simple wrapper for type_info
00049    *  which can be used as an index type in associative containers
00050    *  (23.6) and in unordered associative containers (23.7).
00051    */
00052   struct type_index
00053   {
00054     type_index(const type_info& __rhs) noexcept
00055     : _M_target(&__rhs) { }
00056 
00057     bool
00058     operator==(const type_index& __rhs) const noexcept
00059     { return *_M_target == *__rhs._M_target; }
00060 
00061     bool
00062     operator!=(const type_index& __rhs) const noexcept
00063     { return *_M_target != *__rhs._M_target; }
00064 
00065     bool
00066     operator<(const type_index& __rhs) const noexcept
00067     { return _M_target->before(*__rhs._M_target); }
00068 
00069     bool
00070     operator<=(const type_index& __rhs) const noexcept
00071     { return !__rhs._M_target->before(*_M_target); }
00072 
00073     bool
00074     operator>(const type_index& __rhs) const noexcept
00075     { return __rhs._M_target->before(*_M_target); }
00076 
00077     bool
00078     operator>=(const type_index& __rhs) const noexcept
00079     { return !_M_target->before(*__rhs._M_target); }
00080 
00081     size_t
00082     hash_code() const noexcept
00083     { return _M_target->hash_code(); }
00084 
00085     const char*
00086     name() const noexcept
00087     { return _M_target->name(); }
00088 
00089   private:
00090     const type_info* _M_target;
00091   };
00092 
00093   template<typename _Tp> struct hash;
00094 
00095   /// std::hash specialization for type_index.
00096   template<>
00097     struct hash<type_index>
00098     {
00099       typedef size_t        result_type;
00100       typedef type_index  argument_type;
00101 
00102       size_t
00103       operator()(const type_index& __ti) const noexcept
00104       { return __ti.hash_code(); }
00105     };
00106 
00107 _GLIBCXX_END_NAMESPACE_VERSION
00108 } // namespace std
00109 
00110 #endif  // C++11
00111 
00112 #endif  // _GLIBCXX_TYPEINDEX