mirror of
https://github.com/matrix-construct/construct
synced 2024-12-24 14:34:00 +01:00
ircd::util: Generalize util::boolean into returns template.
This commit is contained in:
parent
8ce326da5b
commit
1c6d216daf
13 changed files with 67 additions and 63 deletions
|
@ -22,7 +22,7 @@ namespace ircd
|
|||
/// and '?' characters and equality of the string expressions will be
|
||||
/// determined. Case insensitive.
|
||||
struct ircd::globular_iequals
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
using is_transparent = std::true_type;
|
||||
|
||||
|
@ -31,7 +31,7 @@ struct ircd::globular_iequals
|
|||
template<class A,
|
||||
class 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)))
|
||||
{}
|
||||
};
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace ircd::m
|
|||
/// find and add the prev_state/prev_content for state events, etc.
|
||||
///
|
||||
struct ircd::m::event::append
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
struct opts;
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ namespace ircd::m::feds
|
|||
/// the user can distinguish different requests in their options vector.
|
||||
///
|
||||
struct ircd::m::feds::execute
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
execute(const vector_view<const opts> &, const closure &);
|
||||
execute(const opts &, const closure &);
|
||||
|
|
|
@ -59,7 +59,7 @@ decltype(ircd::m::push::request::list)
|
|||
ircd::instance_list<ircd::m::push::request>::list;
|
||||
|
||||
struct ircd::m::push::match
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
struct opts;
|
||||
using cond_kind_func = bool (*)(const event &, const cond &, const opts &);
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace ircd::m
|
|||
}
|
||||
|
||||
struct ircd::m::redacted
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
redacted(const event::idx &);
|
||||
redacted(const event::id &);
|
||||
|
@ -44,7 +44,7 @@ ircd::m::redacted::redacted(const event::id &event_id)
|
|||
|
||||
inline
|
||||
ircd::m::redacted::redacted(const event::idx &event_idx)
|
||||
:boolean
|
||||
:returns<bool>
|
||||
{
|
||||
event_idx?
|
||||
event::refs(event_idx).has(dbs::ref::M_ROOM_REDACTION):
|
||||
|
|
|
@ -110,14 +110,14 @@ struct ircd::m::room::power
|
|||
};
|
||||
|
||||
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 m::id::user &, const int64_t &);
|
||||
};
|
||||
|
||||
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 m::id::user &);
|
||||
|
|
|
@ -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()}
|
||||
{}
|
||||
};
|
49
include/ircd/util/returns.h
Normal file
49
include/ircd/util/returns.h
Normal 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;
|
||||
};
|
|
@ -62,7 +62,7 @@ namespace ircd
|
|||
#include "closure.h"
|
||||
#include "env.h"
|
||||
#include "test.h"
|
||||
#include "boolean.h"
|
||||
#include "returns.h"
|
||||
#include "maybe.h"
|
||||
#include "all.h"
|
||||
#include "compare_exchange.h"
|
||||
|
|
|
@ -54,7 +54,7 @@ ircd::m::event_append_default_keys
|
|||
ircd::m::event::append::append(json::stack::array &array,
|
||||
const event &event_,
|
||||
const opts &opts)
|
||||
:boolean{[&]
|
||||
:returns<bool>{[&]
|
||||
{
|
||||
assert(array.s);
|
||||
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,
|
||||
const event &event,
|
||||
const opts &opts)
|
||||
:boolean{[&]
|
||||
:returns<bool>{[&]
|
||||
{
|
||||
// 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.
|
||||
|
|
|
@ -93,7 +93,7 @@ noexcept
|
|||
|
||||
ircd::m::feds::execute::execute(const vector_view<const opts> &optsv,
|
||||
const closure &closure)
|
||||
:boolean{true}
|
||||
:returns<bool>{true}
|
||||
{
|
||||
request_list list;
|
||||
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)
|
||||
timeout = std::max(opts.timeout, timeout);
|
||||
|
||||
this->boolean::val = handler(list, timeout, closure);
|
||||
ret = handler(list, timeout, closure);
|
||||
}
|
||||
|
||||
ircd::m::feds::request_list
|
||||
|
|
|
@ -110,7 +110,7 @@ ircd::m::push::match::cond_kind_name
|
|||
ircd::m::push::match::match(const event &event,
|
||||
const rule &rule,
|
||||
const match::opts &opts)
|
||||
:boolean{[&event, &rule, &opts]
|
||||
:returns<bool>{[&event, &rule, &opts]
|
||||
{
|
||||
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,
|
||||
const cond &cond,
|
||||
const match::opts &opts)
|
||||
:boolean{[&event, &cond, &opts]
|
||||
:returns<bool>{[&event, &cond, &opts]
|
||||
{
|
||||
const string_view &kind
|
||||
{
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
ircd::m::room::power::revoke::revoke(json::stack::object &out,
|
||||
const room::power &power,
|
||||
const pair<string_view> &prop_key)
|
||||
:boolean{false}
|
||||
:returns<bool>{false}
|
||||
{
|
||||
bool &ret(*this);
|
||||
const auto replace{[&ret]
|
||||
|
@ -71,7 +71,7 @@ ircd::m::room::power::grant::grant(json::stack::object &out,
|
|||
const room::power &power,
|
||||
const pair<string_view> &prop_key,
|
||||
const int64_t &level)
|
||||
:boolean{false}
|
||||
:returns<bool>{false}
|
||||
{
|
||||
bool &ret(*this);
|
||||
const auto replace{[&ret, &level]
|
||||
|
|
Loading…
Reference in a new issue