]> git.lyx.org Git - lyx.git/blob - src/support/translator.h
the convert patch
[lyx.git] / src / support / translator.h
1 // -*- C++ -*-
2 /**
3  * \file translator.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Baruch Even
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef TRANSLATOR_H
13 #define TRANSLATOR_H
14
15 #include <boost/assert.hpp>
16 #include <boost/bind.hpp>
17
18 #include <vector>
19 #include <utility>
20 #include <algorithm>
21 #include <functional>
22
23 /**
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
26  * the lyx file.
27  *
28  * The two template arguments should be of different types.
29  */
30 template<typename T1, typename T2>
31 class Translator {
32 public:
33         ///
34         typedef T1 first_argument_type;
35         ///
36         typedef T2 second_argument_type;
37         ///
38         typedef std::pair<T1, T2> MapPair;
39         ///
40         typedef std::vector<MapPair> Map;
41
42         ///
43         Translator(T1 const & t1, T2 const & t2)
44                 : default_t1(t1), default_t2(t2)
45                 {}
46
47         /// Add a mapping to the translator.
48         void addPair(T1 const & first, T2 const & second) {
49                 map.push_back(MapPair(first, second));
50         }
51         // Add the contents of \c other
52         void add(Translator const & other) {
53                 if (other.map.empty())
54                         return;
55                 map.insert(map.end(), other.map.begin(), other.map.end());
56         }
57
58         /// Find the mapping for the first argument
59         T2 const & find(T1 const & first) const {
60                 BOOST_ASSERT(!map.empty());
61
62                 // For explanation see the next find() function.
63                 typename Map::const_iterator it =
64                         std::find_if(map.begin(), map.end(),
65                                      boost::bind(std::equal_to<T1>(),
66                                                  boost::bind(&MapPair::first, _1),
67                                                  first)
68                                 );
69
70                 if (it != map.end()) {
71                         return it->second;
72                 } else {
73                         return default_t2;
74                 }
75         }
76
77         /// Find the mapping for the second argument
78         T1 const & find(T2 const & second) const {
79                 BOOST_ASSERT(!map.empty());
80
81                 // The idea is as follows:
82                 // find_if() will try to compare the data in the vector with
83                 // the value. The vector is made of pairs and the value has
84                 // the type of the second part of the pair.
85                 // We thus give find_if() an equal_to functor and assign to
86                 // its second post the value we want to compare. We now
87                 // compose the equal_to functor with the select2nd functor
88                 // to take only the second value of the pair to be compared.
89                 //
90                 // We can depict it as follows:
91                 // equal_to(select2nd(pair) , second)
92                 typename Map::const_iterator it =
93                         std::find_if(map.begin(), map.end(),
94                                      boost::bind(std::equal_to<T2>(),
95                                                  boost::bind(&MapPair::second, _1),
96                                                  second)
97                                 );
98
99                 if (it != map.end())
100                         return it->first;
101                 else {
102                         return default_t1;
103                 }
104         }
105 private:
106         ///
107         Map map;
108         ///
109         T1 const default_t1;
110         ///
111         T2 const default_t2;
112 };
113
114 #endif // TRANSLATOR_H