2018-02-03 03:51:57 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
2018-02-06 22:03:38 +01:00
|
|
|
#define HAVE_IRCD_UTIL_PARAMS_H
|
2018-02-03 03:51:57 +01:00
|
|
|
|
2018-02-06 22:03:38 +01:00
|
|
|
// This file is not part of the standard include stack. It is included
|
|
|
|
// manually as needed.
|
|
|
|
|
|
|
|
namespace ircd::util
|
|
|
|
{
|
|
|
|
struct params;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ircd::util::params
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view in;
|
2018-02-03 03:51:57 +01:00
|
|
|
const char *sep;
|
|
|
|
std::vector<const char *> names;
|
|
|
|
|
|
|
|
const char *name(const size_t &i) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
IRCD_EXCEPTION(error, missing)
|
|
|
|
IRCD_EXCEPTION(error, invalid)
|
|
|
|
|
|
|
|
size_t count() const;
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view operator[](const size_t &i) const; // returns empty
|
2018-02-03 03:51:57 +01:00
|
|
|
template<class T> T at(const size_t &i, const T &def) const; // throws invalid
|
|
|
|
template<class T> T at(const size_t &i) const; // throws missing or invalid
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view at(const size_t &i) const; // throws missing
|
2018-02-03 03:51:57 +01:00
|
|
|
|
2018-02-06 22:03:38 +01:00
|
|
|
params(const string_view &in,
|
2018-02-03 03:51:57 +01:00
|
|
|
const char *const &sep,
|
|
|
|
const std::initializer_list<const char *> &names = {});
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::params(const string_view &in,
|
|
|
|
const char *const &sep,
|
|
|
|
const std::initializer_list<const char *> &names)
|
2018-02-03 03:51:57 +01:00
|
|
|
:in{in}
|
|
|
|
,sep{sep}
|
|
|
|
,names{names}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i,
|
|
|
|
const T &def)
|
2018-02-03 03:51:57 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return count() > i? at<T>(i) : def;
|
|
|
|
}
|
2018-02-06 22:03:38 +01:00
|
|
|
catch(const bad_lex_cast &e)
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
|
|
|
throw invalid("parameter #%zu <%s>", i, name(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const try
|
|
|
|
{
|
2018-02-06 22:03:38 +01:00
|
|
|
return lex_cast<T>(at(i));
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
2018-02-06 22:03:38 +01:00
|
|
|
catch(const bad_lex_cast &e)
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
|
|
|
throw invalid("parameter #%zu <%s>", i, name(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return token(in, sep, i);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw missing("required parameter #%zu <%s>", i, name(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::operator[](const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-06 22:03:38 +01:00
|
|
|
return count() > i? token(in, sep, i) : string_view{};
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::count()
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return token_count(in, sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char *
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::name(const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return names.size() > i? *std::next(begin(names), i) : "<unnamed>";
|
|
|
|
}
|