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