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