ircd::util: Generalize util::boolean into returns template.

This commit is contained in:
Jason Volk 2023-02-09 18:07:45 -08:00
parent 8ce326da5b
commit 1c6d216daf
13 changed files with 67 additions and 63 deletions

View File

@ -22,7 +22,7 @@ namespace ircd
/// and '?' characters and equality of the string expressions will be /// and '?' characters and equality of the string expressions will be
/// determined. Case insensitive. /// determined. Case insensitive.
struct ircd::globular_iequals struct ircd::globular_iequals
:boolean :returns<bool>
{ {
using is_transparent = std::true_type; using is_transparent = std::true_type;
@ -31,7 +31,7 @@ struct ircd::globular_iequals
template<class A, template<class A,
class B> class B>
globular_iequals(A&& a, B&& b) globular_iequals(A&& a, B&& b)
:boolean{operator()(std::forward<A>(a), std::forward<B>(b))} :returns<bool>(operator()(std::forward<A>(a), std::forward<B>(b)))
{} {}
}; };

View File

@ -25,7 +25,7 @@ namespace ircd::m
/// find and add the prev_state/prev_content for state events, etc. /// find and add the prev_state/prev_content for state events, etc.
/// ///
struct ircd::m::event::append struct ircd::m::event::append
:boolean :returns<bool>
{ {
struct opts; struct opts;

View File

@ -45,7 +45,7 @@ namespace ircd::m::feds
/// the user can distinguish different requests in their options vector. /// the user can distinguish different requests in their options vector.
/// ///
struct ircd::m::feds::execute struct ircd::m::feds::execute
:boolean :returns<bool>
{ {
execute(const vector_view<const opts> &, const closure &); execute(const vector_view<const opts> &, const closure &);
execute(const opts &, const closure &); execute(const opts &, const closure &);

View File

@ -59,7 +59,7 @@ decltype(ircd::m::push::request::list)
ircd::instance_list<ircd::m::push::request>::list; ircd::instance_list<ircd::m::push::request>::list;
struct ircd::m::push::match struct ircd::m::push::match
:boolean :returns<bool>
{ {
struct opts; struct opts;
using cond_kind_func = bool (*)(const event &, const cond &, const opts &); using cond_kind_func = bool (*)(const event &, const cond &, const opts &);

View File

@ -17,7 +17,7 @@ namespace ircd::m
} }
struct ircd::m::redacted struct ircd::m::redacted
:boolean :returns<bool>
{ {
redacted(const event::idx &); redacted(const event::idx &);
redacted(const event::id &); redacted(const event::id &);
@ -44,7 +44,7 @@ ircd::m::redacted::redacted(const event::id &event_id)
inline inline
ircd::m::redacted::redacted(const event::idx &event_idx) ircd::m::redacted::redacted(const event::idx &event_idx)
:boolean :returns<bool>
{ {
event_idx? event_idx?
event::refs(event_idx).has(dbs::ref::M_ROOM_REDACTION): event::refs(event_idx).has(dbs::ref::M_ROOM_REDACTION):

View File

@ -110,14 +110,14 @@ struct ircd::m::room::power
}; };
struct ircd::m::room::power::grant struct ircd::m::room::power::grant
:boolean :returns<bool>
{ {
grant(json::stack::object &, const room::power &, const pair<string_view> &, const int64_t &); grant(json::stack::object &, const room::power &, const pair<string_view> &, const int64_t &);
grant(json::stack::object &, const room::power &, const m::id::user &, const int64_t &); grant(json::stack::object &, const room::power &, const m::id::user &, const int64_t &);
}; };
struct ircd::m::room::power::revoke struct ircd::m::room::power::revoke
:boolean :returns<bool>
{ {
revoke(json::stack::object &, const room::power &, const pair<string_view> &); revoke(json::stack::object &, const room::power &, const pair<string_view> &);
revoke(json::stack::object &, const room::power &, const m::id::user &); revoke(json::stack::object &, const room::power &, const m::id::user &);

View File

@ -1,45 +0,0 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2019 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_UTIL_BOOLEAN_H
namespace ircd
{
inline namespace util
{
struct boolean;
}
}
/// Simple convenience for cheesers to inherit from a POD boolean, similar to
/// IRCD_STRONG_TYPEDEF(bool, boolean) but with additional specific features.
struct ircd::util::boolean
{
bool val;
operator const bool &() const noexcept
{
return val;
}
explicit operator bool &() & noexcept
{
return val;
}
boolean(const bool &val) noexcept
:val{val}
{}
boolean(const std::function<bool ()> &func)
:val{func()}
{}
};

View File

@ -0,0 +1,49 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2023 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_UTIL_RETURNS_H
namespace ircd
{
inline namespace util
{
template<class T>
struct returns;
}
}
/// Simple convenience for cheesers to inherit from a POD type, similar to
/// IRCD_STRONG_TYPEDEF but with additional specific features.
template<class T>
struct ircd::util::returns
{
T ret;
operator const T &() const noexcept
{
return ret;
}
explicit operator T &() & noexcept
{
return ret;
}
returns(const std::function<T ()> &func)
:ret{func()}
{}
returns(const T &ret) noexcept
:ret{ret}
{}
returns() = default;
};

View File

@ -62,7 +62,7 @@ namespace ircd
#include "closure.h" #include "closure.h"
#include "env.h" #include "env.h"
#include "test.h" #include "test.h"
#include "boolean.h" #include "returns.h"
#include "maybe.h" #include "maybe.h"
#include "all.h" #include "all.h"
#include "compare_exchange.h" #include "compare_exchange.h"

View File

@ -54,7 +54,7 @@ ircd::m::event_append_default_keys
ircd::m::event::append::append(json::stack::array &array, ircd::m::event::append::append(json::stack::array &array,
const event &event_, const event &event_,
const opts &opts) const opts &opts)
:boolean{[&] :returns<bool>{[&]
{ {
assert(array.s); assert(array.s);
json::stack::checkpoint cp json::stack::checkpoint cp
@ -84,7 +84,7 @@ ircd::m::event::append::append(json::stack::array &array,
ircd::m::event::append::append(json::stack::object &object, ircd::m::event::append::append(json::stack::object &object,
const event &event, const event &event,
const opts &opts) const opts &opts)
:boolean{[&] :returns<bool>{[&]
{ {
// Assertions that the event being appended has some required fields. This // Assertions that the event being appended has some required fields. This
// is a central butt-end test of data coming through the system to here. // is a central butt-end test of data coming through the system to here.

View File

@ -93,7 +93,7 @@ noexcept
ircd::m::feds::execute::execute(const vector_view<const opts> &optsv, ircd::m::feds::execute::execute(const vector_view<const opts> &optsv,
const closure &closure) const closure &closure)
:boolean{true} :returns<bool>{true}
{ {
request_list list; request_list list;
for(const auto &opts : optsv) switch(opts.op) for(const auto &opts : optsv) switch(opts.op)
@ -138,7 +138,7 @@ ircd::m::feds::execute::execute(const vector_view<const opts> &optsv,
for(const auto &opts : optsv) for(const auto &opts : optsv)
timeout = std::max(opts.timeout, timeout); timeout = std::max(opts.timeout, timeout);
this->boolean::val = handler(list, timeout, closure); ret = handler(list, timeout, closure);
} }
ircd::m::feds::request_list ircd::m::feds::request_list

View File

@ -110,7 +110,7 @@ ircd::m::push::match::cond_kind_name
ircd::m::push::match::match(const event &event, ircd::m::push::match::match(const event &event,
const rule &rule, const rule &rule,
const match::opts &opts) const match::opts &opts)
:boolean{[&event, &rule, &opts] :returns<bool>{[&event, &rule, &opts]
{ {
if(json::get<"pattern"_>(rule)) if(json::get<"pattern"_>(rule))
{ {
@ -137,7 +137,7 @@ ircd::m::push::match::match(const event &event,
ircd::m::push::match::match(const event &event, ircd::m::push::match::match(const event &event,
const cond &cond, const cond &cond,
const match::opts &opts) const match::opts &opts)
:boolean{[&event, &cond, &opts] :returns<bool>{[&event, &cond, &opts]
{ {
const string_view &kind const string_view &kind
{ {

View File

@ -11,7 +11,7 @@
ircd::m::room::power::revoke::revoke(json::stack::object &out, ircd::m::room::power::revoke::revoke(json::stack::object &out,
const room::power &power, const room::power &power,
const pair<string_view> &prop_key) const pair<string_view> &prop_key)
:boolean{false} :returns<bool>{false}
{ {
bool &ret(*this); bool &ret(*this);
const auto replace{[&ret] const auto replace{[&ret]
@ -71,7 +71,7 @@ ircd::m::room::power::grant::grant(json::stack::object &out,
const room::power &power, const room::power &power,
const pair<string_view> &prop_key, const pair<string_view> &prop_key,
const int64_t &level) const int64_t &level)
:boolean{false} :returns<bool>{false}
{ {
bool &ret(*this); bool &ret(*this);
const auto replace{[&ret, &level] const auto replace{[&ret, &level]