Skip to content

Commit

Permalink
Made use of the "new" lexical_cast implementation
Browse files Browse the repository at this point in the history
Note the "old" implementation of lexical_cast_default is still used since the new code doesn't have
the equivalent functionality yet.
  • Loading branch information
Vultraz committed Dec 7, 2016
1 parent 1202eb1 commit 626d6c6
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 191 deletions.
1 change: 1 addition & 0 deletions src/ai/composite/value_translator.hpp
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/commandline_options.cpp
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion src/config.cpp
Expand Up @@ -22,6 +22,7 @@

#include "global.hpp"

#include "lexical_cast.hpp"
#include "log.hpp"
#include "util.hpp"
#include "utils/const_clone.hpp"
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/font/font_description.hpp
Expand Up @@ -16,6 +16,7 @@
#define FONT_FONT_DESCRIPTION_HPP

#include "config.hpp"
#include "lexical_cast.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"

Expand Down
2 changes: 2 additions & 0 deletions src/font/marked-up_text.cpp
Expand Up @@ -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 '\\':
Expand Down Expand Up @@ -132,6 +133,7 @@ std::string::const_iterator parse_markup(std::string::const_iterator i1,
}
++i1;
}
} catch(bad_lexical_cast) {}

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Dec 7, 2016

Member

Why the weird indentation here

This comment has been minimized.

Copy link
@Vultraz

Vultraz Dec 7, 2016

Author Member

Committed in error, removed later.

return i1;
}

Expand Down
1 change: 1 addition & 0 deletions src/game_initialization/lobby_data.cpp
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/gui/auxiliary/filter.hpp
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions src/gui/dialogs/campaign_selection.cpp
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/gui/dialogs/editor/generate_map.cpp
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/preferences_dialog.cpp
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/gui/widgets/pane.cpp
Expand Up @@ -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"

Expand Down
8 changes: 7 additions & 1 deletion src/lexical_cast.hpp
Expand Up @@ -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()

This comment has been minimized.

Copy link
@jyrkive

jyrkive Dec 7, 2016

Member

In C++11, noexcept is generally recommended over throw().

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Dec 7, 2016

Member

If I recall correctly, we have a macro for that.

This comment has been minimized.

Copy link
@Vultraz

Vultraz Dec 7, 2016

Author Member

Already implemented.

{
return "bad_lexical_cast";
}
};

namespace implementation {

Expand Down
1 change: 1 addition & 0 deletions src/terrain/translation.cpp
Expand Up @@ -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"
Expand Down
12 changes: 7 additions & 5 deletions src/units/frame.cpp
Expand Up @@ -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) {}
}
}

Expand Down
131 changes: 2 additions & 129 deletions src/util.cpp
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -300,4 +174,3 @@ float lexical_cast_default<float, const char*>(const char* a, float def)
return res;
}
}

0 comments on commit 626d6c6

Please sign in to comment.