Claw
1.7.3
|
The multi type map is a map in which values of different types can be associated to the same key.
In its original form, the goal of the claw::multi_type_map was to provide a simple and elegant solution to associate values of several types to keys in a map. This is a quite recurrent problem in programming. Indeed, a quick search on the Internet will result on questions like in this forum, or this other forum or eventually this last one.
One solution for this problem would be to store the values in several maps, and deal with them according to the context. An other solution would be to use a single map and to impose to the values to inherit from a given class. Both solutions are tricky and painful.
Hopefully, when using claw::multi_type_map you can obtain the same behavior without worrying. Plus, several functionalities are ready, such as exploring the whole set of pairs (key, value).
This section presents the creation and the exploration of a claw::multi_type_map.
The following program defines a multi_type_map
where the values of type bool
, float
or std::string
are associated to keys of type int
.
The first statement defines the list of the types of the values stored in the map. This list must end with claw::meta::no_type. Here, the claw::meta::type_list_maker type from the metaprogramming package with manage this for us.
Then you can insert values in the map:
You can also get the value associated with a given key for a given type:
Or even iterate on the pairs defined for a given type:
Exploring the map for a given type is a convenient thing. An other convenient thing would be to iterate over the map for all the types at once. This is what the claw::multi_type_map_visitor is made for.
First we have to define a function object that will receive the keys and the values. Our goal is to print the values in a stream, thus we define:
Then, all we have to do is to pass an instance of this function object to a claw::multi_type_map_visitor:
The following output is obtained from this program:
(0, 0) (1, 1) (10, 3.14159) (20, 1.61803) (30, 2.71828) (100, a string) (200, an other string)
This section presents the interfaces of the structures related to the claw::multi_type_map.
The claw::multi_type_map class is defined as follows. For the sake of clarity, the end of the recursion on the inheritance is not described here.
Instantiations of the claw::multi_type_map type must meet the following type requirements:
The claw::multi_type_map_visitor is defined as follows.
The type passed for the Function type argument of the claw::multi_type_map_visitor::run method must be a function object with an operator() similar to the following, where [text] means that text is optional.
template<typename T> [any_return_type] operator() (Key [const [&]], T [[const] &]) [const]
In other words:
Moreover, it is worth noting that any element can be inserted in the map during the visit and that removing the visited element from the map is allowed.