Chris@16
|
1 // Boost.Bimap
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright (c) 2006-2007 Matias Capeletto
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 /// \file tags/tagged.hpp
|
Chris@16
|
10 /// \brief Defines the tagged class
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_BIMAP_TAGS_TAGGED_HPP
|
Chris@16
|
13 #define BOOST_BIMAP_TAGS_TAGGED_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #if defined(_MSC_VER) && (_MSC_VER>=1200)
|
Chris@16
|
16 #pragma once
|
Chris@16
|
17 #endif
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost {
|
Chris@16
|
22 namespace bimaps {
|
Chris@16
|
23
|
Chris@16
|
24 /// \brief A light non-invasive idiom to tag a type.
|
Chris@16
|
25 /**
|
Chris@16
|
26
|
Chris@16
|
27 There are a lot of ways of tagging a type. The standard library for example
|
Chris@16
|
28 defines tags (empty structs) that are then inherited by the tagged class. To
|
Chris@16
|
29 support built-in types and other types that simple cannot inherit from the
|
Chris@16
|
30 tag, the standard builds another level of indirection. An example of this is
|
Chris@16
|
31 the type_traits metafunction. This approach is useful if the tags are intended
|
Chris@16
|
32 to be used in the library internals, and if the user does not have to create
|
Chris@16
|
33 new tagged types often.
|
Chris@16
|
34
|
Chris@16
|
35 Boost.MultiIndex is an example of a library that defines a tagged idiom that
|
Chris@16
|
36 is better suited to the user. As an option, in the indexed by declaration
|
Chris@16
|
37 of a multi-index container a user can \b attach a tag to each index, so it
|
Chris@16
|
38 can be referred by it instead of by the index number. It is a very user
|
Chris@16
|
39 friendly way of specifying a tag but is very invasive from the library writer's
|
Chris@16
|
40 point of view. Each index must now support this additional parameter. Maybe
|
Chris@16
|
41 not in the case of the multi-index container, but in simpler classes
|
Chris@16
|
42 the information of the tags is used by the father class rather than by the
|
Chris@16
|
43 tagged types.
|
Chris@16
|
44
|
Chris@16
|
45 \b tagged is a light non-invasive idiom to tag a type. It is very intuitive
|
Chris@16
|
46 and user-friendly. With the use of the defined metafunctions the library
|
Chris@16
|
47 writer can enjoy the coding too.
|
Chris@16
|
48
|
Chris@16
|
49 **/
|
Chris@16
|
50
|
Chris@16
|
51 namespace tags {
|
Chris@16
|
52
|
Chris@16
|
53 /// \brief The tag holder
|
Chris@16
|
54 /**
|
Chris@16
|
55
|
Chris@16
|
56 The idea is to add a level of indirection to the type being tagged. With this
|
Chris@16
|
57 class you wrapped a type and apply a tag to it. The only thing to remember is
|
Chris@16
|
58 that if you write
|
Chris@16
|
59
|
Chris@16
|
60 \code
|
Chris@16
|
61 typedef tagged<type,tag> taggedType;
|
Chris@16
|
62 \endcode
|
Chris@16
|
63
|
Chris@16
|
64 Then instead to use directly the tagged type, in order to access it you have
|
Chris@16
|
65 to write \c taggedType::value_type. The tag can be obtained using \c taggedType::tag.
|
Chris@16
|
66 The idea is not to use this metadata directly but rather using the metafunctions
|
Chris@16
|
67 that are defined in the support namespace. With this metafunctions you can work
|
Chris@16
|
68 with tagged and untagged types in a consistent way. For example, the following
|
Chris@16
|
69 code is valid:
|
Chris@16
|
70
|
Chris@16
|
71 \code
|
Chris@16
|
72 BOOST_STATIC_ASSERT( is_same< value_type_of<taggedType>, value_type_of<type> >::value );
|
Chris@16
|
73 \endcode
|
Chris@16
|
74
|
Chris@16
|
75 The are other useful metafunctions there too.
|
Chris@16
|
76 See also value_type_of, tag_of, is_tagged, apply_to_value_type.
|
Chris@16
|
77
|
Chris@16
|
78 \ingroup tagged_group
|
Chris@16
|
79 **/
|
Chris@16
|
80 template< class Type, class Tag >
|
Chris@16
|
81 struct tagged
|
Chris@16
|
82 {
|
Chris@16
|
83 typedef Type value_type;
|
Chris@16
|
84 typedef Tag tag;
|
Chris@16
|
85 };
|
Chris@16
|
86
|
Chris@16
|
87 } // namespace tags
|
Chris@16
|
88 } // namespace bimaps
|
Chris@16
|
89 } // namespace boost
|
Chris@16
|
90
|
Chris@16
|
91 /** \namespace boost::bimaps::tags::support
|
Chris@16
|
92 \brief Metafunctions to work with tagged types.
|
Chris@16
|
93
|
Chris@16
|
94 This metafunctions aims to make easier the manage of tagged types. They are all mpl
|
Chris@16
|
95 compatible metafunctions and can be used with lambda expresions.
|
Chris@16
|
96 The metafunction value_type_of and tag_of get the data in a tagged type in a secure
|
Chris@16
|
97 and consistent way.
|
Chris@16
|
98 default_tagged and overwrite_tagged allows to work with the tag of a tagged type,
|
Chris@16
|
99 and apply_to_value_type is a higher order metafunction that allow the user to change
|
Chris@16
|
100 the type of a TaggedType.
|
Chris@16
|
101 **/
|
Chris@16
|
102
|
Chris@16
|
103 #endif // BOOST_BIMAP_TAGS_TAGGED_HPP
|
Chris@16
|
104
|
Chris@16
|
105
|
Chris@16
|
106
|
Chris@16
|
107
|