LokiTypeInfo.h

00001 
00002 // The Loki Library
00003 // Copyright (c) 2001 by Andrei Alexandrescu
00004 // This code accompanies the book:
00005 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design 
00006 //     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
00007 // Permission to use, copy, modify, distribute and sell this software for any 
00008 //     purpose is hereby granted without fee, provided that the above copyright 
00009 //     notice appear in all copies and that both that copyright notice and this 
00010 //     permission notice appear in supporting documentation.
00011 // The author or Addison-Wesley Longman make no representations about the 
00012 //     suitability of this software for any purpose. It is provided "as is" 
00013 //     without express or implied warranty.
00015 #ifndef LOKI_LOKITYPEINFO_INC_
00016 #define LOKI_LOKITYPEINFO_INC_
00017 
00018 // $Id: LokiTypeInfo.h 748 2006-10-17 19:49:08Z syntheticpp $
00019 
00020 
00021 #include <typeinfo>
00022 #include <cassert>
00023 #include "Typelist.h"
00024 
00025 namespace Loki
00026 {
00028 // class TypeInfo
00029 // Purpose: offer a first-class, comparable wrapper over std::type_info
00031 
00032     class TypeInfo
00033     {
00034     public:
00035         // Constructors
00036         TypeInfo(); // needed for containers
00037         TypeInfo(const std::type_info&); // non-explicit
00038 
00039         // Access for the wrapped std::type_info
00040         const std::type_info& Get() const;
00041         // Compatibility functions
00042         bool before(const TypeInfo& rhs) const;
00043         const char* name() const;
00044 
00045     private:
00046         const std::type_info* pInfo_;
00047     };
00048     
00049 // Implementation
00050     
00051     inline TypeInfo::TypeInfo()
00052     {
00053         class Nil {};
00054         pInfo_ = &typeid(Nil);
00055         assert(pInfo_);
00056     }
00057     
00058     inline TypeInfo::TypeInfo(const std::type_info& ti)
00059     : pInfo_(&ti)
00060     { assert(pInfo_); }
00061     
00062     inline bool TypeInfo::before(const TypeInfo& rhs) const
00063     {
00064         assert(pInfo_);
00065         // type_info::before return type is int in some VC libraries 
00066         return pInfo_->before(*rhs.pInfo_) != 0;
00067     }
00068 
00069     inline const std::type_info& TypeInfo::Get() const
00070     {
00071         assert(pInfo_);
00072         return *pInfo_;
00073     }
00074     
00075     inline const char* TypeInfo::name() const
00076     {
00077         assert(pInfo_);
00078         return pInfo_->name();
00079     }
00080 
00081 // Comparison operators
00082     
00083     inline bool operator==(const TypeInfo& lhs, const TypeInfo& rhs)
00084     // type_info::operator== return type is int in some VC libraries
00085     { return (lhs.Get() == rhs.Get()) != 0; }
00086 
00087     inline bool operator<(const TypeInfo& lhs, const TypeInfo& rhs)
00088     { return lhs.before(rhs); }
00089 
00090     inline bool operator!=(const TypeInfo& lhs, const TypeInfo& rhs)
00091     { return !(lhs == rhs); }    
00092     
00093     inline bool operator>(const TypeInfo& lhs, const TypeInfo& rhs)
00094     { return rhs < lhs; }
00095     
00096     inline bool operator<=(const TypeInfo& lhs, const TypeInfo& rhs)
00097     { return !(lhs > rhs); }
00098      
00099     inline bool operator>=(const TypeInfo& lhs, const TypeInfo& rhs)
00100     { return !(lhs < rhs); }
00101 }
00102 
00103 #endif // end file guardian

Generated on Sun Feb 25 16:52:26 2007 for Loki by  doxygen 1.5.1-p1