diff --git a/charybdis/console.cc b/charybdis/console.cc index 29c10e5dd..d1c5add5d 100644 --- a/charybdis/console.cc +++ b/charybdis/console.cc @@ -23,6 +23,7 @@ #include #include #include "charybdis.h" +#include "params.h" using namespace ircd; diff --git a/charybdis/params.h b/charybdis/params.h new file mode 100644 index 000000000..d23187670 --- /dev/null +++ b/charybdis/params.h @@ -0,0 +1,103 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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 +#define HAVE_CONSTRUCT_PARAMS_H + +class params +{ + ircd::string_view in; + const char *sep; + std::vector 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; + ircd::string_view operator[](const size_t &i) const; // returns empty + template T at(const size_t &i, const T &def) const; // throws invalid + template T at(const size_t &i) const; // throws missing or invalid + ircd::string_view at(const size_t &i) const; // throws missing + + params(const ircd::string_view &in, + const char *const &sep, + const std::initializer_list &names = {}); +}; + +inline +params::params(const ircd::string_view &in, + const char *const &sep, + const std::initializer_list &names) +:in{in} +,sep{sep} +,names{names} +{ +} + +template +T +params::at(const size_t &i, + const T &def) +const try +{ + return count() > i? at(i) : def; +} +catch(const ircd::bad_lex_cast &e) +{ + throw invalid("parameter #%zu <%s>", i, name(i)); +} + +template +T +params::at(const size_t &i) +const try +{ + return ircd::lex_cast(at(i)); +} +catch(const ircd::bad_lex_cast &e) +{ + throw invalid("parameter #%zu <%s>", i, name(i)); +} + +inline ircd::string_view +params::at(const size_t &i) +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 +params::operator[](const size_t &i) +const +{ + return count() > i? token(in, sep, i) : ircd::string_view{}; +} + +inline size_t +params::count() +const +{ + return token_count(in, sep); +} + +inline const char * +params::name(const size_t &i) +const +{ + return names.size() > i? *std::next(begin(names), i) : ""; +} diff --git a/include/ircd/params.h b/include/ircd/params.h deleted file mode 100644 index f876db0f4..000000000 --- a/include/ircd/params.h +++ /dev/null @@ -1,120 +0,0 @@ -/* - * charybdis: an advanced ircd. - * inline/stringops.h: inlined string operations used in a few places - * - * Copyright (C) 2005-2016 Charybdis Development Team - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA - */ - -#pragma once -#define HAVE_IRCD_PARAMS_H - -namespace ircd::util -{ - struct params; -} - -class ircd::util::params -{ - string_view in; - const char *sep; - std::vector 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; - string_view operator[](const size_t &i) const; // returns empty - template T at(const size_t &i, const T &def) const; // throws invalid - template T at(const size_t &i) const; // throws missing or invalid - string_view at(const size_t &i) const; // throws missing - - params(const string_view &in, - const char *const &sep, - const std::initializer_list &names = {}); -}; - -inline -ircd::util::params::params(const string_view &in, - const char *const &sep, - const std::initializer_list &names) -:in{in} -,sep{sep} -,names{names} -{ -} - -template -T -ircd::util::params::at(const size_t &i, - const T &def) -const try -{ - return count() > i? at(i) : def; -} -catch(const bad_lex_cast &e) -{ - throw invalid("parameter #%zu <%s>", i, name(i)); -} - -template -T -ircd::util::params::at(const size_t &i) -const try -{ - return lex_cast(at(i)); -} -catch(const bad_lex_cast &e) -{ - throw invalid("parameter #%zu <%s>", i, name(i)); -} - -inline ircd::string_view -ircd::util::params::at(const size_t &i) -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 -ircd::util::params::operator[](const size_t &i) -const -{ - return count() > i? token(in, sep, i) : string_view{}; -} - -inline size_t -ircd::util::params::count() -const -{ - return token_count(in, sep); -} - -inline const char * -ircd::util::params::name(const size_t &i) -const -{ - return names.size() > i? *std::next(begin(names), i) : ""; -} diff --git a/include/ircd/stdinc.h b/include/ircd/stdinc.h index 94ff8038e..2e372df8a 100644 --- a/include/ircd/stdinc.h +++ b/include/ircd/stdinc.h @@ -220,7 +220,6 @@ namespace ircd #include "base.h" #include "stringops.h" #include "tokens.h" -#include "params.h" #include "iov.h" #include "parse.h" #include "rfc1459.h"