]> git.lyx.org Git - lyx.git/blob - src/support/translator.h
Re-add the RasterImage template.
[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         // 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                                      lyx::equal_1st_in_pair<MapPair>(first)
66                                 );
67
68                 if (it != map.end()) {
69                         return it->second;
70                 } else {
71                         return default_t2;
72                 }
73         }
74
75         /// Find the mapping for the second argument
76         T1 const & find(T2 const & second) const {
77                 BOOST_ASSERT(!map.empty());
78
79                 // The idea is as follows:
80                 // find_if() will try to compare the data in the vector with
81                 // the value. The vector is made of pairs and the value has
82                 // the type of the second part of the pair.
83                 // We thus give find_if() an equal_to functor and assign to
84                 // its second post the value we want to compare. We now
85                 // compose the equal_to functor with the select2nd functor
86                 // to take only the second value of the pair to be compared.
87                 //
88                 // We can depict it as follows:
89                 // equal_to(select2nd(pair) , second)
90                 typename Map::const_iterator it =
91                         std::find_if(map.begin(), map.end(),
92                                      lyx::equal_2nd_in_pair<MapPair>(second)
93                                 );
94
95                 if (it != map.end())
96                         return it->first;
97                 else {
98                         return default_t1;
99                 }
100         }
101 private:
102         ///
103         Map map;
104         ///
105         T1 const default_t1;
106         ///
107         T2 const default_t2;
108 };
109
110 #endif // TRANSLATOR_H