2018-03-13 05:17:09 +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_BUFFER_FIXED_BUFFER_H
|
|
|
|
|
|
|
|
/// fixed_buffer wraps an std::array with construction and conversions apropos
|
|
|
|
/// the ircd::buffer suite. fixed_buffer should be punnable. Its only memory
|
|
|
|
/// footprint is the array itself and
|
|
|
|
///
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type,
|
2018-03-13 05:17:09 +01:00
|
|
|
size_t SIZE>
|
|
|
|
struct ircd::buffer::fixed_buffer
|
2020-10-29 08:08:06 +01:00
|
|
|
:std::array<typename std::remove_const<typename buffer_type::value_type>::type, SIZE>
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
using mutable_type = typename std::remove_const<typename buffer_type::value_type>::type;
|
2018-03-13 05:17:09 +01:00
|
|
|
using const_type = typename std::add_const<mutable_type>::type;
|
|
|
|
using array_type = std::array<mutable_type, SIZE>;
|
2020-10-29 08:08:06 +01:00
|
|
|
using proto_type = void (const mutable_buffer &);
|
|
|
|
using args_type = const mutable_buffer &;
|
2018-03-13 05:17:09 +01:00
|
|
|
|
2022-07-04 22:10:46 +02:00
|
|
|
operator buffer_type() const noexcept;
|
|
|
|
operator buffer_type() noexcept;
|
2018-03-13 05:17:09 +01:00
|
|
|
|
|
|
|
using array_type::array_type;
|
2020-10-29 08:08:06 +01:00
|
|
|
|
|
|
|
template<class F>
|
|
|
|
fixed_buffer(F&&,
|
|
|
|
typename std::enable_if<std::is_invocable<F, args_type>::value, void>::type * = nullptr);
|
|
|
|
|
2022-07-04 22:10:46 +02:00
|
|
|
explicit fixed_buffer(nullptr_t) noexcept;
|
|
|
|
fixed_buffer(const buffer_type &b) noexcept;
|
2018-03-13 05:17:09 +01:00
|
|
|
fixed_buffer() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
// Assertion over an arbitrary but common template configuration.
|
|
|
|
std::is_standard_layout<ircd::buffer::fixed_buffer<ircd::buffer::const_buffer, 32>>::value,
|
|
|
|
"ircd::buffer::fixed_buffer must be standard layout"
|
|
|
|
);
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type,
|
2018-03-13 05:17:09 +01:00
|
|
|
size_t SIZE>
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class F>
|
|
|
|
inline
|
|
|
|
ircd::buffer::fixed_buffer<buffer_type, SIZE>::fixed_buffer(F&& closure,
|
|
|
|
typename std::enable_if<std::is_invocable<F, args_type>::value, void>::type *)
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2022-07-04 22:10:46 +02:00
|
|
|
const mutable_buffer buf
|
2020-10-29 08:08:06 +01:00
|
|
|
{
|
2022-07-04 22:10:46 +02:00
|
|
|
reinterpret_cast<mutable_buffer::iterator>(this->data()), SIZE
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(buf);
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type,
|
|
|
|
size_t SIZE>
|
|
|
|
inline
|
2022-07-04 22:10:46 +02:00
|
|
|
ircd::buffer::fixed_buffer<buffer_type, SIZE>::fixed_buffer(const buffer_type &b)
|
|
|
|
noexcept
|
|
|
|
:array_type
|
|
|
|
{
|
|
|
|
std::begin(b), std::end(b)
|
|
|
|
}
|
2020-10-29 08:08:06 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class buffer_type,
|
2018-03-13 05:17:09 +01:00
|
|
|
size_t SIZE>
|
2020-10-29 08:08:06 +01:00
|
|
|
inline
|
2022-07-04 22:10:46 +02:00
|
|
|
ircd::buffer::fixed_buffer<buffer_type, SIZE>::fixed_buffer(nullptr_t)
|
|
|
|
noexcept
|
|
|
|
:array_type{{0}}
|
2018-03-13 05:17:09 +01:00
|
|
|
{}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type,
|
2018-03-13 05:17:09 +01:00
|
|
|
size_t SIZE>
|
2020-10-29 08:08:06 +01:00
|
|
|
inline
|
|
|
|
ircd::buffer::fixed_buffer<buffer_type, SIZE>::operator
|
|
|
|
buffer_type()
|
2022-07-04 22:10:46 +02:00
|
|
|
noexcept
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
return buffer_type
|
|
|
|
{
|
2022-07-04 22:10:46 +02:00
|
|
|
this->data(), SIZE,
|
2020-10-29 08:08:06 +01:00
|
|
|
};
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class buffer_type,
|
2018-03-13 05:17:09 +01:00
|
|
|
size_t SIZE>
|
2020-10-29 08:08:06 +01:00
|
|
|
inline
|
|
|
|
ircd::buffer::fixed_buffer<buffer_type, SIZE>::operator
|
|
|
|
buffer_type()
|
2022-07-04 22:10:46 +02:00
|
|
|
const noexcept
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
return buffer_type
|
|
|
|
{
|
2022-07-04 22:10:46 +02:00
|
|
|
this->data(), SIZE,
|
2020-10-29 08:08:06 +01:00
|
|
|
};
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|