base: unify ascii_to conversion functions

This patch addresses the first point of the issue #1477, namely
replacing the function-template magic by plain function overloads.

Issue #1477
This commit is contained in:
Norman Feske 2015-04-10 12:50:48 +02:00
parent 84a98e18ad
commit d884cf1a9a
2 changed files with 8 additions and 34 deletions

View File

@ -264,28 +264,10 @@ namespace Genode {
}
/**
* Convert ASCII string to another type
*
* \param T destination type of conversion
* \param s null-terminated source string
* \param result destination pointer to conversion result
* \param base base, autodetected if set to 0
* \return number of consumed characters
*
* Please note that 'base' and 's_max_len' are not evaluated by all
* template specializations.
*/
template <typename T>
inline size_t ascii_to(const char *s, T *result, unsigned base = 0);
/**
* Read unsigned long value from string
*/
template <>
inline size_t ascii_to<unsigned long>(const char *s, unsigned long *result,
unsigned base)
inline size_t ascii_to(const char *s, unsigned long *result, unsigned base = 0)
{
unsigned long i = 0, value = 0;
@ -320,12 +302,10 @@ namespace Genode {
/**
* Read unsigned int value from string
*/
template <>
inline size_t ascii_to<unsigned int>(const char *s, unsigned int *result,
unsigned base)
inline size_t ascii_to(const char *s, unsigned int *result, unsigned base = 10)
{
unsigned long result_long = 0;
size_t ret = ascii_to<unsigned long>(s, &result_long, base);
size_t ret = ascii_to(s, &result_long, base);
*result = result_long;
return ret;
}
@ -334,8 +314,7 @@ namespace Genode {
/**
* Read signed long value from string
*/
template <>
inline size_t ascii_to<long>(const char *s, long *result, unsigned base)
inline size_t ascii_to(const char *s, long *result, unsigned base = 10)
{
int i = 0;
@ -362,8 +341,7 @@ namespace Genode {
* This function scales the resulting size value according to the suffixes
* for G (2^30), M (2^20), and K (2^10) if present.
*/
template <>
inline size_t ascii_to(const char *s, Number_of_bytes *result, unsigned)
inline size_t ascii_to(const char *s, Number_of_bytes *result, unsigned base = 0)
{
unsigned long res = 0;
@ -387,8 +365,7 @@ namespace Genode {
/**
* Read double float value from string
*/
template <>
inline size_t ascii_to<double>(const char *s, double *result, unsigned)
inline size_t ascii_to(const char *s, double *result, unsigned base = 0)
{
double v = 0.0; /* decimal part */
double d = 0.1; /* power of fractional digit */

View File

@ -19,8 +19,7 @@
namespace Genode {
struct Color;
template <>
inline size_t ascii_to<Color>(const char *, Color *, unsigned);
inline size_t ascii_to(const char *, Color *, unsigned base = 0);
}
@ -46,9 +45,7 @@ struct Genode::Color
* \return number of consumed characters, or 0 if the string contains
* no valid color
*/
template <>
inline Genode::size_t
Genode::ascii_to<Genode::Color>(const char *s, Genode::Color *result, unsigned)
inline Genode::size_t Genode::ascii_to(const char *s, Genode::Color *result, unsigned)
{
/* validate string */
if (strlen(s) < 7 || *s != '#') return 0;