2018-03-02 09:35:02 +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
|
|
|
|
#define HAVE_IRCD_CONF_H
|
|
|
|
|
|
|
|
namespace ircd::conf
|
|
|
|
{
|
|
|
|
template<class T = void> struct item; // doesn't exist
|
|
|
|
template<> struct item<void>; // base class of all conf items
|
2018-03-02 16:45:02 +01:00
|
|
|
template<> struct item<std::string>;
|
2018-03-03 08:54:35 +01:00
|
|
|
template<> struct item<uint64_t>;
|
|
|
|
template<> struct item<int64_t>;
|
2018-03-09 21:20:23 +01:00
|
|
|
template<> struct item<hours>;
|
2018-03-02 09:35:02 +01:00
|
|
|
template<> struct item<seconds>;
|
2018-03-09 21:20:23 +01:00
|
|
|
template<> struct item<milliseconds>;
|
|
|
|
template<> struct item<microseconds>;
|
|
|
|
|
|
|
|
template<class T> struct value; // abstraction for carrying item value
|
|
|
|
template<class T> struct lex_castable; // abstraction for lex_cast compatible
|
2018-03-02 09:35:02 +01:00
|
|
|
|
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
2018-03-02 12:03:33 +01:00
|
|
|
IRCD_EXCEPTION(error, not_found)
|
|
|
|
IRCD_EXCEPTION(error, bad_value)
|
2018-03-02 09:35:02 +01:00
|
|
|
|
2018-03-02 12:03:33 +01:00
|
|
|
extern const std::string &config; //TODO: X
|
|
|
|
extern std::map<string_view, item<> *> items;
|
|
|
|
|
|
|
|
string_view get(const string_view &key, const mutable_buffer &out);
|
|
|
|
bool set(const string_view &key, const string_view &value);
|
|
|
|
bool set(std::nothrow_t, const string_view &key, const string_view &value);
|
2018-03-02 09:35:02 +01:00
|
|
|
|
|
|
|
void init(const string_view &configfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Conf item base class. You don't create this directly; use one of the
|
|
|
|
/// derived templates instead.
|
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<void>
|
|
|
|
{
|
|
|
|
json::strung feature_;
|
|
|
|
json::object feature;
|
|
|
|
string_view name;
|
|
|
|
|
2018-03-02 12:03:33 +01:00
|
|
|
virtual string_view get(const mutable_buffer &) const;
|
|
|
|
virtual bool set(const string_view &);
|
2018-03-02 09:35:02 +01:00
|
|
|
|
|
|
|
item(const json::members &);
|
2018-03-02 12:03:33 +01:00
|
|
|
item(item &&) = delete;
|
|
|
|
item(const item &) = delete;
|
2018-03-02 09:35:02 +01:00
|
|
|
virtual ~item() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Conf item value abstraction. If possible, the conf item will also
|
|
|
|
/// inherit from this template to deduplicate functionality between
|
|
|
|
/// conf items which contain similar classes of values.
|
|
|
|
template<class T>
|
|
|
|
struct ircd::conf::value
|
|
|
|
{
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
T _value;
|
|
|
|
|
|
|
|
operator const T &() const
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2018-03-02 12:03:33 +01:00
|
|
|
template<class... A>
|
|
|
|
value(A&&... a)
|
2018-03-09 21:20:23 +01:00
|
|
|
:_value(std::forward<A>(a)...)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct ircd::conf::lex_castable
|
|
|
|
:conf::item<>
|
|
|
|
,conf::value<T>
|
|
|
|
{
|
|
|
|
string_view get(const mutable_buffer &out) const override
|
|
|
|
{
|
|
|
|
return lex_cast(this->_value, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set(const string_view &s) override
|
|
|
|
{
|
|
|
|
this->_value = lex_cast<T>(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
lex_castable(const json::members &members)
|
|
|
|
:conf::item<>{members}
|
|
|
|
,conf::value<T>(feature.get("default", long(0)))
|
2018-03-02 09:35:02 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-03-02 16:45:02 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<std::string>
|
|
|
|
:conf::item<>
|
|
|
|
,conf::value<std::string>
|
|
|
|
{
|
|
|
|
operator string_view() const
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string_view get(const mutable_buffer &out) const override
|
|
|
|
{
|
|
|
|
return { data(out), _value.copy(data(out), size(out)) };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set(const string_view &s) override
|
|
|
|
{
|
|
|
|
_value = std::string{s};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:20:23 +01:00
|
|
|
item(const json::members &members)
|
|
|
|
:conf::item<>{members}
|
2018-03-02 16:45:02 +01:00
|
|
|
,value{unquote(feature.get("default"))}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-03-03 08:54:35 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<uint64_t>
|
2018-03-09 21:20:23 +01:00
|
|
|
:lex_castable<uint64_t>
|
2018-03-03 08:54:35 +01:00
|
|
|
{
|
2018-03-09 21:20:23 +01:00
|
|
|
using lex_castable::lex_castable;
|
2018-03-03 08:54:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<int64_t>
|
2018-03-09 21:20:23 +01:00
|
|
|
:lex_castable<int64_t>
|
2018-03-03 08:54:35 +01:00
|
|
|
{
|
2018-03-09 21:20:23 +01:00
|
|
|
using lex_castable::lex_castable;
|
|
|
|
};
|
2018-03-03 08:54:35 +01:00
|
|
|
|
2018-03-09 21:20:23 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<ircd::hours>
|
|
|
|
:lex_castable<ircd::hours>
|
|
|
|
{
|
|
|
|
using lex_castable::lex_castable;
|
2018-03-03 08:54:35 +01:00
|
|
|
};
|
|
|
|
|
2018-03-02 09:35:02 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<ircd::seconds>
|
2018-03-09 21:20:23 +01:00
|
|
|
:lex_castable<ircd::seconds>
|
2018-03-02 09:35:02 +01:00
|
|
|
{
|
2018-03-09 21:20:23 +01:00
|
|
|
using lex_castable::lex_castable;
|
|
|
|
};
|
2018-03-02 12:03:33 +01:00
|
|
|
|
2018-03-09 21:20:23 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<ircd::milliseconds>
|
|
|
|
:lex_castable<ircd::milliseconds>
|
|
|
|
{
|
|
|
|
using lex_castable::lex_castable;
|
|
|
|
};
|
2018-03-02 12:03:33 +01:00
|
|
|
|
2018-03-09 21:20:23 +01:00
|
|
|
template<>
|
|
|
|
struct ircd::conf::item<ircd::microseconds>
|
|
|
|
:lex_castable<ircd::microseconds>
|
|
|
|
{
|
|
|
|
using lex_castable::lex_castable;
|
2018-03-02 09:35:02 +01:00
|
|
|
};
|