Chris@16: // boost/identifier.hpp ----------------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes 2006 Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See documentation at http://www.boost.org/libs/utility Chris@16: Chris@16: #ifndef BOOST_IDENTIFIER_HPP Chris@16: #define BOOST_IDENTIFIER_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: // class template identifier ---------------------------------------------// Chris@16: Chris@16: // Always used as a base class so that different instantiations result in Chris@16: // different class types even if instantiated with the same value type T. Chris@16: Chris@16: // Expected usage is that T is often an integer type, best passed by Chris@16: // value. There is no reason why T can't be a possibly larger class such as Chris@16: // std::string, best passed by const reference. Chris@16: Chris@16: // This implementation uses pass by value, based on expected common uses. Chris@16: Chris@16: template Chris@16: class identifier Chris@16: { Chris@16: public: Chris@16: typedef T value_type; Chris@16: Chris@16: const value_type value() const { return m_value; } Chris@16: void assign( value_type v ) { m_value = v; } Chris@16: Chris@16: bool operator==( const D & rhs ) const { return m_value == rhs.m_value; } Chris@16: bool operator!=( const D & rhs ) const { return m_value != rhs.m_value; } Chris@16: bool operator< ( const D & rhs ) const { return m_value < rhs.m_value; } Chris@16: bool operator<=( const D & rhs ) const { return m_value <= rhs.m_value; } Chris@16: bool operator> ( const D & rhs ) const { return m_value > rhs.m_value; } Chris@16: bool operator>=( const D & rhs ) const { return m_value >= rhs.m_value; } Chris@16: Chris@16: typedef void (*unspecified_bool_type)(D); // without the D, unspecified_bool_type Chris@16: static void unspecified_bool_true(D){} // conversion allows relational operators Chris@16: // between different identifier types Chris@16: Chris@16: operator unspecified_bool_type() const { return m_value == value_type() ? 0 : unspecified_bool_true; } Chris@16: bool operator!() const { return m_value == value_type(); } Chris@16: Chris@16: // constructors are protected so that class can only be used as a base class Chris@16: protected: Chris@16: identifier() {} Chris@16: explicit identifier( value_type v ) : m_value(v) {} Chris@16: Chris@16: private: Chris@16: T m_value; Chris@16: }; Chris@16: Chris@16: //#ifndef BOOST_NO_SFINAE Chris@16: Chris@16: // template Chris@16: // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, Chris@16: // Ostream & >::type operator<<( Ostream & os, const Id & id ) Chris@16: // { Chris@16: // return os << id.value(); Chris@16: // } Chris@16: Chris@16: // template Chris@16: // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, Chris@16: // Istream & >::type operator>>( Istream & is, Id & id ) Chris@16: // { Chris@16: // typename Id::value_type v; Chris@16: // is >> v; Chris@16: // id.value( v ); Chris@16: // return is; Chris@16: // } Chris@16: //#endif Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_IDENTIFIER_HPP