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::multibuf::v2 {
20
22
24enum class Property : uint8_t {
28 kConst = 1 << 0,
29
33 kLayerable = 1 << 1,
34
39 kObservable = 1 << 2,
40};
41
43
44template <Property...>
45class BasicMultiBuf;
46
47namespace internal {
48
49class GenericMultiBuf;
50
53template <Property>
54constexpr bool PropertiesAreInOrderWithoutDuplicates() {
55 return true;
56}
57template <Property kLhs, Property kRhs, Property... kOthers>
58constexpr bool PropertiesAreInOrderWithoutDuplicates() {
59 return (kLhs < kRhs) &&
60 PropertiesAreInOrderWithoutDuplicates<kRhs, kOthers...>();
61}
63
65template <Property... 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 <Property... kProperties>
80struct IsBasicMultiBuf<BasicMultiBuf<kProperties...>> : public std::true_type {
81};
82
90template <typename From, typename To>
91using EnableIfConvertible =
92 std::enable_if_t<std::is_same_v<To, GenericMultiBuf> ||
93 // Only conversion to other MultiBuf types are supported.
95 // Read-only data cannot be converted to mutable data.
96 (!From::is_const() || To::is_const()) &&
97 // Flat MultiBufs do not have layer-related methods.
98 (From::is_layerable() || !To::is_layerable()) &&
99 // Untracked MultiBufs do not have observer-related
100 // methods.
101 (From::is_observable() || !To::is_observable()))>;
102
106template <typename From, typename To>
107static constexpr void AssertIsConvertibleIgnoreConst() {
108 if constexpr (!std::is_same_v<To, GenericMultiBuf>) {
109 static_assert(IsBasicMultiBuf<To>::value,
110 "Only conversion to other MultiBuf types are supported.");
111 static_assert(From::is_layerable() || !To::is_layerable(),
112 "Flat MultiBufs do not have layer-related methods.");
113 static_assert(From::is_observable() || !To::is_observable(),
114 "Untracked MultiBufs do not have observer-related methods.");
115 }
116}
117
123template <typename From, typename To>
124static constexpr void AssertIsConvertible() {
125 if constexpr (!std::is_same_v<To, GenericMultiBuf>) {
126 AssertIsConvertibleIgnoreConst<From, To>();
127 static_assert(!From::is_const() || To::is_const(),
128 "Read-only data cannot be converted to mutable data.");
129 }
130}
131
139template <typename From, typename To>
140static constexpr void AssertIsAssignable() {
141 if constexpr (!std::is_same_v<To, GenericMultiBuf>) {
142 static_assert(IsBasicMultiBuf<To>::value,
143 "Only assignment to other MultiBuf types are supported.");
144 static_assert(!From::is_const() || To::is_const(),
145 "Read-only data cannot be assigned to mutable data.");
146 static_assert(!From::is_layerable() || To::is_layerable(),
147 "Layered MultiBufs cannot be assigned to flat MultiBufs.");
148 static_assert(
149 !From::is_observable() || To::is_observable(),
150 "Tracked MultiBufs cannot be assigned to untracked MultiBufs.");
151 }
152}
153
154} // namespace internal
155} // namespace pw::multibuf::v2
Definition: multibuf.h:194
Property
Basic properties of a MultiBuf.
Definition: properties.h:24
Type trait to identify MultiBuf types.
Definition: properties.h:77