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 const & 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 const & 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)
66 /// Add a mapping to the translator.
67 void addPair(T1 const & first, T2 const & second) {
68 map.push_back(MapPair(first, second));
71 /// Find the mapping for the first argument
72 T2 const & find(T1 const & first) const {
73 #ifdef ENABLE_ASSERTIONS
74 Assert( ! map.empty());
77 // For explanation see the next find() function.
78 Map::const_iterator it =
79 std::find_if(map.begin(), map.end(),
80 equal_1st_in_pair<T1, T2>(first)
90 /// Find the mapping for the second argument
91 T1 const & find(T2 const & second) const {
92 #ifdef ENABLE_ASSERTIONS
93 Assert( ! map.empty());
96 // The idea is as follows:
97 // find_if() will try to compare the data in the vector with the value.
98 // The vector is made of pairs and the value has the type of the
99 // second part of the pair.
100 // We thus give find_if() an equal_to functor and assign to its second
101 // post the value we want to compare. We now compose the equal_to
102 // functor with the select2nd functor to take only the second value
103 // of the pair to be compared.
105 // We can depict it as follows:
106 // equal_to( select2nd(pair) , second)
107 Map::const_iterator it =
108 std::find_if(map.begin(), map.end(),
109 equal_2nd_in_pair<T1, T2>(second)
120 typedef std::pair<T1, T2> MapPair;
121 typedef std::vector<MapPair> Map;