00001 // boost/identifier.hpp ----------------------------------------------------// 00002 00003 // Copyright Beman Dawes 2006 00004 00005 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00007 00008 // See documentation at http://www.boost.org/libs/utility 00009 00010 #ifndef BOOST_IDENTIFIER_HPP 00011 #define BOOST_IDENTIFIER_HPP 00012 00013 #include <boost/utility/enable_if.hpp> 00014 #include <boost/type_traits/is_base_of.hpp> 00015 #include <iosfwd> 00016 00017 namespace boost 00018 { 00019 namespace detail 00020 { 00021 // class template identifier ---------------------------------------------// 00022 00023 // Always used as a base class so that different instantiations result in 00024 // different class types even if instantiated with the same value type T. 00025 00026 // Expected usage is that T is often an integer type, best passed by 00027 // value. There is no reason why T can't be a possibly larger class such as 00028 // std::string, best passed by const reference. 00029 00030 // This implementation uses pass by value, based on expected common uses. 00031 00032 template <typename T, typename D> 00033 class identifier 00034 { 00035 public: 00036 typedef T value_type; 00037 00038 const value_type value() const { return m_value; } 00039 void assign( value_type v ) { m_value = v; } 00040 00041 bool operator==( const D & rhs ) const { return m_value == rhs.m_value; } 00042 bool operator!=( const D & rhs ) const { return m_value != rhs.m_value; } 00043 bool operator< ( const D & rhs ) const { return m_value < rhs.m_value; } 00044 bool operator<=( const D & rhs ) const { return m_value <= rhs.m_value; } 00045 bool operator> ( const D & rhs ) const { return m_value > rhs.m_value; } 00046 bool operator>=( const D & rhs ) const { return m_value >= rhs.m_value; } 00047 00048 typedef void (*unspecified_bool_type)(D); // without the D, unspecified_bool_type 00049 static void unspecified_bool_true(D){} // conversion allows relational operators 00050 // between different identifier types 00051 00052 operator unspecified_bool_type() const { return m_value == value_type() ? 0 : unspecified_bool_true; } 00053 bool operator!() const { return m_value == value_type(); } 00054 00055 // constructors are protected so that class can only be used as a base class 00056 protected: 00057 identifier() {} 00058 explicit identifier( value_type v ) : m_value(v) {} 00059 00060 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // 1300 == VC++ 7.0 bug workaround 00061 private: 00062 #endif 00063 T m_value; 00064 }; 00065 00066 //#ifndef BOOST_NO_SFINAE 00067 00068 // template <class Ostream, class Id> 00069 // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, 00070 // Ostream & >::type operator<<( Ostream & os, const Id & id ) 00071 // { 00072 // return os << id.value(); 00073 // } 00074 00075 // template <class Istream, class Id> 00076 // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, 00077 // Istream & >::type operator>>( Istream & is, Id & id ) 00078 // { 00079 // typename Id::value_type v; 00080 // is >> v; 00081 // id.value( v ); 00082 // return is; 00083 // } 00084 //#endif 00085 00086 } // namespace detail 00087 } // namespace boost 00088 00089 #endif // BOOST_IDENTIFIER_HPP