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 {
20namespace multibuf {
21
23
25enum class Property : uint8_t {
29 kConst = 1 << 0,
30
34 kLayerable = 1 << 1,
35
40 kObservable = 1 << 2,
41};
42
44
45} // namespace multibuf
46
47template <multibuf::Property...>
48class BasicMultiBuf;
49
50namespace multibuf::internal {
51
52class GenericMultiBuf;
53
56template <Property>
57constexpr bool PropertiesAreInOrderWithoutDuplicates() {
58 return true;
59}
60template <Property kLhs, Property kRhs, Property... kOthers>
61constexpr bool PropertiesAreInOrderWithoutDuplicates() {
62 return (kLhs < kRhs) &&
63 PropertiesAreInOrderWithoutDuplicates<kRhs, kOthers...>();
64}
66
68template <Property... kProperties>
69constexpr bool PropertiesAreValid() {
70 if constexpr (sizeof...(kProperties) != 0) {
71 static_assert(PropertiesAreInOrderWithoutDuplicates<kProperties...>(),
72 "Properties must be specified in the following order, "
73 "without duplicates: kConst, kLayerable, kObservable");
74 }
75 return true;
76}
77
79template <typename>
80struct IsBasicMultiBuf : public std::false_type {};
81
82template <Property... kProperties>
83struct IsBasicMultiBuf<BasicMultiBuf<kProperties...>> : public std::true_type {
84};
85
88template <typename From, typename To>
89using EnableIfConvertible =
90 std::enable_if_t<std::is_same_v<To, GenericMultiBuf> ||
91 // Only conversion to other MultiBuf types are supported.
93 // Read-only data cannot be converted to mutable data.
94 (!From::is_const() || To::is_const()) &&
95 // Flat MultiBufs do not have layer-related methods.
96 (From::is_layerable() || !To::is_layerable()) &&
97 // Untracked MultiBufs do not have observer-related
98 // methods.
99 (From::is_observable() || !To::is_observable()))>;
100
103template <typename From, typename To>
104static constexpr void AssertIsConvertible() {
105 if constexpr (!std::is_same_v<To, GenericMultiBuf>) {
106 static_assert(IsBasicMultiBuf<To>::value,
107 "Only conversion to other MultiBuf types are supported.");
108 static_assert(!From::is_const() || To::is_const(),
109 "Read-only data cannot be converted to mutable data.");
110 static_assert(From::is_layerable() || !To::is_layerable(),
111 "Flat MultiBufs do not have layer-related methods.");
112 static_assert(From::is_observable() || !To::is_observable(),
113 "Untracked MultiBufs do not have observer-related methods.");
114 }
115}
116
117} // namespace multibuf::internal
118} // namespace pw
Definition: multibuf_v2.h:185
Property
Basic properties of a MultiBuf.
Definition: properties.h:25
The Pigweed namespace.
Definition: alignment.h:27
Type trait to identify MultiBuf types.
Definition: properties.h:80