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