Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
span.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17#include <memory>
18#include <type_traits>
19
20#include "pw_span/span.h"
21
22namespace pw {
23
24// Aliases for spans of bytes.
25using ByteSpan = span<std::byte>;
26
27using ConstByteSpan = span<const std::byte>;
28
33template <typename T>
34span<const std::byte, sizeof(T)> ObjectAsBytes(const T& obj) {
35 static_assert(std::is_trivially_copyable_v<T>);
36 static_assert(std::has_unique_object_representations_v<T>);
37
38 auto s = pw::span<const T, 1>(std::addressof(obj), 1);
39 return pw::as_bytes(s);
40}
41
45template <typename T>
46span<std::byte, sizeof(T)> ObjectAsWritableBytes(T& obj) {
47 static_assert(std::is_trivially_copyable_v<T>);
48 static_assert(std::has_unique_object_representations_v<T>);
49
50 auto s = pw::span<T, 1>(std::addressof(obj), 1);
51 return pw::as_writable_bytes(s);
52}
53
54} // namespace pw
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27
span< const std::byte, sizeof(T)> ObjectAsBytes(const T &obj)
Definition: span.h:34
span< std::byte, sizeof(T)> ObjectAsWritableBytes(T &obj)
Definition: span.h:46