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/lyxfunctional.h"
22 * This class template is used to translate between two elements, specifically
23 * it was worked out to translate between an enum and strings when reading
26 * The two template arguments should be of different types.
28 template<typename T1, typename T2>
32 typedef T1 first_argument_type;
34 typedef T2 second_argument_type;
36 typedef std::pair<T1, T2> MapPair;
38 typedef std::vector<MapPair> Map;
41 Translator(T1 const & t1, T2 const & t2)
42 : default_t1(t1), default_t2(t2)
45 /// Add a mapping to the translator.
46 void addPair(T1 const & first, T2 const & second) {
47 map.push_back(MapPair(first, second));
50 /// Find the mapping for the first argument
51 T2 const & find(T1 const & first) const {
52 BOOST_ASSERT(!map.empty());
54 // For explanation see the next find() function.
55 typename Map::const_iterator it =
56 std::find_if(map.begin(), map.end(),
57 lyx::equal_1st_in_pair<MapPair>(first)
60 if (it != map.end()) {
67 /// Find the mapping for the second argument
68 T1 const & find(T2 const & second) const {
69 BOOST_ASSERT(!map.empty());
71 // The idea is as follows:
72 // find_if() will try to compare the data in the vector with
73 // the value. The vector is made of pairs and the value has
74 // the type of the second part of the pair.
75 // We thus give find_if() an equal_to functor and assign to
76 // its second post the value we want to compare. We now
77 // compose the equal_to functor with the select2nd functor
78 // to take only the second value of the pair to be compared.
80 // We can depict it as follows:
81 // equal_to(select2nd(pair) , second)
82 typename Map::const_iterator it =
83 std::find_if(map.begin(), map.end(),
84 lyx::equal_2nd_in_pair<MapPair>(second)
102 #endif // TRANSLATOR_H