Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parse_number function, optimize parse_parameter function performance #472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 171 additions & 2 deletions source/corvusoft/restbed/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,104 @@ namespace restbed
//Constructors

//Functionality
static int inline parse_number( const std::string& value, const int default_value )
{
try
{
return std::stoi( value );
}
catch ( ... )
{
return default_value;
}
}

static long inline parse_number( const std::string& value, const long default_value )
{
try
{
return std::stol( value );
}
catch ( ... )
{
return default_value;
}
}

static long long inline parse_number( const std::string& value, const long long default_value )
{
try
{
return std::stoll( value );
}
catch ( ... )
{
return default_value;
}
}

static unsigned long inline parse_number( const std::string& value, const unsigned long default_value )
{
try
{
return std::stoul( value );
}
catch ( ... )
{
return default_value;
}
}

static unsigned long long inline parse_number( const std::string& value, const unsigned long long default_value )
{
try
{
return std::stoull( value );
}
catch ( ... )
{
return default_value;
}
}

static float inline parse_number( const std::string& value, const float default_value )
{
try
{
return std::stof( value );
}
catch ( ... )
{
return default_value;
}
}

static double inline parse_number( const std::string& value, const double default_value )
{
try
{
return std::stod( value );
}
catch ( ... )
{
return default_value;
}
}

static long double inline parse_number( const std::string& value, const long double default_value )
{
try
{
return std::stold( value );
}
catch ( ... )
{
return default_value;
}
}

template< typename Type >
static Type parse_parameter( const std::string& value, const Type default_value )
static inline Type parse_parameter( const std::string& value, const Type default_value )
{
std::istringstream stream( value );

Expand All @@ -51,7 +147,7 @@ namespace restbed

return parameter;
}

template< typename Type >
static bool has_parameter( const std::string& name, const Type& parameters )
{
Expand Down Expand Up @@ -138,4 +234,77 @@ namespace restbed

//Properties
};


template<>
inline std::string Common::parse_parameter( const std::string& value, const std::string )
{
return value;
}

template<>
inline bool Common::parse_parameter( const std::string& value, const bool default_value )
{
// true / false
// yes / no
// 1 / 0
if ( value == "false" || value == "FALSE" || value == "0" || value == "no" || value == "NO" )
{
return false;
}
if ( value == "true" || value == "TRUE" || value == "1" || value == "yes" || value == "YES" )
{
return true;
}
return default_value;
}

template<>
inline int Common::parse_parameter( const std::string& value, const int default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline long Common::parse_parameter( const std::string& value, const long default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline long long Common::parse_parameter( const std::string& value, const long long default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline unsigned long Common::parse_parameter( const std::string& value, const unsigned long default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline unsigned long long Common::parse_parameter( const std::string& value, const unsigned long long default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline float Common::parse_parameter( const std::string& value, const float default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline double Common::parse_parameter( const std::string& value, const double default_value )
{
return Common::parse_number( value, default_value );
}

template<>
inline long double Common::parse_parameter( const std::string& value, const long double default_value )
{
return Common::parse_number( value, default_value );
}

}