Files

158 lines
4.8 KiB
C++
Raw Permalink Normal View History

2016-08-16 16:51:22 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
* Copyright (C) 2016, 2019-2024 Tom Zander <tom@flowee.org>
2016-08-16 16:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_CONSTBUFFER_H
#define FLOWEE_CONSTBUFFER_H
2016-08-16 16:51:22 +02:00
#include <memory>
#include <boost/asio/buffer.hpp>
2019-03-09 19:57:40 +01:00
#include "Logger.h"
2016-08-16 16:51:22 +02:00
namespace Streaming
{
/**
* Class for holding a reference to an slice of the buffer in buffer_base
* Since it has a reference to the actual buffer with a shared_array
* the underlying memory will not be deallocated until all references are removed
*/
class ConstBuffer
{
public:
2019-10-05 16:00:31 +02:00
static ConstBuffer create(const char *start, size_t size);
static ConstBuffer create(const std::vector<unsigned char> &vector);
2016-08-16 16:51:22 +02:00
/// creates an invalid buffer
ConstBuffer();
bool isValid() const {
return m_start != nullptr && m_stop != nullptr;
}
2019-08-24 22:24:39 +02:00
bool isEmpty() const {
return m_start == m_stop;
}
2016-08-16 16:51:22 +02:00
/// Construct from already allocated storage.
/// Keep in mind that a shared_ptr can have a custom dtor if we want to send something special
2018-03-07 22:07:03 +01:00
explicit ConstBuffer(std::shared_ptr<char> buffer, char const *start, char const *stop);
2016-08-16 16:51:22 +02:00
char const* begin() const;
inline char const* cbegin() const {
return begin();
}
char const* end() const;
inline char const* cend() const {
return end();
}
/// standard indexing operator - assumes begin() + idx < end()
char operator[](size_t idx) const;
char const* constData() const;
/// returns end - begin
int size() const;
2022-02-08 19:03:14 +01:00
/// return a std::string copy
2022-01-25 23:59:54 +01:00
inline std::string toString() const {
return std::string(begin(), size());
}
/// return the buffer as hex string.
std::string toHex() const {
return toHex(Forward);
}
/// return the buffer as hex string, in reversed order.
std::string toHex_reversed() const {
return toHex(Reversed);
}
/// return the index in the buffer where byte \a k is found, starting at 'begin()' plus \a offset.
/// returns -1 if not found before end()
2024-06-11 21:34:41 +02:00
int indexOf(char k, int offset = 0) const;
2022-01-25 23:59:54 +01:00
2016-08-16 16:51:22 +02:00
/// Implement ConvertibleToConstBuffer for asio
operator boost::asio::const_buffer() const;
2022-02-08 19:03:14 +01:00
/**
* Return a section of this buffer.
* Notice that due to the nature of ConstBuffer, this is a O1 operation
* that does not copy any actual data.
*/
2019-08-23 22:34:33 +02:00
ConstBuffer mid(int offset, int length = -1) const;
2017-05-03 16:08:13 +02:00
2022-02-08 19:03:14 +01:00
/**
* Returns true if this buffer starts with the \a other bytes
*/
2019-08-26 20:48:12 +02:00
bool startsWith(const Streaming::ConstBuffer &other) const;
2024-06-11 17:57:17 +02:00
bool startsWith(char byte) const;
2019-08-26 20:48:12 +02:00
2024-06-11 17:57:17 +02:00
bool endsWith(const Streaming::ConstBuffer &other) const;
bool endsWith(char byte) const;
2022-02-08 19:03:14 +01:00
/**
* Compares the two buffers on having the same contents (byte-series).
*/
2019-12-30 17:37:23 +01:00
bool operator==(const Streaming::ConstBuffer &other) const;
2022-02-08 19:03:14 +01:00
/**
* Compares the two buffers on having the same contents (byte-series).
*/
2020-01-04 21:08:38 +01:00
inline bool operator!=(const Streaming::ConstBuffer &other) const { return !operator==(other); }
/**
* Drop current data and make this an invalid and empty buffer.
*/
void clear();
2019-12-30 17:37:23 +01:00
2022-02-08 19:03:14 +01:00
/// \internal
std::shared_ptr<char> internal_buffer() const;
2016-08-16 16:51:22 +02:00
private:
enum Direction_ { Forward, Reversed };
std::string toHex(Direction_ dir) const;
2016-08-16 16:51:22 +02:00
std::shared_ptr<char> m_buffer;
char const* m_start;
char const* m_stop;
};
}
2019-03-09 19:57:40 +01:00
inline Log::Item operator<<(Log::Item item, const Streaming::ConstBuffer &buf) {
if (item.isEnabled()) {
const bool old = item.useSpace();
item.nospace() << '{';
const bool tooLong = buf.size() > 80;
const uint8_t *end = reinterpret_cast<const uint8_t*>(tooLong ? buf.begin() + 77 : buf.end());
for (const uint8_t *p = reinterpret_cast<const uint8_t*>(buf.begin()); p < end; ++p) {
char h = '0' + (*p >> 4);
if (h > '9') h += 7;
char l = '0' + (*p & 0xF);
if (l > '9') l += 7;
item << h << l;
}
if (tooLong) item << "...";
item << '}';
if (old)
return item.space();
}
return item;
}
inline Log::SilentItem operator<<(Log::SilentItem item, const Streaming::ConstBuffer&) { return item; }
2016-08-16 16:51:22 +02:00
#endif