4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h"
23 * This class template is used to translate between two elements, specifically
24 * it was worked out to translate between an enum and strings when reading
27 * The two template arguments should be of different types.
29 template<typename T1, typename T2>
33 typedef T1 first_argument_type;
35 typedef T2 second_argument_type;
37 typedef std::pair<T1, T2> MapPair;
39 typedef std::vector<MapPair> Map;
42 Translator(T1 const & t1, T2 const & t2)
43 : default_t1(t1), default_t2(t2)
46 /// Add a mapping to the translator.
47 void addPair(T1 const & first, T2 const & second) {
48 map.push_back(MapPair(first, second));
51 /// Find the mapping for the first argument
52 T2 const & find(T1 const & first) const {
53 lyx::Assert(!map.empty());
55 // For explanation see the next find() function.
56 typename Map::const_iterator it =
57 std::find_if(map.begin(), map.end(),
58 lyx::equal_1st_in_pair<MapPair>(first)
61 if (it != map.end()) {
68 /// Find the mapping for the second argument
69 T1 const & find(T2 const & second) const {
70 lyx::Assert(!map.empty());
72 // The idea is as follows:
73 // find_if() will try to compare the data in the vector with
74 // the value. The vector is made of pairs and the value has
75 // the type of the second part of the pair.
76 // We thus give find_if() an equal_to functor and assign to
77 // its second post the value we want to compare. We now
78 // compose the equal_to functor with the select2nd functor
79 // to take only the second value of the pair to be compared.
81 // We can depict it as follows:
82 // equal_to(select2nd(pair) , second)
83 typename Map::const_iterator it =
84 std::find_if(map.begin(), map.end(),
85 lyx::equal_2nd_in_pair<MapPair>(second)
103 #endif // TRANSLATOR_H