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