C++: Provide fallbacks for missing stoi/stod functions
Notably, Android does not provide these functions. The fallback implementation is based on the one in the GNU ISO C++ Library.
This commit is contained in:
parent
d1a5f73781
commit
d92de05ad1
|
@ -30,6 +30,46 @@ const ConfigKey *ConfigKey::get(string identifier)
|
|||
return get(info->key);
|
||||
}
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifndef HAVE_STOI_STOD
|
||||
|
||||
/* Fallback implementation of stoi and stod */
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cerrno>
|
||||
#include <stdexcept>
|
||||
#include <limits>
|
||||
|
||||
static inline int stoi( const std::string& str )
|
||||
{
|
||||
char *endptr;
|
||||
errno = 0;
|
||||
const long ret = std::strtol(str.c_str(), &endptr, 10);
|
||||
if (endptr == str.c_str())
|
||||
throw std::invalid_argument("stoi");
|
||||
else if (errno == ERANGE ||
|
||||
ret < std::numeric_limits<int>::min() ||
|
||||
ret > std::numeric_limits<int>::max())
|
||||
throw std::out_of_range("stoi");
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline double stod( const std::string& str )
|
||||
{
|
||||
char *endptr;
|
||||
errno = 0;
|
||||
const double ret = std::strtod(str.c_str(), &endptr);
|
||||
if (endptr == str.c_str())
|
||||
throw std::invalid_argument("stod");
|
||||
else if (errno == ERANGE)
|
||||
throw std::out_of_range("stod");
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
Glib::VariantBase ConfigKey::parse_string(string value) const
|
||||
{
|
||||
GVariant *variant;
|
||||
|
|
13
configure.ac
13
configure.ac
|
@ -386,6 +386,19 @@ PKG_CHECK_MODULES([glibmm], [glibmm-2.4 >= 2.32.0],
|
|||
CXXLIBS="$CXXLIBS $glibmm_LIBS"],
|
||||
[BINDINGS_CXX="no"; cxx_msg="glibmm required"])
|
||||
|
||||
# C++ bindings want stoi and stod
|
||||
if test "x$BINDINGS_CXX" == "xyes"; then
|
||||
AC_LANG_PUSH([C++])
|
||||
AC_MSG_CHECKING([for stoi and stod])
|
||||
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <string>],
|
||||
[{ return std::stoi("1")+std::stod("1.0"); }])],
|
||||
[AC_MSG_RESULT([yes]);
|
||||
AC_DEFINE_UNQUOTED(HAVE_STOI_STOD, [1],
|
||||
[Specifies whether we have the stoi and stod functions.])],
|
||||
[AC_MSG_RESULT([no])])
|
||||
AC_LANG_POP([C++])
|
||||
fi
|
||||
|
||||
# PyGObject is needed for the Python bindings.
|
||||
PKG_CHECK_MODULES([pygobject], [pygobject-3.0 >= 3.0.0],
|
||||
[CXXFLAGS="$CXXFLAGS $pygobject_CFLAGS";
|
||||
|
|
Loading…
Reference in New Issue