Skip to content

Commit

Permalink
Add parse_number function, optimize parse_parameter function performance
Browse files Browse the repository at this point in the history
Signed-off-by: solym <[email protected]>
  • Loading branch information
sotex committed Sep 23, 2020
1 parent c99646c commit ac47dc7
Showing 1 changed file with 171 additions and 2 deletions.
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 );
}

}

0 comments on commit ac47dc7

Please sign in to comment.