]> git.lyx.org Git - lyx.git/blob - src/support/translator.h
small changes read 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
61     /// c-tor.
62     Translator(T1 const & t1, T2 const & t2) 
63         : default_t1(t1), default_t2(t2) 
64         {}
65
66     /// Add a mapping to the translator.
67     void addPair(T1 const & first, T2 const & second) {
68         map.push_back(MapPair(first, second));
69     }
70
71     /// Find the mapping for the first argument
72     T2 const & find(T1 const & first) const {
73 #ifdef ENABLE_ASSERTIONS
74         Assert( ! map.empty());
75 #endif
76
77         // For explanation see the next find() function.
78         Map::const_iterator it =
79             std::find_if(map.begin(), map.end(),
80                     equal_1st_in_pair<T1, T2>(first)
81                         );
82
83         if (it != map.end())
84             return (*it).second;
85         else {
86             return default_t2;
87         }
88     }
89
90     /// Find the mapping for the second argument
91     T1 const & find(T2 const & second) const {
92 #ifdef ENABLE_ASSERTIONS
93         Assert( ! map.empty());
94 #endif
95
96         // The idea is as follows:
97         // find_if() will try to compare the data in the vector with the value.
98         // The vector is made of pairs and the value has the type of the
99         // second part of the pair. 
100         // We thus give find_if() an equal_to functor and assign to its second
101         // post the value we want to compare. We now compose the equal_to 
102         // functor with the select2nd functor to take only the second value
103         // of the pair to be compared.
104         //
105         // We can depict it as follows:
106         // equal_to( select2nd(pair) , second)
107         Map::const_iterator it =
108             std::find_if(map.begin(), map.end(),
109                     equal_2nd_in_pair<T1, T2>(second)
110                         );
111
112         if (it != map.end())
113             return (*it).first;
114         else {
115             return default_t1;
116         }
117     }
118
119 private:
120     typedef std::pair<T1, T2> MapPair;
121     typedef std::vector<MapPair> Map;
122
123     Map map;
124
125     const T1 default_t1;
126     const T2 default_t2;
127
128 };
129
130 #endif