C/C++ API Reference
Loading...
Searching...
No Matches
properties.h
1// Copyright 2025 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 <cstdint>
17#include <type_traits>
18
19namespace pw {
20
22
24enum class MultiBufProperty : uint8_t {
28 kConst = 1 << 0,
29
33 kLayerable = 1 << 1,
34
39 kObservable = 1 << 2,
40};
41
42template <MultiBufProperty...>
43class BasicMultiBuf;
44
45namespace multibuf_impl {
46
47class GenericMultiBuf;
48
51template <MultiBufProperty>
52constexpr bool PropertiesAreInOrderWithoutDuplicates() {
53 return true;
54}
55template <MultiBufProperty kLhs,
57 MultiBufProperty... kOthers>
58constexpr bool PropertiesAreInOrderWithoutDuplicates() {
59 return (kLhs < kRhs) &&
60 PropertiesAreInOrderWithoutDuplicates<kRhs, kOthers...>();
61}
63
65template <MultiBufProperty... kProperties>
66constexpr bool PropertiesAreValid() {
67 if constexpr (sizeof...(kProperties) != 0) {
68 static_assert(PropertiesAreInOrderWithoutDuplicates<kProperties...>(),
69 "Properties must be specified in the following order, "
70 "without duplicates: kConst, kLayerable, kObservable");
71 }
72 return true;
73}
74
76template <typename>
77struct IsBasicMultiBuf : public std::false_type {};
78
79template <MultiBufProperty... kProperties>
80struct IsBasicMultiBuf<BasicMultiBuf<kProperties...>> : public std::true_type {
81};
82
85template <typename From, typename To>
86using EnableIfConvertible =
87 std::enable_if_t<std::is_same_v<To, GenericMultiBuf> ||
88 // Only conversion to other MultiBuf types are supported.
90 // Read-only data cannot be converted to mutable data.
91 (!From::is_const() || To::is_const()) &&
92 // Flat MultiBufs do not have layer-related methods.
93 (From::is_layerable() || !To::is_layerable()) &&
94 // Untracked MultiBufs do not have observer-related
95 // methods.
96 (From::is_observable() || !To::is_observable()))>;
97
100template <typename From, typename To>
101static constexpr void AssertIsConvertible() {
102 if constexpr (!std::is_same_v<To, GenericMultiBuf>) {
103 static_assert(IsBasicMultiBuf<To>::value,
104 "Only conversion to other MultiBuf types are supported.");
105 static_assert(!From::is_const() || To::is_const(),
106 "Read-only data cannot be converted to mutable data.");
107 static_assert(From::is_layerable() || !To::is_layerable(),
108 "Flat MultiBufs do not have layer-related methods.");
109 static_assert(From::is_observable() || !To::is_observable(),
110 "Untracked MultiBufs do not have observer-related methods.");
111 }
112}
113
114} // namespace multibuf_impl
115
117
118} // namespace pw
Definition: multibuf_v2.h:192
MultiBufProperty
Basic properties of a MultiBuf.
Definition: properties.h:24
The Pigweed namespace.
Definition: alignment.h:27
Type trait to identify MultiBuf types.
Definition: properties.h:77