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_CONST_BUFFER_H
|
|
|
|
|
|
|
|
struct ircd::buffer::const_buffer
|
|
|
|
:buffer<const char *>
|
|
|
|
{
|
|
|
|
// Definition for this is somewhere in the .cc files where boost is incl.
|
2022-03-22 20:17:07 +01:00
|
|
|
operator boost::asio::const_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 const R *() const noexcept(!safety);
|
|
|
|
|
2018-07-02 04:49:49 +02:00
|
|
|
// For boost::spirit conceptual compliance; illegal/noop
|
|
|
|
void insert(const char *const &, const char &);
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
using buffer<const char *>::buffer;
|
2021-03-15 01:04:36 +01:00
|
|
|
template<class T, size_t SIZE> explicit const_buffer(const T (&)[SIZE]);
|
2018-03-13 05:17:09 +01:00
|
|
|
template<size_t SIZE> const_buffer(const char (&buf)[SIZE]);
|
|
|
|
template<size_t SIZE> const_buffer(const std::array<char, SIZE> &buf);
|
|
|
|
const_buffer(const buffer<const char *> &b);
|
2019-12-31 18:45:38 +01:00
|
|
|
const_buffer(const buffer<char *> &b, const size_t &);
|
2018-03-13 05:17:09 +01:00
|
|
|
const_buffer(const buffer<char *> &b);
|
|
|
|
const_buffer(const mutable_buffer &b);
|
|
|
|
const_buffer(const string_view &s);
|
|
|
|
const_buffer() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::const_buffer::const_buffer(const buffer<const char *> &b)
|
|
|
|
:buffer<const char *>{b}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::const_buffer::const_buffer(const buffer<char *> &b)
|
|
|
|
:buffer<const char *>{data(b), size(b)}
|
|
|
|
{}
|
|
|
|
|
2019-12-31 18:45:38 +01:00
|
|
|
inline
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::buffer::const_buffer::const_buffer(const buffer<char *> &b,
|
|
|
|
const size_t &max)
|
|
|
|
:buffer<const char *>{{data(b), size(b)}, max}
|
|
|
|
{}
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::const_buffer::const_buffer(const mutable_buffer &b)
|
|
|
|
:buffer<const char *>{data(b), size(b)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2019-05-08 08:51:15 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::const_buffer::const_buffer(const string_view &s)
|
|
|
|
:buffer<const char *>{data(s), size(s)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
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::const_buffer::const_buffer(const char (&buf)[SIZE])
|
|
|
|
:buffer<const 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::const_buffer::const_buffer(const std::array<char, SIZE> &buf)
|
|
|
|
:buffer<const char *>{reinterpret_cast<const char *>(buf.data()), SIZE}
|
|
|
|
{}
|
2018-07-02 04:49:49 +02:00
|
|
|
|
2021-03-15 01:04:36 +01:00
|
|
|
template<class T,
|
|
|
|
size_t SIZE>
|
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::const_buffer::const_buffer(const T (&buf)[SIZE])
|
|
|
|
:buffer<const char *>{reinterpret_cast<const char *>(buf), SIZE * sizeof(T)}
|
|
|
|
{}
|
|
|
|
|
2018-07-02 04:49:49 +02:00
|
|
|
#ifndef _NDEBUG
|
|
|
|
__attribute__((noreturn))
|
|
|
|
#endif
|
|
|
|
inline void
|
|
|
|
ircd::buffer::const_buffer::insert(const char *const &,
|
|
|
|
const char &)
|
|
|
|
{
|
|
|
|
assert(0);
|
2019-06-21 12:07:33 +02:00
|
|
|
__builtin_unreachable();
|
2018-07-02 04:49:49 +02:00
|
|
|
}
|
2022-07-07 02:32:25 +02:00
|
|
|
|
|
|
|
template<class R,
|
|
|
|
bool safety>
|
|
|
|
inline ircd::buffer::const_buffer::operator
|
|
|
|
const 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));
|
|
|
|
}
|