2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
21 // Functors used in the template.
22 template<typename T1, typename T2>
23 class equal_1st_in_pair {
25 equal_1st_in_pair(T1 const & value) : value_(value) {}
27 typedef std::pair<T1, T2> pair_type;
28 bool operator() (pair_type p) const {
29 return p.first == value_;
35 template<typename T1, typename T2>
36 class equal_2nd_in_pair {
38 equal_2nd_in_pair(T2 const & value) : value_(value) {}
40 typedef std::pair<T1, T2> pair_type;
41 bool operator() (pair_type p) const {
42 return p.second == value_;
48 /** This class template is used to translate between two elements, specifically
49 * it was worked out to translate between an enum and strings when reading
52 * The two template arguments should be of different types.
55 template<typename T1, typename T2>
58 typedef T1 first_argument_type;
59 typedef T2 second_argument_type;
62 Translator(T1 const & t1, T2 const & t2)
63 : default_t1(t1), default_t2(t2)
65 /// d-tor. Not virtual since it's not going to be inherited.
68 /// Add a mapping to the translator.
69 void addPair(T1 const & first, T2 const & second) {
70 map.push_back(MapPair(first, second));
73 /// Find the mapping for the first argument
74 T2 const & find(T1 const & first) const {
75 #ifdef ENABLE_ASSERTIONS
76 Assert( ! map.empty());
79 // For explanation see the next find() function.
80 Map::const_iterator it =
81 std::find_if(map.begin(), map.end(),
82 equal_1st_in_pair<T1, T2>(first)
92 /// Find the mapping for the second argument
93 T1 const & find(T2 const & second) const {
94 #ifdef ENABLE_ASSERTIONS
95 Assert( ! map.empty());
98 // The idea is as follows:
99 // find_if() will try to compare the data in the vector with the value.
100 // The vector is made of pairs and the value has the type of the
101 // second part of the pair.
102 // We thus give find_if() an equal_to functor and assign to its second
103 // post the value we want to compare. We now compose the equal_to
104 // functor with the select2nd functor to take only the second value
105 // of the pair to be compared.
107 // We can depict it as follows:
108 // equal_to( select2nd(pair) , second)
109 Map::const_iterator it =
110 std::find_if(map.begin(), map.end(),
111 equal_2nd_in_pair<T1, T2>(second)
122 typedef std::pair<T1, T2> MapPair;
123 typedef std::vector<MapPair> Map;