1 // boost/identifier.hpp ----------------------------------------------------// 2 3 // Copyright Beman Dawes 2006 4 5 // Distributed under the Boost Software License, Version 1.0. (See accompanying 6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7 8 // See documentation at http://www.boost.org/libs/utility 9 10 #ifndef BOOST_IDENTIFIER_HPP 11 #define BOOST_IDENTIFIER_HPP 12 13 #include <boost/utility/enable_if.hpp> 14 #include <boost/type_traits/is_base_of.hpp> 15 #include <iosfwd> 16 17 namespace boost 18 { 19 namespace detail 20 { 21 // class template identifier ---------------------------------------------// 22 23 // Always used as a base class so that different instantiations result in 24 // different class types even if instantiated with the same value type T. 25 26 // Expected usage is that T is often an integer type, best passed by 27 // value. There is no reason why T can't be a possibly larger class such as 28 // std::string, best passed by const reference. 29 30 // This implementation uses pass by value, based on expected common uses. 31 32 template <typename T, typename D> 33 class identifier 34 { 35 public: 36 typedef T value_type; 37 value() const38 const value_type value() const { return m_value; } assign(value_type v)39 void assign( value_type v ) { m_value = v; } 40 operator ==(const D & rhs) const41 bool operator==( const D & rhs ) const { return m_value == rhs.m_value; } operator !=(const D & rhs) const42 bool operator!=( const D & rhs ) const { return m_value != rhs.m_value; } operator <(const D & rhs) const43 bool operator< ( const D & rhs ) const { return m_value < rhs.m_value; } operator <=(const D & rhs) const44 bool operator<=( const D & rhs ) const { return m_value <= rhs.m_value; } operator >(const D & rhs) const45 bool operator> ( const D & rhs ) const { return m_value > rhs.m_value; } operator >=(const D & rhs) const46 bool operator>=( const D & rhs ) const { return m_value >= rhs.m_value; } 47 48 typedef void (*unspecified_bool_type)(D); // without the D, unspecified_bool_type unspecified_bool_true(D)49 static void unspecified_bool_true(D){} // conversion allows relational operators 50 // between different identifier types 51 operator unspecified_bool_type() const52 operator unspecified_bool_type() const { return m_value == value_type() ? 0 : unspecified_bool_true; } operator !() const53 bool operator!() const { return m_value == value_type(); } 54 55 // constructors are protected so that class can only be used as a base class 56 protected: identifier()57 identifier() {} identifier(value_type v)58 explicit identifier( value_type v ) : m_value(v) {} 59 60 private: 61 T m_value; 62 }; 63 64 //#ifndef BOOST_NO_SFINAE 65 66 // template <class Ostream, class Id> 67 // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, 68 // Ostream & >::type operator<<( Ostream & os, const Id & id ) 69 // { 70 // return os << id.value(); 71 // } 72 73 // template <class Istream, class Id> 74 // typename enable_if< is_base_of< identifier< typename Id::value_type, Id >, Id >, 75 // Istream & >::type operator>>( Istream & is, Id & id ) 76 // { 77 // typename Id::value_type v; 78 // is >> v; 79 // id.value( v ); 80 // return is; 81 // } 82 //#endif 83 84 } // namespace detail 85 } // namespace boost 86 87 #endif // BOOST_IDENTIFIER_HPP 88