blob: 11d029f48afff8257fa02b3cbf52857b8a5cac20 (
plain) (
tree)
|
|
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-common" project.
// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.
#pragma once
#include <SafeInt.hpp>
#include <cstddef>
#include <cstring>
#include <initializer_list>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace winapi {
class Buffer : public std::vector<unsigned char> {
public:
typedef std::vector<unsigned char> Parent;
Buffer() = default;
Buffer(std::initializer_list<unsigned char> lst) : Parent(lst) {}
explicit Buffer(Parent&& src) : Parent(std::move(src)) {}
template <typename CharT>
explicit Buffer(const std::basic_string<CharT>& src) {
set(src);
}
Buffer(const void* src, std::size_t nb) { set(src, nb); }
template <typename CharT>
void set(const std::basic_string<CharT>& src) {
std::size_t new_size = 0;
if (!SafeMultiply(src.length(), sizeof(std::basic_string<CharT>::char_type), new_size)) {
throw std::runtime_error{"Destination buffer size is too large"};
}
set(src.c_str(), new_size);
}
void set(const void* src, std::size_t nb) {
resize(nb);
std::memcpy(data(), src, nb);
}
std::string as_utf8() const {
const auto c_str = reinterpret_cast<const char*>(data());
const auto nb = size();
const auto nch = nb;
return {c_str, nch};
}
std::wstring as_utf16() const {
const auto c_str = reinterpret_cast<const wchar_t*>(data());
const auto nb = size();
if (nb % 2 != 0) {
std::ostringstream oss;
oss << "Buffer size invalid at " << nb << " bytes";
throw std::runtime_error{oss.str()};
}
const auto nch = nb / 2;
return {c_str, nch};
}
void add(const Buffer& src) {
const auto nb = size();
{
std::size_t new_size = 0;
if (!SafeAdd(size(), src.size(), new_size)) {
throw std::runtime_error{"Destination buffer size is too large"};
}
resize(new_size);
}
std::memcpy(data() + nb, src.data(), src.size());
}
};
} // namespace winapi
|