Path: blob/master/dep/rapidyaml/include/c4/blob.hpp
4261 views
#ifndef _C4_BLOB_HPP_1#define _C4_BLOB_HPP_23#include "c4/types.hpp"4#include "c4/error.hpp"56/** @file blob.hpp Mutable and immutable binary data blobs.7*/89namespace c4 {1011template<class T>12struct blob_;1314namespace detail {15template<class T> struct is_blob_type : std::integral_constant<bool, false> {};16template<class T> struct is_blob_type<blob_<T>> : std::integral_constant<bool, true> {};17template<class T> struct is_blob_value_type : std::integral_constant<bool, (std::is_fundamental<T>::value || std::is_trivially_copyable<T>::value)> {};18} // namespace1920template<class T>21struct blob_22{23static_assert(std::is_same<T, byte>::value || std::is_same<T, cbyte>::value, "must be either byte or cbyte");24static_assert(sizeof(T) == 1u, "must be either byte or cbyte");2526public:2728T * buf;29size_t len;3031public:3233C4_ALWAYS_INLINE blob_() noexcept = default;34C4_ALWAYS_INLINE blob_(blob_ const& that) noexcept = default;35C4_ALWAYS_INLINE blob_(blob_ && that) noexcept = default;36C4_ALWAYS_INLINE blob_& operator=(blob_ && that) noexcept = default;37C4_ALWAYS_INLINE blob_& operator=(blob_ const& that) noexcept = default;3839template<class U, class=typename std::enable_if<std::is_const<T>::value && std::is_same<typename std::add_const<U>::type, T>::value, U>::type> C4_ALWAYS_INLINE blob_(blob_<U> const& that) noexcept : buf(that.buf), len(that.len) {}40template<class U, class=typename std::enable_if<std::is_const<T>::value && std::is_same<typename std::add_const<U>::type, T>::value, U>::type> C4_ALWAYS_INLINE blob_(blob_<U> && that) noexcept : buf(that.buf), len(that.len) {}41template<class U, class=typename std::enable_if<std::is_const<T>::value && std::is_same<typename std::add_const<U>::type, T>::value, U>::type> C4_ALWAYS_INLINE blob_& operator=(blob_<U> && that) noexcept { buf = that.buf; len = that.len; }42template<class U, class=typename std::enable_if<std::is_const<T>::value && std::is_same<typename std::add_const<U>::type, T>::value, U>::type> C4_ALWAYS_INLINE blob_& operator=(blob_<U> const& that) noexcept { buf = that.buf; len = that.len; }4344C4_ALWAYS_INLINE blob_(void *ptr, size_t n) noexcept : buf(reinterpret_cast<T*>(ptr)), len(n) {}45C4_ALWAYS_INLINE blob_(void const *ptr, size_t n) noexcept : buf(reinterpret_cast<T*>(ptr)), len(n) {}4647#define _C4_REQUIRE_BLOBTYPE(ty) class=typename std::enable_if<((!detail::is_blob_type<ty>::value) && (detail::is_blob_value_type<ty>::value)), T>::type48template<class U, _C4_REQUIRE_BLOBTYPE(U)> C4_ALWAYS_INLINE blob_(U &var) noexcept : buf(reinterpret_cast<T*>(&var)), len(sizeof(U)) {}49template<class U, _C4_REQUIRE_BLOBTYPE(U)> C4_ALWAYS_INLINE blob_(U *ptr, size_t n) noexcept : buf(reinterpret_cast<T*>(ptr)), len(sizeof(U) * n) { C4_ASSERT(is_aligned(ptr)); }50template<class U, _C4_REQUIRE_BLOBTYPE(U)> C4_ALWAYS_INLINE blob_& operator= (U &var) noexcept { buf = reinterpret_cast<T*>(&var); len = sizeof(U); return *this; }51template<class U, size_t N, _C4_REQUIRE_BLOBTYPE(U)> C4_ALWAYS_INLINE blob_(U (&arr)[N]) noexcept : buf(reinterpret_cast<T*>(arr)), len(sizeof(U) * N) {}52template<class U, size_t N, _C4_REQUIRE_BLOBTYPE(U)> C4_ALWAYS_INLINE blob_& operator= (U (&arr)[N]) noexcept { buf = reinterpret_cast<T*>(arr); len = sizeof(U) * N; return *this; }53#undef _C4_REQUIRE_BLOBTYPE54};5556/** an immutable binary blob */57using cblob = blob_<cbyte>;58/** a mutable binary blob */59using blob = blob_< byte>;6061C4_MUST_BE_TRIVIAL_COPY(blob);62C4_MUST_BE_TRIVIAL_COPY(cblob);6364} // namespace c46566#endif // _C4_BLOB_HPP_676869