4 * Copyright 1995-2002 the LyX Team
5 * Read the file COPYING
7 * \author Baruch Even <baruch@lyx.org>
18 #include "support/LAssert.h"
19 #include "support/lyxfunctional.h"
21 * This class template is used to translate between two elements, specifically
22 * it was worked out to translate between an enum and strings when reading
25 * The two template arguments should be of different types.
27 template<typename T1, typename T2>
31 typedef T1 first_argument_type;
33 typedef T2 second_argument_type;
35 typedef std::pair<T1, T2> MapPair;
37 typedef std::vector<MapPair> Map;
40 Translator(T1 const & t1, T2 const & t2)
41 : default_t1(t1), default_t2(t2)
44 /// Add a mapping to the translator.
45 void addPair(T1 const & first, T2 const & second) {
46 map.push_back(MapPair(first, second));
49 /// Find the mapping for the first argument
50 T2 const & find(T1 const & first) const {
51 lyx::Assert(!map.empty());
53 // For explanation see the next find() function.
54 typename Map::const_iterator it =
55 std::find_if(map.begin(), map.end(),
56 lyx::equal_1st_in_pair<MapPair>(first)
59 if (it != map.end()) {
66 /// Find the mapping for the second argument
67 T1 const & find(T2 const & second) const {
68 lyx::Assert(!map.empty());
70 // The idea is as follows:
71 // find_if() will try to compare the data in the vector with
72 // the value. The vector is made of pairs and the value has
73 // the type of the second part of the pair.
74 // We thus give find_if() an equal_to functor and assign to
75 // its second post the value we want to compare. We now
76 // compose the equal_to functor with the select2nd functor
77 // to take only the second value of the pair to be compared.
79 // We can depict it as follows:
80 // equal_to(select2nd(pair) , second)
81 typename Map::const_iterator it =
82 std::find_if(map.begin(), map.end(),
83 lyx::equal_2nd_in_pair<MapPair>(second)
101 #endif // TRANSLATOR_H