2003-06-18 09:56:10 +00:00
|
|
|
/**
|
2007-11-10 13:44:50 +00:00
|
|
|
* \file convert.cpp
|
2003-06-18 09:56:10 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
|
|
|
* \author Lars Gullik Bjønnes
|
2003-06-18 09:56:10 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-06-18 09:56:10 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-13 14:36:24 +00:00
|
|
|
#include <config.h>
|
2003-09-09 18:27:24 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/convert.h"
|
2006-09-09 22:27:22 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2004-09-04 12:13:50 +00:00
|
|
|
|
2005-01-07 13:32:26 +00:00
|
|
|
#include <string>
|
2014-07-12 17:12:08 +00:00
|
|
|
#include <sstream>
|
2012-11-18 16:42:02 +00:00
|
|
|
//needed for Mac OSX 10.5.2 Leopard
|
2008-03-29 01:45:24 +00:00
|
|
|
#include <cstdlib>
|
2005-01-07 13:32:26 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2017-03-24 15:44:14 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// A version of lexical cast that does not throw. Useful for when we convert to string
|
|
|
|
template<typename To, typename From>
|
|
|
|
To lexical_cast(From const & value, To const & defaultResult = To())
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return boost::lexical_cast<To>(value);
|
|
|
|
} catch(...) {
|
|
|
|
// Ignore all exceptions and use default.
|
|
|
|
return defaultResult;
|
|
|
|
}
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2017-03-24 15:44:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
2003-05-13 14:36:24 +00:00
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(bool b)
|
2003-05-13 14:36:24 +00:00
|
|
|
{
|
|
|
|
return (b ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(char c)
|
2003-05-13 14:36:24 +00:00
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return string(1, c);
|
2003-05-13 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(short unsigned int sui)
|
2003-05-14 07:54:12 +00:00
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return lexical_cast<string>(sui);
|
2003-05-14 07:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(int i)
|
2003-05-13 14:36:24 +00:00
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return lexical_cast<string>(i);
|
2003-05-13 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 11:18:13 +00:00
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(int i)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
return from_ascii(lexical_cast<string>(i));
|
2006-09-09 22:27:22 +00:00
|
|
|
}
|
2003-05-13 14:36:24 +00:00
|
|
|
|
2006-09-13 11:18:13 +00:00
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(unsigned int ui)
|
2003-05-13 14:36:24 +00:00
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return lexical_cast<string>(ui);
|
2003-05-13 14:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-13 11:18:13 +00:00
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(unsigned int ui)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
return from_ascii(lexical_cast<string>(ui));
|
2006-09-13 11:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-07 13:32:26 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(unsigned long ul)
|
|
|
|
{
|
|
|
|
return lexical_cast<string>(ul);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-13 15:35:18 +00:00
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(unsigned long ul)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
return from_ascii(lexical_cast<string>(ul));
|
2006-10-13 15:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 13:05:50 +00:00
|
|
|
#ifdef HAVE_LONG_LONG_INT
|
2016-05-05 11:05:12 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(unsigned long long ull)
|
|
|
|
{
|
|
|
|
return lexical_cast<string>(ull);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(unsigned long long ull)
|
|
|
|
{
|
|
|
|
return from_ascii(lexical_cast<string>(ull));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-01-07 17:54:12 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(long l)
|
|
|
|
{
|
|
|
|
return lexical_cast<string>(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-13 11:18:13 +00:00
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(long l)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
return from_ascii(lexical_cast<string>(l));
|
2006-09-13 11:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 13:05:50 +00:00
|
|
|
#ifdef HAVE_LONG_LONG_INT
|
2016-05-05 11:05:12 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(long long ll)
|
|
|
|
{
|
|
|
|
return lexical_cast<string>(ll);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(long long ll)
|
|
|
|
{
|
|
|
|
return from_ascii(lexical_cast<string>(ll));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(float f)
|
2003-05-13 14:36:24 +00:00
|
|
|
{
|
2014-07-12 17:12:08 +00:00
|
|
|
std::ostringstream val;
|
|
|
|
val << f;
|
|
|
|
return val.str();
|
2003-05-13 14:36:24 +00:00
|
|
|
}
|
2004-01-07 16:40:30 +00:00
|
|
|
|
|
|
|
|
2005-01-06 15:40:49 +00:00
|
|
|
template<>
|
|
|
|
string convert<string>(double d)
|
2004-01-07 16:40:30 +00:00
|
|
|
{
|
2014-07-12 17:12:08 +00:00
|
|
|
std::ostringstream val;
|
|
|
|
val << d;
|
|
|
|
return val.str();
|
2004-01-07 16:40:30 +00:00
|
|
|
}
|
2005-01-27 21:05:44 +00:00
|
|
|
|
|
|
|
|
Add caching for the QTextLayout objects we use
The QTextLayout handling is terribly slow on Qt 4.8.7, but some
caching has been added in Qt5 that makes it much faster. For some
reason, it is not that slow with Qt 4.8.1.
Caches are introduced for the three following methods
* width(doctring), controlled by CACHE_METRICS_WIDTH. This cache already
existed, but the code has been cleaned up
* getTextLayout, controlled by CACHE_METRICS_QTEXTLAYOUT (disabled by
default on Qt5, which does its own caching). This is used for pos2x
and x2pos and now for drawing of text too. The previous code used a
trivial caching scheme of the last used QTextLayout, but now they
are properly kept in a QCache. Moreover, the cacheEnabled() property
is enabled for these QTextLayout object (not sure what this does).
* breakAt, controlled by CACHE_METRICS_BREAKAT. This is the only user
of QTextLayout which did not have some kind of caching already.
For some weird reasons related to Argument-dependent look-up, the
qHash(docstring) function has to be defined in std namespace, since
lyx::docstring is actually std::basic_string<wchar_t>.
[NOTE: this version has profiling hooks, enabled by commenting out the line
#define DISABLE_PMPROF
that should eventually be removed.]
2016-07-05 12:06:22 +00:00
|
|
|
template<>
|
|
|
|
docstring convert<docstring>(double d)
|
|
|
|
{
|
|
|
|
return from_ascii(convert<string>(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
template<>
|
|
|
|
int convert<int>(string const s)
|
|
|
|
{
|
|
|
|
return strtol(s.c_str(), 0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 16:12:49 +00:00
|
|
|
template<>
|
|
|
|
int convert<int>(docstring const s)
|
|
|
|
{
|
2007-11-10 13:44:50 +00:00
|
|
|
return strtol(to_ascii(s).c_str(), 0, 10);
|
2006-10-20 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
template<>
|
|
|
|
unsigned int convert<unsigned int>(string const s)
|
|
|
|
{
|
|
|
|
return strtoul(s.c_str(), 0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-17 08:31:00 +00:00
|
|
|
template<>
|
|
|
|
unsigned long convert<unsigned long>(string const s)
|
|
|
|
{
|
|
|
|
return strtoul(s.c_str(), 0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-27 21:05:44 +00:00
|
|
|
template<>
|
|
|
|
double convert<double>(string const s)
|
|
|
|
{
|
|
|
|
return strtod(s.c_str(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
int convert<int>(char const * cptr)
|
|
|
|
{
|
|
|
|
return strtol(cptr, 0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
double convert<double>(char const * cptr)
|
|
|
|
{
|
|
|
|
return strtod(cptr, 0);
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|