]> git.lyx.org Git - lyx.git/blob - src/support/translator.h
Change Assert to BOOST_ASSERT.
[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 <vector>
16 #include <utility>
17 #include <algorithm>
18 #include <functional>
19
20 #include "support/lyxfunctional.h"
21 /**
22  * This class template is used to translate between two elements, specifically
23  * it was worked out to translate between an enum and strings when reading
24  * the lyx file.
25  *
26  * The two template arguments should be of different types.
27  */
28 template<typename T1, typename T2>
29 class Translator {
30 public:
31         ///
32         typedef T1 first_argument_type;
33         ///
34         typedef T2 second_argument_type;
35         ///
36         typedef std::pair<T1, T2> MapPair;
37         ///
38         typedef std::vector<MapPair> Map;
39
40         ///
41         Translator(T1 const & t1, T2 const & t2)
42                 : default_t1(t1), default_t2(t2)
43                 {}
44
45         /// Add a mapping to the translator.
46         void addPair(T1 const & first, T2 const & second) {
47                 map.push_back(MapPair(first, second));
48         }
49
50         /// Find the mapping for the first argument
51         T2 const & find(T1 const & first) const {
52                 BOOST_ASSERT(!map.empty());
53
54                 // For explanation see the next find() function.
55                 typename Map::const_iterator it =
56                         std::find_if(map.begin(), map.end(),
57                                      lyx::equal_1st_in_pair<MapPair>(first)
58                                 );
59
60                 if (it != map.end()) {
61                         return it->second;
62                 } else {
63                         return default_t2;
64                 }
65         }
66
67         /// Find the mapping for the second argument
68         T1 const & find(T2 const & second) const {
69                 BOOST_ASSERT(!map.empty());
70
71                 // The idea is as follows:
72                 // find_if() will try to compare the data in the vector with
73                 // the value. The vector is made of pairs and the value has
74                 // the type of the second part of the pair.
75                 // We thus give find_if() an equal_to functor and assign to
76                 // its second post the value we want to compare. We now
77                 // compose the equal_to functor with the select2nd functor
78                 // to take only the second value of the pair to be compared.
79                 //
80                 // We can depict it as follows:
81                 // equal_to(select2nd(pair) , second)
82                 typename Map::const_iterator it =
83                         std::find_if(map.begin(), map.end(),
84                                      lyx::equal_2nd_in_pair<MapPair>(second)
85                                 );
86
87                 if (it != map.end())
88                         return it->first;
89                 else {
90                         return default_t1;
91                 }
92         }
93 private:
94         ///
95         Map map;
96         ///
97         T1 const default_t1;
98         ///
99         T2 const default_t2;
100 };
101
102 #endif // TRANSLATOR_H