tagged.hpp 3.7 KB

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