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_BASE_H
|
|
|
|
|
|
|
|
/// Base for all buffer types
|
|
|
|
///
|
|
|
|
template<class it>
|
|
|
|
struct ircd::buffer::buffer
|
2019-05-08 08:51:15 +02:00
|
|
|
:std::pair<it, it>
|
2018-03-13 05:17:09 +01:00
|
|
|
{
|
|
|
|
using iterator = it;
|
|
|
|
using value_type = typename std::remove_pointer<iterator>::type;
|
|
|
|
|
|
|
|
auto &begin() const { return std::get<0>(*this); }
|
|
|
|
auto &begin() { return std::get<0>(*this); }
|
|
|
|
auto &end() const { return std::get<1>(*this); }
|
|
|
|
auto &end() { return std::get<1>(*this); }
|
|
|
|
|
2019-04-10 22:38:47 +02:00
|
|
|
bool null() const;
|
|
|
|
bool empty() const; // For boost::spirit conceptual compliance.
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
auto &operator[](const size_t &i) const;
|
|
|
|
auto &operator[](const size_t &i);
|
|
|
|
|
2019-04-10 22:38:47 +02:00
|
|
|
explicit operator const it &() const;
|
|
|
|
explicit operator std::string_view() const;
|
|
|
|
explicit operator std::string() const;
|
|
|
|
operator string_view() const;
|
2018-07-02 04:20:00 +02:00
|
|
|
|
2019-12-31 18:45:38 +01:00
|
|
|
buffer(const buffer &start, const size_t &size);
|
2018-03-13 05:17:09 +01:00
|
|
|
buffer(const it &start, const it &stop);
|
|
|
|
buffer(const it &start, const size_t &size);
|
|
|
|
buffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline __attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::buffer<it>::buffer()
|
|
|
|
:buffer{nullptr, nullptr}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class it>
|
2019-05-08 08:51:15 +02:00
|
|
|
inline __attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::buffer<it>::buffer(const it &start,
|
|
|
|
const size_t &size)
|
|
|
|
:buffer{start, start + size}
|
|
|
|
{}
|
|
|
|
|
2019-12-31 18:45:38 +01:00
|
|
|
template<class it>
|
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::buffer<it>::buffer(const buffer &start,
|
|
|
|
const size_t &size)
|
|
|
|
:buffer
|
|
|
|
{
|
|
|
|
data(start), std::min(ircd::buffer::size(start), size)
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2018-03-13 05:17:09 +01:00
|
|
|
template<class it>
|
2020-06-05 05:26:26 +02:00
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::buffer<it>::buffer(const it &start,
|
|
|
|
const it &stop)
|
|
|
|
:std::pair<it, it>{start, stop}
|
|
|
|
{
|
|
|
|
//assert(this->begin() <= this->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
inline __attribute__((always_inline))
|
2022-05-28 09:34:04 +02:00
|
|
|
ircd::buffer::buffer<it>::operator std::string()
|
2018-03-13 05:17:09 +01:00
|
|
|
const
|
|
|
|
{
|
2022-05-28 09:34:04 +02:00
|
|
|
return std::string(static_cast<std::string_view>(*this));
|
2019-04-10 22:38:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2022-05-28 09:34:04 +02:00
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::buffer<it>::operator std::string_view()
|
2019-04-10 22:38:47 +02:00
|
|
|
const
|
|
|
|
{
|
2022-05-28 09:34:04 +02:00
|
|
|
return static_cast<ircd::string_view>(*this);
|
2019-04-10 22:38:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2022-05-28 09:34:04 +02:00
|
|
|
inline __attribute__((always_inline))
|
|
|
|
ircd::buffer::buffer<it>::operator string_view()
|
2019-04-10 22:38:47 +02:00
|
|
|
const
|
|
|
|
{
|
2022-05-28 09:34:04 +02:00
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
const_cast<const char *>(data(*this)), size(*this)
|
|
|
|
};
|
2018-03-13 05:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline auto &
|
|
|
|
__attribute__((always_inline))
|
2018-03-13 05:17:09 +01:00
|
|
|
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
|
|
|
{
|
2019-04-10 22:38:47 +02:00
|
|
|
assert(begin() + i < end());
|
2018-03-13 05:17:09 +01:00
|
|
|
return *(begin() + i);
|
|
|
|
}
|
2018-11-14 09:31:55 +01:00
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline auto &
|
|
|
|
__attribute__((always_inline))
|
2019-04-10 22:38:47 +02:00
|
|
|
ircd::buffer::buffer<it>::operator[](const size_t &i)
|
2018-11-14 09:31:55 +01:00
|
|
|
const
|
|
|
|
{
|
2019-04-10 22:38:47 +02:00
|
|
|
assert(begin() + i < end());
|
|
|
|
return *(begin() + i);
|
2018-11-14 09:31:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2019-04-10 22:38:47 +02:00
|
|
|
ircd::buffer::buffer<it>::empty()
|
2018-11-14 09:31:55 +01:00
|
|
|
const
|
|
|
|
{
|
2019-04-10 22:38:47 +02:00
|
|
|
return null() || std::distance(begin(), end()) == 0;
|
2018-11-14 09:31:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
2019-04-10 22:38:47 +02:00
|
|
|
ircd::buffer::buffer<it>::null()
|
2018-11-14 09:31:55 +01:00
|
|
|
const
|
|
|
|
{
|
2019-04-10 22:38:47 +02:00
|
|
|
return !begin();
|
2018-11-14 09:31:55 +01:00
|
|
|
}
|
2019-02-09 03:22:40 +01:00
|
|
|
|
|
|
|
template<class it>
|
2019-07-11 03:50:27 +02:00
|
|
|
inline __attribute__((always_inline))
|
2019-02-09 03:22:40 +01:00
|
|
|
ircd::buffer::buffer<it>::operator
|
|
|
|
const it &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return begin();
|
|
|
|
}
|