diff --git a/src/util/ConstBuffer.hxx b/src/util/ConstBuffer.hxx index f0fa0fa6a..51c19cc38 100644 --- a/src/util/ConstBuffer.hxx +++ b/src/util/ConstBuffer.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 Max Kellermann + * Copyright 2013-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,12 +27,19 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef CONST_BUFFER_HXX -#define CONST_BUFFER_HXX +#pragma once #include #include +#if __cplusplus >= 202002 || (defined(__GNUC__) && __GNUC__ >= 10) +#include +#endif + +#ifdef __cpp_lib_span +#include +#endif + template struct ConstBuffer; @@ -56,6 +63,11 @@ struct ConstBuffer { constexpr ConstBuffer(pointer _data, size_type _size) noexcept :data(_data), size(_size) {} +#ifdef __cpp_lib_span + constexpr ConstBuffer(std::span s) noexcept + :data(s.data()), size(s.size()) {} +#endif + constexpr static ConstBuffer FromVoid(ConstBuffer other) noexcept { return other; } @@ -79,6 +91,12 @@ struct ConstBuffer { constexpr bool empty() const noexcept { return size == 0; } + +#ifdef __cpp_lib_span + constexpr operator std::span() const noexcept { + return {static_cast(data), size}; + } +#endif }; /** @@ -116,6 +134,11 @@ struct ConstBuffer { constexpr ConstBuffer(const T (&_data)[_size]) noexcept :data(_data), size(_size) {} +#ifdef __cpp_lib_span + constexpr ConstBuffer(std::span s) noexcept + :data(s.data()), size(s.size()) {} +#endif + /** * Cast a ConstBuffer to a ConstBuffer, rounding down * to the next multiple of T's size. @@ -268,6 +291,10 @@ struct ConstBuffer { size = new_end - data; } -}; +#ifdef __cpp_lib_span + constexpr operator std::span() const noexcept { + return {data, size}; + } #endif +}; diff --git a/src/util/WritableBuffer.hxx b/src/util/WritableBuffer.hxx index a3207bd83..f93125a12 100644 --- a/src/util/WritableBuffer.hxx +++ b/src/util/WritableBuffer.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 Max Kellermann + * Copyright 2013-2022 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -27,14 +27,21 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef WRITABLE_BUFFER_HXX -#define WRITABLE_BUFFER_HXX +#pragma once #include "ConstBuffer.hxx" #include #include +#if __cplusplus >= 202002 || (defined(__GNUC__) && __GNUC__ >= 10) +#include +#endif + +#ifdef __cpp_lib_span +#include +#endif + template struct WritableBuffer; @@ -58,6 +65,11 @@ struct WritableBuffer { constexpr WritableBuffer(pointer _data, size_type _size) noexcept :data(_data), size(_size) {} +#ifdef __cpp_lib_span + constexpr WritableBuffer(std::span s) noexcept + :data(s.data()), size(s.size()) {} +#endif + constexpr static WritableBuffer FromVoid(WritableBuffer other) noexcept { return other; } @@ -85,6 +97,16 @@ struct WritableBuffer { constexpr bool empty() const noexcept { return size == 0; } + +#ifdef __cpp_lib_span + constexpr operator std::span() const noexcept { + return {static_cast(data), size}; + } + + constexpr operator std::span() const noexcept { + return {static_cast(data), size}; + } +#endif }; /** @@ -117,6 +139,11 @@ struct WritableBuffer { constexpr WritableBuffer(pointer _data, pointer _end) noexcept :data(_data), size(_end - _data) {} +#ifdef __cpp_lib_span + constexpr WritableBuffer(std::span s) noexcept + :data(s.data()), size(s.size()) {} +#endif + /** * Convert array to WritableBuffer instance. */ @@ -271,6 +298,14 @@ struct WritableBuffer { size = new_end - data; } -}; +#ifdef __cpp_lib_span + constexpr operator std::span() const noexcept { + return {data, size}; + } + + constexpr operator std::span() const noexcept { + return {data, size}; + } #endif +};