4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include <boost/assert.hpp>
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
28 * The two template arguments should be of different types.
30 template<typename T1, typename T2>
34 typedef T1 first_argument_type;
36 typedef T2 second_argument_type;
38 typedef std::pair<T1, T2> MapPair;
40 typedef std::vector<MapPair> Map;
42 typedef typename Map::const_iterator const_iterator;
45 Translator(T1 const & t1, T2 const & t2)
46 : default_t1(t1), default_t2(t2)
49 /// Add a mapping to the translator.
50 void addPair(T1 const & first, T2 const & second)
52 map.push_back(MapPair(first, second));
55 // Add the contents of \c other
56 void add(Translator const & other)
58 if (other.map.empty())
60 map.insert(map.end(), other.map.begin(), other.map.end());
63 /// Find the mapping for the first argument
64 T2 const & find(T1 const & first) const
66 BOOST_ASSERT(!map.empty());
67 const_iterator it = map.begin();
68 const_iterator end = map.end();
69 for (; it != end; ++it)
70 if (it->first == first)
75 /// Find the mapping for the second argument
76 T1 const & find(T2 const & second) const
78 BOOST_ASSERT(!map.empty());
79 const_iterator it = map.begin();
80 const_iterator end = map.end();
81 for (; it != end; ++it)
82 if (it->second == second)
98 #endif // TRANSLATOR_H