Made use of the "new" lexical_cast implementation
Note the "old" implementation of lexical_cast_default is still used since the new code doesn't have the equivalent functionality yet.
This commit is contained in:
parent
1202eb160e
commit
626d6c63cb
16 changed files with 30 additions and 191 deletions
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "ai/manager.hpp"
|
||||
#include "terrain/filter.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "util.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "resources.hpp"
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
|
||||
#include "config.hpp"
|
||||
#include "formatter.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "log.hpp" // for logger, set_strict_severity, etc
|
||||
#include "serialization/string_utils.hpp" // for split
|
||||
#include "util.hpp" // for lexical_cast
|
||||
|
||||
#include <boost/any.hpp> // for any
|
||||
#include <boost/program_options/cmdline.hpp>
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include "global.hpp"
|
||||
|
||||
#include "lexical_cast.hpp"
|
||||
#include "log.hpp"
|
||||
#include "util.hpp"
|
||||
#include "utils/const_clone.hpp"
|
||||
|
@ -42,7 +43,7 @@ static lg::log_domain log_config("config");
|
|||
#define DBG_CF LOG_STREAM(debug, log_config)
|
||||
|
||||
|
||||
#ifdef USE_HETEROGENOUS_LOOKUPS
|
||||
#ifdef USE_HETEROGENOUS_LOOKUPS
|
||||
struct config_simple_key
|
||||
{
|
||||
const char* str;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#define FONT_FONT_DESCRIPTION_HPP
|
||||
|
||||
#include "config.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "util.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@ std::string::const_iterator parse_markup(std::string::const_iterator i1,
|
|||
int* font_size,
|
||||
color_t* color, int* style)
|
||||
{
|
||||
try {
|
||||
while(i1 != i2) {
|
||||
switch(*i1) {
|
||||
case '\\':
|
||||
|
@ -132,6 +133,7 @@ std::string::const_iterator parse_markup(std::string::const_iterator i1,
|
|||
}
|
||||
++i1;
|
||||
}
|
||||
} catch(bad_lexical_cast) {}
|
||||
return i1;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "formatter.hpp"
|
||||
#include "formula/string_utils.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "log.hpp"
|
||||
#include "map/map.hpp"
|
||||
#include "map/exception.hpp"
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
#include "global.hpp"
|
||||
#include "gui/widgets/text_box.hpp"
|
||||
#include "util.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "serialization/unicode.hpp"
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include "gui/widgets/tree_view_node.hpp"
|
||||
#include "gui/widgets/toggle_button.hpp"
|
||||
#include "gui/widgets/window.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "gui/widgets/settings.hpp"
|
||||
#include "gui/widgets/text_box.hpp"
|
||||
#include "generators/map_generator.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@
|
|||
#include "gui/widgets/text_box.hpp"
|
||||
#include "gui/widgets/toggle_button.hpp"
|
||||
#include "gui/widgets/window.hpp"
|
||||
#include "util.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
#include <boost/math/common_factor_rt.hpp>
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "utils/const_clone.hpp"
|
||||
#include "gui/core/event/message.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
||||
|
|
|
@ -95,7 +95,13 @@ inline To lexical_cast(From value)
|
|||
}
|
||||
|
||||
/** Thrown when a lexical_cast fails. */
|
||||
struct bad_lexical_cast : std::exception {};
|
||||
struct bad_lexical_cast : std::exception
|
||||
{
|
||||
const char* what() const throw()
|
||||
{
|
||||
return "bad_lexical_cast";
|
||||
}
|
||||
};
|
||||
|
||||
namespace implementation {
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include "global.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include "lexical_cast.hpp"
|
||||
#include "log.hpp"
|
||||
#include "terrain/translation.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
|
|
|
@ -171,11 +171,13 @@ progressive_<T>::progressive_(const std::string &data, int duration) :
|
|||
ERR_NG << "Invalid time in unit animation: " << colon_split[1] << "\n";
|
||||
}
|
||||
|
||||
std::vector<std::string> range = utils::split(colon_split[0],'~',split_flag);
|
||||
T range0 = lexical_cast<T>(range[0]);
|
||||
T range1 = (range.size() > 1) ? lexical_cast<T>(range[1]) : range0;
|
||||
typedef std::pair<T,T> range_pair;
|
||||
data_.push_back(std::pair<range_pair,int>(range_pair(range0, range1), time));
|
||||
try {
|
||||
std::vector<std::string> range = utils::split(colon_split[0],'~',split_flag);
|
||||
T range0 = lexical_cast<T>(range[0]);
|
||||
T range1 = (range.size() > 1) ? lexical_cast<T>(range[1]) : range0;
|
||||
typedef std::pair<T,T> range_pair;
|
||||
data_.push_back(std::pair<range_pair,int>(range_pair(range0, range1), time));
|
||||
} catch(bad_lexical_cast) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
131
src/util.cpp
131
src/util.cpp
|
@ -20,33 +20,7 @@
|
|||
#include "util.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
template<>
|
||||
size_t lexical_cast<size_t, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
size_t res = strtoul(a.c_str(), &endptr, 10);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MSVC_DO_UNIT_TESTS
|
||||
template<>
|
||||
size_t lexical_cast<size_t, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
size_t res = strtoul(a, &endptr, 10);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
template<>
|
||||
size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def)
|
||||
{
|
||||
|
@ -63,6 +37,7 @@ size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, si
|
|||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def)
|
||||
{
|
||||
|
@ -79,31 +54,7 @@ size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def)
|
|||
return res;
|
||||
}
|
||||
}
|
||||
template<>
|
||||
long lexical_cast<long, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
long res = strtol(a.c_str(), &endptr, 10);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
long lexical_cast<long, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
long res = strtol(a, &endptr, 10);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
template<>
|
||||
long lexical_cast_default<long, const std::string&>(const std::string& a, long def)
|
||||
{
|
||||
|
@ -120,6 +71,7 @@ long lexical_cast_default<long, const std::string&>(const std::string& a, long d
|
|||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
long lexical_cast_default<long, const char*>(const char* a, long def)
|
||||
{
|
||||
|
@ -136,33 +88,6 @@ long lexical_cast_default<long, const char*>(const char* a, long def)
|
|||
return res;
|
||||
}
|
||||
}
|
||||
template<>
|
||||
int lexical_cast<int, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a.c_str(), &endptr, 10);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MSVC_DO_UNIT_TESTS
|
||||
template<>
|
||||
int lexical_cast<int, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
int res = strtol(a, &endptr, 10);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const std::string&>(const std::string& a, int def)
|
||||
|
@ -198,32 +123,6 @@ int lexical_cast_default<int, const char*>(const char* a, int def)
|
|||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
double lexical_cast<double, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
double res = strtod(a.c_str(), &endptr);
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
double lexical_cast<double, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
double res = strtod(a, &endptr);
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
double lexical_cast_default<double, const std::string&>(const std::string& a, double def)
|
||||
{
|
||||
|
@ -250,31 +149,6 @@ double lexical_cast_default<double, const char*>(const char* a, double def)
|
|||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const std::string&>(const std::string& a)
|
||||
{
|
||||
char* endptr;
|
||||
float res = static_cast<float>(strtod(a.c_str(), &endptr));
|
||||
|
||||
if (a.empty() || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const char*>(const char* a)
|
||||
{
|
||||
char* endptr;
|
||||
float res = static_cast<float>(strtod(a, &endptr));
|
||||
|
||||
if (*a == '\0' || *endptr != '\0') {
|
||||
throw bad_lexical_cast();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
template<>
|
||||
float lexical_cast_default<float, const std::string&>(const std::string& a, float def)
|
||||
{
|
||||
|
@ -300,4 +174,3 @@ float lexical_cast_default<float, const char*>(const char* a, float def)
|
|||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
52
src/util.hpp
52
src/util.hpp
|
@ -77,28 +77,6 @@ inline double round_portable(double d) {
|
|||
return (d >= 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
|
||||
}
|
||||
|
||||
struct bad_lexical_cast : public std::exception
|
||||
{
|
||||
const char *what() const throw()
|
||||
{
|
||||
return "bad_lexical_cast";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename To, typename From>
|
||||
To lexical_cast(From a)
|
||||
{
|
||||
To res = To();
|
||||
std::stringstream str;
|
||||
|
||||
if(str << a && str >> res) {
|
||||
return res;
|
||||
} else {
|
||||
throw bad_lexical_cast();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename To, typename From>
|
||||
To lexical_cast_default(From a, To def=To())
|
||||
{
|
||||
|
@ -112,60 +90,30 @@ To lexical_cast_default(From a, To def=To())
|
|||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
size_t lexical_cast<size_t, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
size_t lexical_cast<size_t, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def);
|
||||
|
||||
template<>
|
||||
size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def);
|
||||
|
||||
template<>
|
||||
long lexical_cast<long, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
long lexical_cast<long, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
long lexical_cast_default<long, const std::string&>(const std::string& a, long def);
|
||||
|
||||
template<>
|
||||
long lexical_cast_default<long, const char*>(const char* a, long def);
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
int lexical_cast<int, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const std::string&>(const std::string& a, int def);
|
||||
|
||||
template<>
|
||||
int lexical_cast_default<int, const char*>(const char* a, int def);
|
||||
|
||||
template<>
|
||||
double lexical_cast<double, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
double lexical_cast<double, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
double lexical_cast_default<double, const std::string&>(const std::string& a, double def);
|
||||
|
||||
template<>
|
||||
double lexical_cast_default<double, const char*>(const char* a, double def);
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const std::string&>(const std::string& a);
|
||||
|
||||
template<>
|
||||
float lexical_cast<float, const char*>(const char* a);
|
||||
|
||||
template<>
|
||||
float lexical_cast_default<float, const std::string&>(const std::string& a, float def);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue