![]() |
Home | Libraries | People | FAQ | More |
The code produced in this fashion tends to be obscure and error-prone. The bimap family lets you tag your types so they can be accessed by a more descriptive name. This produces more readable code.
A tagged type is a type that has been labelled using a tag. A tag is any
valid C++ type. In a bimap, the types are always tagged. If you do not specify
your own tag, the container uses member_at::left
and member_at::right to tag the left and right sides respectively.
In order to specify a custom tag, the set type specification of each side
has to be tagged. Tagging a type is very simple:
typedef tagged< int, a_tag > tagged_int;
For example, if you want to use the tag id
and name a bimap from a set
of ids to a multiset of names such as this one:
typedef bimap < int, multiset_of<string> > People; People people; // ... people.left[28928546] = "Gray Simpson"; people.right.insert( People::right_value_type("Marta Smith",30215692) ); cout << (*people.begin()).left;
you can rewrite it as:
struct id {}; // Tag for the identification number struct name {}; // Tag for the name of the person typedef bimap < tagged< int , id >, tagged< multiset_of<string>, name > > People; People people; // ... map_by<id>(people)[28928546] = "Gray Simpson"; map_by<name>(people).insert( value_type_by<name,People>("Marta Smith",30215692) ); cout << get<id>(*people.begin());
Here are the functions and metafunctions you can use with tags. In each,
the tag parameter can be either a user-defined tag or member_at::{side}, even when there is no user-defined tag.
This allows you to use this function in non-tagged bimaps, and then be able
to tag them later without changing the code.
Metafunctions
template< class Tag, class Bimap > struct value_type_by { typedef typename Bimap::{side}_value_type type; };
template< class Tag, class Bimap > struct key_type_by { typedef typename Bimap::{side}_key_type type; };
template< class Tag, class Bimap > struct data_type_by { typedef typename Bimap::{side}_data_type type; };
template< class Tag, class Bimap > struct iterator_type_by { typedef typename Bimap::{side}_iterator type; }; template< class Tag, class Bimap > struct const_iterator_type_by { typedef typename Bimap::{side}_iterator type; }; // The following metafunctions are valid only if the selected views // support these types of iteration. template< class Tag, class Bimap > struct reverse_iterator_type_by { typedef -unspecified- type; }; template< class Tag, class Bimap > struct const_reverse_iterator_type_by { typedef -unspecified- type; }; template< class Tag, class Bimap > struct local_iterator_type_by { typedef -unspecified- type; }; template< class Tag, class Bimap > struct const_local_iterator_type_by { typedef -unspecified- type; };
Functions
template<class Tag, class Relation> result_of::map_by< Tag, Bimap>::type map_by(Bimap &);
metafunctions
template< class Tag, class Relation > struct value_type_of { typedef typename Relation::{side}_type type; };
template< class Tag, class Relation > struct pair_type_by { typedef {compatible with std::pair} type; };
Functions
template<class Tag, class Relation> result_of::get< Tag, Relation>::type get(Relation &r);
template<class Tag, class Relation> result_of::pair_by< Tag, Relation>::type pair_by(Relation &);
| Copyright © 2006 Matias Capeletto |