identifier.hpp 3.1 KB

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