2000-07-31 12:30:10 +00:00
|
|
|
// -*- C++ -*-
|
2002-05-30 02:24:33 +00:00
|
|
|
/**
|
|
|
|
* \file translator.h
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author Baruch Even
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-05-30 02:24:33 +00:00
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
#ifndef TRANSLATOR_H
|
|
|
|
#define TRANSLATOR_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
#include "support/LAssert.h"
|
2000-10-11 21:06:43 +00:00
|
|
|
#include "support/lyxfunctional.h"
|
2002-05-30 02:24:33 +00:00
|
|
|
/**
|
|
|
|
* This class template is used to translate between two elements, specifically
|
|
|
|
* it was worked out to translate between an enum and strings when reading
|
|
|
|
* the lyx file.
|
|
|
|
*
|
|
|
|
* The two template arguments should be of different types.
|
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
class Translator {
|
|
|
|
public:
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
|
|
|
typedef T1 first_argument_type;
|
|
|
|
///
|
|
|
|
typedef T2 second_argument_type;
|
|
|
|
///
|
|
|
|
typedef std::pair<T1, T2> MapPair;
|
|
|
|
///
|
|
|
|
typedef std::vector<MapPair> Map;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
2002-03-21 17:09:55 +00:00
|
|
|
Translator(T1 const & t1, T2 const & t2)
|
|
|
|
: default_t1(t1), default_t2(t2)
|
2000-09-14 17:53:12 +00:00
|
|
|
{}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
/// Add a mapping to the translator.
|
|
|
|
void addPair(T1 const & first, T2 const & second) {
|
|
|
|
map.push_back(MapPair(first, second));
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
/// Find the mapping for the first argument
|
|
|
|
T2 const & find(T1 const & first) const {
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(!map.empty());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
// For explanation see the next find() function.
|
2002-04-05 19:54:17 +00:00
|
|
|
typename Map::const_iterator it =
|
2000-09-14 17:53:12 +00:00
|
|
|
std::find_if(map.begin(), map.end(),
|
2002-02-16 15:59:55 +00:00
|
|
|
lyx::equal_1st_in_pair<MapPair>(first)
|
2000-09-14 17:53:12 +00:00
|
|
|
);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
if (it != map.end()) {
|
2001-07-12 11:11:10 +00:00
|
|
|
return it->second;
|
2000-09-14 17:53:12 +00:00
|
|
|
} else {
|
|
|
|
return default_t2;
|
|
|
|
}
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
/// Find the mapping for the second argument
|
|
|
|
T1 const & find(T2 const & second) const {
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(!map.empty());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
// The idea is as follows:
|
|
|
|
// find_if() will try to compare the data in the vector with
|
|
|
|
// the value. The vector is made of pairs and the value has
|
2002-03-21 17:09:55 +00:00
|
|
|
// the type of the second part of the pair.
|
2000-09-14 17:53:12 +00:00
|
|
|
// We thus give find_if() an equal_to functor and assign to
|
|
|
|
// its second post the value we want to compare. We now
|
|
|
|
// compose the equal_to functor with the select2nd functor
|
|
|
|
// to take only the second value of the pair to be compared.
|
|
|
|
//
|
|
|
|
// We can depict it as follows:
|
2002-02-16 15:59:55 +00:00
|
|
|
// equal_to(select2nd(pair) , second)
|
2002-04-05 19:54:17 +00:00
|
|
|
typename Map::const_iterator it =
|
2000-09-14 17:53:12 +00:00
|
|
|
std::find_if(map.begin(), map.end(),
|
2002-02-16 15:59:55 +00:00
|
|
|
lyx::equal_2nd_in_pair<MapPair>(second)
|
2000-09-14 17:53:12 +00:00
|
|
|
);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
if (it != map.end())
|
2001-07-12 11:11:10 +00:00
|
|
|
return it->first;
|
2000-09-14 17:53:12 +00:00
|
|
|
else {
|
|
|
|
return default_t1;
|
|
|
|
}
|
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
private:
|
2000-09-14 17:53:12 +00:00
|
|
|
///
|
|
|
|
Map map;
|
|
|
|
///
|
|
|
|
T1 const default_t1;
|
|
|
|
///
|
|
|
|
T2 const default_t2;
|
2000-07-31 12:30:10 +00:00
|
|
|
};
|
|
|
|
|
2002-05-30 02:24:33 +00:00
|
|
|
#endif // TRANSLATOR_H
|