parse: add non-const view overloads

This commit is contained in:
Danny Robson 2019-03-16 18:17:18 +11:00
parent 5d123a5fd5
commit 3e9760d84f
2 changed files with 30 additions and 9 deletions

View File

@ -38,9 +38,18 @@ namespace {
///////////////////////////////////////////////////////////////////////////
template <typename ValueT>
template <
typename ValueT,
typename IteratorT,
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<IteratorT>,
char
>
>
>
ValueT
c_iparse (cruft::view<char const*> &data)
c_iparse (cruft::view<IteratorT*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
@ -55,9 +64,18 @@ namespace {
//-------------------------------------------------------------------------
template <typename ValueT>
template <
typename ValueT,
typename IteratorT,
typename = std::enable_if_t<
std::is_same_v<
std::remove_cv_t<IteratorT>,
char
>
>
>
ValueT
c_fparse (cruft::view<char const*> &data)
c_fparse (cruft::view<IteratorT*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
@ -75,15 +93,19 @@ namespace {
///////////////////////////////////////////////////////////////////////////////
/// Specialise cruft::parse for the type KLASS and dispatch the actual parsing
/// to safe template wrappers around the C parsing functions.
#define C_PARSE(PREFIX, KLASS) \
#define C_PARSE_ITERATOR(ITERATOR,PREFIX, KLASS)\
template <> \
KLASS \
cruft::parse<KLASS> ( \
cruft::view<const char *> &str \
cruft::view<ITERATOR> &str \
) { \
return c_ ## PREFIX ## parse<KLASS> (str); \
}
#define C_PARSE(PREFIX,KLASS) \
C_PARSE_ITERATOR(char *, PREFIX, KLASS) \
C_PARSE_ITERATOR(char const *, PREFIX, KLASS)
//-----------------------------------------------------------------------------
MAP1(C_PARSE, i,

View File

@ -17,9 +17,8 @@ namespace cruft {
/// Throws std::invalid_argument when the type cannot be parsed.
///
/// The view is modified in place to reflect the unused data.
template <typename T>
T
parse (cruft::view<const char *> &);
template <typename T> T parse (cruft::view<const char *> &);
template <typename T> T parse (cruft::view< char *> &);
/// Parses a prefix string to obtain an instance of T.