The Battle for Wesnoth  1.15.2+dev
Classes | Namespaces | Macros | Functions
lexical_cast.hpp File Reference

New lexcical_cast header. More...

#include "global.hpp"
#include <cstdlib>
#include <limits>
#include <string>
#include <sstream>
#include <type_traits>
#include <boost/mpl/set.hpp>
#include <boost/optional.hpp>
Include dependency graph for lexical_cast.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  implementation::lexical_caster< To, From, ToEnable, FromEnable >
 Base class for the conversion. More...
 
struct  bad_lexical_cast
 Thrown when a lexical_cast fails. More...
 
struct  implementation::lexical_caster< To, From, ToEnable, FromEnable >
 Base class for the conversion. More...
 
struct  implementation::value >>< From >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< long long, std::string >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, From, std::enable_if_t< std::is_integral< To >::value &&std::is_signed< To >::value &&!std::is_same< To, long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_integral< To >::value &&std::is_signed< To >::value &&!std::is_same< To, long long >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, From, std::enable_if_t< std::is_floating_point< To >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_floating_point< To >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< unsigned long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< unsigned long long, std::string >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, From, std::enable_if_t< std::is_unsigned< To >::value &&!std::is_same< To, unsigned long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >
 Specialized conversion class. More...
 
struct  implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_unsigned< To >::value > >
 Specialized conversion class. More...
 

Namespaces

 implementation
 Contains the implementation details for lexical_cast and shouldn't be used directly.
 

Macros

#define LEXICAL_CAST_HPP_INCLUDED
 
#define DEBUG_THROW(id)
 Throws an exception for debugging. More...
 
#define LEXICAL_CAST_HPP_INCLUDED
 
#define DEBUG_THROW(id)   throw id;
 

Functions

template<typename To , typename From >
To lexical_cast (From value)
 Lexical cast converts one type to another. More...
 
template<typename To , typename From >
To lexical_cast_default (From value, To fallback=To())
 Lexical cast converts one type to another with a fallback. More...
 

Detailed Description

New lexcical_cast header.

For debugging you can include this header in a namespace (to honor ODR) and have a set of functions that throws exceptions instead of doing the real job. This is done for the unit tests but should normally not be done.

Definition in file lexical_cast.hpp.

Macro Definition Documentation

◆ DEBUG_THROW [1/2]

#define DEBUG_THROW (   id)    throw id;

◆ DEBUG_THROW [2/2]

#define DEBUG_THROW (   id)

Throws an exception for debugging.

Parameters
idThe unique name to identify the function.
Note
this name is a user defined string and should not be modified once used!

Definition at line 42 of file test_lexical_cast.cpp.

Referenced by implementation::lexical_caster< To, From, ToEnable, FromEnable >::operator()(), implementation::value >>< From >::operator()(), implementation::lexical_caster< long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< long long, std::string >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_integral< To >::value &&std::is_signed< To >::value &&!std::is_same< To, long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_integral< To >::value &&std::is_signed< To >::value &&!std::is_same< To, long long >::value > >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_floating_point< To >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_floating_point< To >::value > >::operator()(), implementation::lexical_caster< unsigned long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< unsigned long long, std::string >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_unsigned< To >::value &&!std::is_same< To, unsigned long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), and implementation::lexical_caster< To, std::string, std::enable_if_t< std::is_unsigned< To >::value > >::operator()().

◆ LEXICAL_CAST_HPP_INCLUDED [1/2]

#define LEXICAL_CAST_HPP_INCLUDED

◆ LEXICAL_CAST_HPP_INCLUDED [2/2]

#define LEXICAL_CAST_HPP_INCLUDED

Definition at line 30 of file test_lexical_cast.cpp.

Function Documentation

◆ lexical_cast()

template<typename To , typename From >
To lexical_cast ( From  value)
inline

Lexical cast converts one type to another.

Template Parameters
ToThe type to convert to.
FromThe type to convert from.
Parameters
valueThe value to convert.
Returns
The converted value.
Exceptions
bad_lexical_castif the cast was unsuccessful.

Definition at line 92 of file lexical_cast.hpp.

Referenced by config::apply_diff(), BOOST_AUTO_TEST_CASE(), config::clear_diff_track(), events::console_handler::do_control(), events::console_handler::do_controller(), editor::editor_display::draw_sidebar(), gui2::dialogs::editor_generate_map::get_seed(), help::help_text_area::handle_jump_cfg(), server_base::handle_termination(), gui2::dialogs::lua_interpreter::input_model::list_history(), gui2::dialogs::help_browser::on_topic_select(), implementation::lexical_caster< long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_integral< To >::value &&std::is_signed< To >::value &&!std::is_same< To, long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_floating_point< To >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), config_attribute_value::string_visitor::operator()(), implementation::lexical_caster< unsigned long long, From, void, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), implementation::lexical_caster< To, From, std::enable_if_t< std::is_unsigned< To >::value &&!std::is_same< To, unsigned long long >::value >, std::enable_if_t< boost::mpl::has_key< boost::mpl::set< char *, const char * >, From >::value > >::operator()(), font::parse_markup(), commandline_options::parse_to_uint_string_string_tuples_(), commandline_options::parse_to_uint_string_tuples_(), gui2::dialogs::preferences_dialog::post_build(), progressive_pair< int >::progressive_pair(), gui2::sort(), and ai::config_value_translator< T >::value_to_cfg().

◆ lexical_cast_default()

template<typename To , typename From >
To lexical_cast_default ( From  value,
To  fallback = To() 
)
inline

Lexical cast converts one type to another with a fallback.

Template Parameters
ToThe type to convert to.
FromThe type to convert from.
Parameters
valueThe value to convert.
fallbackThe fallback value to return if the cast fails.
Returns
The converted value.

Definition at line 109 of file lexical_cast.hpp.

Referenced by t_translation::string_to_builder_number_().