Removed old implementation of lexical_cast_default

This commit is contained in:
Charles Dang 2016-12-08 14:15:12 +11:00
parent 10abf90ff3
commit 8c7df93d4f
20 changed files with 20 additions and 294 deletions

View file

@ -1103,7 +1103,6 @@
<Unit filename="../../src/units/udisplay.hpp" />
<Unit filename="../../src/units/unit.cpp" />
<Unit filename="../../src/units/unit.hpp" />
<Unit filename="../../src/util.cpp" />
<Unit filename="../../src/util.hpp" />
<Unit filename="../../src/utils/const_clone.hpp" />
<Unit filename="../../src/utils/context_free_grammar_generator.cpp" />

View file

@ -129,7 +129,6 @@
<Unit filename="../../src/server/user_handler.hpp" />
<Unit filename="../../src/tstring.cpp" />
<Unit filename="../../src/tstring.hpp" />
<Unit filename="../../src/util.cpp" />
<Unit filename="../../src/util.hpp" />
<Unit filename="../../src/version.cpp" />
<Unit filename="../../src/version.hpp" />

View file

@ -222,7 +222,6 @@ set(libwesnoth-core_STAT_SRC
mt_rng.cpp
seed_rng.cpp
tstring.cpp
util.cpp
version.cpp
serialization/binary_or_text.cpp
serialization/parser.cpp

View file

@ -40,7 +40,6 @@ libwesnoth_core_sources = Split("""
terrain/type_data.cpp
tools/schema/tag.cpp
tstring.cpp
util.cpp
version.cpp
""")

View file

@ -15,8 +15,8 @@
#include "editor/editor_preferences.hpp"
#include "config.hpp"
#include "game_preferences.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
namespace preferences {

View file

@ -20,8 +20,8 @@
#include "formula/formula.hpp"
#include "gui/core/log.hpp"
#include "gui/widgets/helper.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
#include "tstring.hpp"
#include <cassert>

View file

@ -15,12 +15,12 @@
#include "global.hpp"
#include "filesystem.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "persist_context.hpp"
#include "persist_manager.hpp"
#include "serialization/binary_or_text.hpp"
#include "serialization/parser.hpp"
#include "util.hpp"
config pack_scalar(const std::string &name, const t_string &val)
{

View file

@ -25,6 +25,7 @@
#include "filesystem.hpp"
#include "game_config.hpp"
#include "hotkey/hotkey_item.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "sound.hpp"

View file

@ -13,13 +13,13 @@
*/
#include "config.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "filesystem.hpp"
#include "serialization/parser.hpp"
#include "serialization/binary_or_text.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
#include "util.hpp"
#include "server/ban.hpp"

View file

@ -16,12 +16,12 @@
#include "filesystem.hpp"
#include "game_config.hpp" // game_config::observer_team_name
#include "lexical_cast.hpp"
#include "log.hpp"
#include "server/game.hpp"
#include "server/player_network.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
#include <sstream>
#include <iomanip>

View file

@ -15,8 +15,8 @@
#include "global.hpp"
#include "server/player.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
bool registered, const size_t max_messages,
@ -68,7 +68,7 @@ void wesnothd::player::mark_available(const int game_id,
} else {
cfg_.set_attr("available", "no");
}
cfg_.set_attr_dup("game_id", lexical_cast_default<std::string>(game_id).c_str());
cfg_.set_attr_dup("game_id", lexical_cast<std::string>(game_id).c_str());
cfg_.set_attr_dup("location", location.c_str());
}

View file

@ -23,6 +23,7 @@
#include "config.hpp"
#include "game_config.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "filesystem.hpp"
#include "multiplayer_error_codes.hpp"
@ -30,7 +31,6 @@
#include "serialization/preprocessor.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/unicode.hpp"
#include "util.hpp"
#include "utils/iterable_pair.hpp"
#include "server/game.hpp"
@ -1286,7 +1286,7 @@ void server::handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml
}
g.set_description(&desc);
desc.set_attr_dup("id", lexical_cast_default<std::string>(g.id()).c_str());
desc.set_attr_dup("id", lexical_cast<std::string>(g.id()).c_str());
} else {
WRN_SERVER << client_address(socket) << "\t" << player.name()
<< "\tsent scenario data in game:\t\"" << g.name() << "\" ("

View file

@ -19,6 +19,7 @@
#include "global.hpp"
#include "lexical_cast.hpp"
#include "settings.hpp"
#include "serialization/string_utils.hpp"

View file

@ -16,67 +16,12 @@
#include <boost/test/unit_test.hpp>
#include "lexical_cast.hpp"
#include "util.hpp"
#include <cstdint>
BOOST_AUTO_TEST_SUITE( util )
BOOST_AUTO_TEST_CASE( test_lexical_cast )
{
/* First check if lexical_cast returns correct results for correct args */
int result = lexical_cast<int, const std::string&>(std::string("1"));
BOOST_CHECK( result == 1 );
int result2 = lexical_cast<int, const char*>("2");
BOOST_CHECK( result2 == 2 );
/* Check that an exception is thrown when an invalid argument is passed */
try {
lexical_cast<int, const std::string&>(std::string("iddqd"));
/* A bad_lexical_cast should have been thrown already */
BOOST_CHECK( false );
}
catch( const bad_lexical_cast &) {
// Don't do anything, we succeeded.
}
try {
lexical_cast<int, const char*>("idkfa");
/* A bad_lexical_cast should have been thrown already */
BOOST_CHECK( false );
}
catch( const bad_lexical_cast &) {
// Don't do anything, we succeeded.
}
}
BOOST_AUTO_TEST_CASE( test_lexical_cast_default )
{
/* First check if it works with correct values */
int result = lexical_cast_default<int, const std::string&>(std::string("1"));
BOOST_CHECK( result == 1 );
int result2 = lexical_cast_default<int, const char*>("2");
BOOST_CHECK( result2 == 2 );
double result3 = lexical_cast_default<double, const std::string&>(std::string("0.5"));
BOOST_CHECK( result3 >= 0.499 && result3 <= 0.511 );
/* Check if default is returned when argument is empty/invalid */
int result4 = lexical_cast_default<int, const std::string&>(std::string(), 4);
BOOST_CHECK( result4 == 4 );
int result5 = lexical_cast_default<int, const char*>("", 5);
BOOST_CHECK( result5 == 5 );
double result6 = lexical_cast_default<double, const std::string&>(std::string(), 0.5);
BOOST_CHECK( result6 >= 0.499 && result6 <= 0.511 );
}
BOOST_AUTO_TEST_CASE( test_bit_width )
{
BOOST_CHECK( bit_width<uint8_t>() == 8 );

View file

@ -23,6 +23,7 @@
#include "formula/callable_objects.hpp"
#include "formula/formula.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "gettext.hpp"
@ -114,13 +115,13 @@ static bool matches_simple_filter(const attack_type & attack, const config & fil
if (!filter_attacks.empty() && !in_ranges(attack.num_attacks(), utils::parse_ranges(filter_attacks)))
return false;
if (!filter_accuracy.empty() && !in_ranges(attack.accuracy(), utils::parse_ranges(filter_accuracy)))
return false;
if (!filter_parry.empty() && !in_ranges(attack.parry(), utils::parse_ranges(filter_parry)))
return false;
if (!filter_movement.empty() && !in_ranges(attack.movement_used(), utils::parse_ranges(filter_movement)))
return false;
@ -132,7 +133,7 @@ static bool matches_simple_filter(const attack_type & attack, const config & fil
if ( !filter_special.empty() && !attack.get_special_bool(filter_special, true) )
return false;
if (!filter_formula.empty()) {
try {
const attack_type_callable callable(attack);
@ -275,7 +276,7 @@ bool attack_type::apply_modification(const config& cfg)
if (num_attacks_ < 0) {
num_attacks_ = 0;
}
}
if(increase_attacks.empty() == false) {

View file

@ -30,6 +30,7 @@
#include "game_events/manager.hpp" // for add_events
#include "game_preferences.hpp" // for encountered_units
#include "gettext.hpp" // for N_
#include "lexical_cast.hpp"
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "utils/make_enum.hpp" // for operator<<, operator>>
#include "map/map.hpp" // for gamemap

View file

@ -1,176 +0,0 @@
/*
Copyright (C) 2005 - 2016 by Philippe Plantier <ayin@anathas.org>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* String-routines - Templates for lexical_cast & lexical_cast_default.
*/
#include "util.hpp"
#include <cstdlib>
template<>
size_t lexical_cast_default<size_t, const std::string&>(const std::string& a, size_t def)
{
if(a.empty()) {
return def;
}
char* endptr;
size_t res = strtoul(a.c_str(), &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
size_t lexical_cast_default<size_t, const char*>(const char* a, size_t def)
{
if(*a == '\0') {
return def;
}
char* endptr;
size_t res = strtoul(a, &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
long lexical_cast_default<long, const std::string&>(const std::string& a, long def)
{
if(a.empty()) {
return def;
}
char* endptr;
long res = strtol(a.c_str(), &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
long lexical_cast_default<long, const char*>(const char* a, long def)
{
if(*a == '\0') {
return def;
}
char* endptr;
long res = strtol(a, &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
int lexical_cast_default<int, const std::string&>(const std::string& a, int def)
{
if(a.empty()) {
return def;
}
char* endptr;
int res = strtol(a.c_str(), &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
int lexical_cast_default<int, const char*>(const char* a, int def)
{
if(*a == '\0') {
return def;
}
char* endptr;
int res = strtol(a, &endptr, 10);
if (*endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
double lexical_cast_default<double, const std::string&>(const std::string& a, double def)
{
char* endptr;
double res = strtod(a.c_str(), &endptr);
if (a.empty() || *endptr != '\0') {
return def;;
} else {
return res;
}
}
template<>
double lexical_cast_default<double, const char*>(const char* a, double def)
{
char* endptr;
double res = strtod(a, &endptr);
if (*a == '\0' || *endptr != '\0') {
return def;
} else {
return res;
}
}
template<>
float lexical_cast_default<float, const std::string&>(const std::string& a, float def)
{
char* endptr;
float res = static_cast<float>(strtod(a.c_str(), &endptr));
if (a.empty() || *endptr != '\0') {
return def;;
} else {
return res;
}
}
template<>
float lexical_cast_default<float, const char*>(const char* a, float def)
{
char* endptr;
float res = static_cast<float>(strtod(a, &endptr));
if (*a == '\0' || *endptr != '\0') {
return def;
} else {
return res;
}
}

View file

@ -77,49 +77,6 @@ inline double round_portable(double d) {
return (d >= 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5);
}
template<typename To, typename From>
To lexical_cast_default(From a, To def=To())
{
To res = To();
std::stringstream str;
if(str << a && str >> res) {
return res;
} else {
return def;
}
}
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_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_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_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_default<float, const std::string&>(const std::string& a, float def);
template<>
float lexical_cast_default<float, const char*>(const char* a, float def);
template<typename Cmp>
bool in_ranges(const Cmp c, const std::vector<std::pair<Cmp, Cmp> >&ranges) {
typename std::vector<std::pair<Cmp,Cmp> >::const_iterator range,

View file

@ -68,7 +68,7 @@
* bad_lexical_cast does not provide any details about the error.
*
* It is recommended to use MAKE_ENUM types with the built-in versions of
* lexical_cast or lexical_cast_default provided by Wesnoth (see util.hpp).
* lexical_cast or lexical_cast_default provided by Wesnoth (see lexical_cast.hpp).
* However, if you do <b>not</b> want wml_exception to be thrown under any
* circumstances, use the string_to_enumname functions instead.
*

View file

@ -13,8 +13,8 @@
*/
#include "version.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
#include <cassert>
#include <functional>