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_MUTABLE_BUFFER_H
|
|
|
|
|
|
|
|
/// Base for mutable buffers, or buffers which can be written to because they
|
|
|
|
/// are not const.
|
|
|
|
///
|
|
|
|
struct ircd::buffer::mutable_buffer
|
|
|
|
:buffer<char *>
|
|
|
|
{
|
|
|
|
// The definition for this is somewhere in one of the .cc files.
|
|
|
|
/// Conversion offered for the analogous asio buffer.
|
2022-03-22 20:17:07 +01:00
|
|
|
operator boost::asio::mutable_buffer() const noexcept;
|
2018-03-13 05:17:09 +01:00
|
|
|
|
2022-07-07 02:32:25 +02:00
|
|
|
template<class R,
|
|
|
|
bool safety = true>
|
|
|
|
explicit operator R *() const noexcept(!safety);
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
/// Allows boost::spirit to append to the buffer; this means the size() of
|
|
|
|
/// this buffer becomes a consumption counter and the real size of the buffer
|
|
|
|
/// must be kept separately. This is the lowlevel basis for a stream buffer.
|
|
|
|
void insert(char *const &it, const value_type &v);
|
|
|
|
|
|
|
|
using buffer<char *>::buffer;
|
2021-03-15 01:04:36 +01:00
|
|
|
template<class T, size_t SIZE> explicit mutable_buffer(T (&)[SIZE]);
|
2018-03-13 05:17:09 +01:00
|
|
|
template<size_t SIZE> mutable_buffer(char (&buf)[SIZE]);
|
|
|
|
template<size_t SIZE> mutable_buffer(std::array<char, SIZE> &buf);
|
|
|
|
mutable_buffer(const std::function<void (const mutable_buffer &)> &);
|
|
|
|
explicit mutable_buffer(std::string &buf);
|
|
|
|
mutable_buffer(const buffer<char *> &b);
|
|
|
|
mutable_buffer() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(const buffer<char *> &b)
|
|
|
|
:buffer<char *>{b}
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// lvalue string reference offered to write through to a std::string as
|
|
|
|
/// the buffer. should be hard to bind by accident...
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(std::string &buf)
|
|
|
|
:mutable_buffer{const_cast<char *>(buf.data()), buf.size()}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(const std::function<void (const mutable_buffer &)> &closure)
|
|
|
|
{
|
|
|
|
closure(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
2020-07-11 05:04:39 +02:00
|
|
|
inline __attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(char (&buf)[SIZE])
|
|
|
|
:buffer<char *>{buf, SIZE}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline __attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(std::array<char, SIZE> &buf)
|
|
|
|
:buffer<char *>{buf.data(), SIZE}
|
|
|
|
{}
|
|
|
|
|
2021-03-15 01:04:36 +01:00
|
|
|
template<class T,
|
|
|
|
size_t SIZE>
|
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::mutable_buffer::mutable_buffer(T (&buf)[SIZE])
|
|
|
|
:buffer<char *>{reinterpret_cast<char *>(buf), SIZE * sizeof(T)}
|
|
|
|
{}
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
inline void
|
|
|
|
ircd::buffer::mutable_buffer::insert(char *const &it,
|
|
|
|
const value_type &v)
|
|
|
|
{
|
|
|
|
assert(it >= this->begin() && it <= this->end());
|
|
|
|
memmove(it + 1, it, std::distance(it, this->end()));
|
|
|
|
*it = v;
|
|
|
|
++std::get<1>(*this);
|
|
|
|
}
|
|
|
|
|
2022-07-07 02:32:25 +02:00
|
|
|
template<class R,
|
|
|
|
bool safety>
|
|
|
|
inline ircd::buffer::mutable_buffer::operator
|
|
|
|
R *()
|
|
|
|
const noexcept(!safety)
|
|
|
|
{
|
|
|
|
assert(sizeof(R) <= size(*this));
|
|
|
|
if constexpr(safety)
|
|
|
|
if(unlikely(sizeof(R) > size(*this)))
|
|
|
|
throw std::bad_cast{};
|
|
|
|
|
|
|
|
return reinterpret_cast<const R *>(data(*this));
|
|
|
|
}
|