]> git.lyx.org Git - lyx.git/blob - src/support/translator.h
Add #include <boost/assert.hpp>.
[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
17 #include <vector>
18 #include <utility>
19 #include <algorithm>
20 #include <functional>
21
22 #include "support/lyxfunctional.h"
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
52         /// Find the mapping for the first argument
53         T2 const & find(T1 const & first) const {
54                 BOOST_ASSERT(!map.empty());
55
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)
60                                 );
61
62                 if (it != map.end()) {
63                         return it->second;
64                 } else {
65                         return default_t2;
66                 }
67         }
68
69         /// Find the mapping for the second argument
70         T1 const & find(T2 const & second) const {
71                 BOOST_ASSERT(!map.empty());
72
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.
81                 //
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)
87                                 );
88
89                 if (it != map.end())
90                         return it->first;
91                 else {
92                         return default_t1;
93                 }
94         }
95 private:
96         ///
97         Map map;
98         ///
99         T1 const default_t1;
100         ///
101         T2 const default_t2;
102 };
103
104 #endif // TRANSLATOR_H