diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-07-29 20:21:00 +0100 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-07-29 20:21:00 +0100 | 
| commit | 7dcad74d144879f4cce790067de1c164df9bd697 (patch) | |
| tree | 34b78aad60cd41495aa9a44b7fdd3145ff2a0611 | |
| parent | getURLParam needn't go through the optional helpers (diff) | |
| download | icespider-7dcad74d144879f4cce790067de1c164df9bd697.tar.bz2 icespider-7dcad74d144879f4cce790067de1c164df9bd697.tar.xz icespider-7dcad74d144879f4cce790067de1c164df9bd697.zip  | |
Simplified convert interface
Removes need to scope temps, boost lexical_cast catch/throw, two functions
| -rw-r--r-- | icespider/core/util.h | 70 | ||||
| -rw-r--r-- | icespider/core/xwwwFormUrlEncoded.cpp | 2 | 
2 files changed, 27 insertions, 45 deletions
diff --git a/icespider/core/util.h b/icespider/core/util.h index d31c7da..f4868c3 100644 --- a/icespider/core/util.h +++ b/icespider/core/util.h @@ -121,61 +121,43 @@ private:  namespace IceSpider {  	[[noreturn]] DLL_PUBLIC void conversion_failure(); -	static_assert(std::is_constructible_v<std::string, std::string_view>); -	static_assert(std::is_convertible_v<std::string_view, std::string_view>); - -	template<typename T> -	inline T -	from_chars(const std::string_view v, T && out) -	{ -		if (std::from_chars(v.begin(), v.end(), out).ec != std::errc {}) { -			conversion_failure(); -		} -		return out; -	} - -	template<typename T> -	inline T -	lexical_cast(const std::string_view v) -	{ -		try { -			return boost::lexical_cast<T>(v); -		} -		catch (const boost::bad_lexical_cast & e) { -			conversion_failure(); +	static_assert(std::is_assignable_v<std::string, std::string_view>); +	static_assert(std::is_assignable_v<std::string_view, std::string_view>); + +	namespace { +		template<typename T> +		inline T +		from_chars(const std::string_view v, T && out) +		{ +			if (std::from_chars(v.begin(), v.end(), out).ec != std::errc {}) { +				conversion_failure(); +			} +			return out;  		} -	} -	template<typename T> -	inline void -	convert(const std::string_view v, T & out) -	{ -		if constexpr (std::is_assignable_v<std::string_view, T>) { -			out = v; -		} -		if constexpr (requires { std::from_chars(v.begin(), v.end(), out); }) { -			from_chars(v, out); -		} -		else { -			out = lexical_cast<T>(v); +		template<typename T> +		inline T +		lexical_cast(const std::string_view v, T && out) +		{ +			if (!boost::conversion::try_lexical_convert(v, out)) { +				conversion_failure(); +			} +			return out;  		}  	}  	template<typename T>  	inline T -	convert(const std::string_view v) +	convert(const std::string_view v, T && out = {})  	{ -		if constexpr (std::is_convertible_v<std::string_view, T>) { -			return v; -		} -		else if constexpr (std::is_constructible_v<T, std::string_view>) { -			return T {v}; +		if constexpr (std::is_assignable_v<T, std::string_view>) { +			return (out = v);  		} -		else if constexpr (requires(T out) { std::from_chars(v.begin(), v.end(), out); }) { -			return from_chars<T>(v, {}); +		else if constexpr (requires { std::from_chars(v.begin(), v.end(), out); }) { +			return from_chars(v, out);  		}  		else { -			return lexical_cast<T>(v); +			return lexical_cast(v, out);  		}  	} diff --git a/icespider/core/xwwwFormUrlEncoded.cpp b/icespider/core/xwwwFormUrlEncoded.cpp index 639b07a..d5b8388 100644 --- a/icespider/core/xwwwFormUrlEncoded.cpp +++ b/icespider/core/xwwwFormUrlEncoded.cpp @@ -181,7 +181,7 @@ namespace IceSpider {  	/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \  	void set(T & t) const override \  	{ \ -		convert<T>(s, t); \ +		convert(s, t); \  	}  		SET(Ice::Byte);  | 
