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