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