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