// Copyright (c) 2020 Egor Tensin // 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 #include #include #include #include #include #include namespace winapi { class Buffer : public std::vector { public: typedef std::vector Parent; Buffer() = default; explicit Buffer(Parent&& src) : Parent(std::move(src)) {} template explicit Buffer(const std::basic_string& src) { set(src); } explicit Buffer(const void* src, std::size_t nb) { set(src, nb); } template void set(const std::basic_string& src) { set(src.c_str(), src.length() * sizeof(std::basic_string::char_type)); } 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(data()); const auto nb = size(); const auto nch = nb; return {c_str, nch}; } std::wstring as_utf16() const { const auto c_str = reinterpret_cast(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(); resize(size() + src.size()); std::memcpy(data() + nb, src.data(), src.size()); } }; } // namespace winapi