Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 {
20
22enum class Property : uint8_t {
26 kConst = 1 << 0,
27
31 kLayerable = 1 << 1,
32
37 kObservable = 1 << 2,
38};
39
40namespace internal {
41
44template <Property>
45constexpr bool PropertiesAreInOrderWithoutDuplicates() {
46 return true;
47}
48template <Property kLhs, Property kRhs, Property... kOthers>
49constexpr bool PropertiesAreInOrderWithoutDuplicates() {
50 return (kLhs < kRhs) &&
51 PropertiesAreInOrderWithoutDuplicates<kRhs, kOthers...>();
52}
54
56template <Property... kProperties>
57constexpr bool PropertiesAreValid() {
58 if constexpr (sizeof...(kProperties) != 0) {
59 static_assert(PropertiesAreInOrderWithoutDuplicates<kProperties...>(),
60 "Properties must be specified in the following order, "
61 "without duplicates: kConst, kLayerable, kObservable");
62 }
63 return true;
64}
65
66} // namespace internal
67
68// Forward declarations.
69template <Property...>
70class BasicMultiBuf;
71
72namespace internal {
73
74class GenericMultiBuf;
75
77template <typename>
78struct IsBasicMultiBuf : public std::false_type {};
79
80template <Property... kProperties>
81struct IsBasicMultiBuf<BasicMultiBuf<kProperties...>> : public std::true_type {
82};
83
86template <typename From, typename To>
87using EnableIfConvertible =
88 std::enable_if_t<std::is_same_v<To, internal::GenericMultiBuf> ||
89 // Only conversion to other MultiBuf types are supported.
91 // Read-only data cannot be converted to mutable data.
92 (!From::is_const() || To::is_const()) &&
93 // Flat MultiBufs do not have layer-related methods.
94 (From::is_layerable() || !To::is_layerable()) &&
95 // Untracked MultiBufs do not have observer-related
96 // methods.
97 (From::is_observable() || !To::is_observable()))>;
98
101template <typename From, typename To>
102static constexpr void AssertIsConvertible() {
103 if constexpr (!std::is_same_v<To, internal::GenericMultiBuf>) {
104 static_assert(IsBasicMultiBuf<To>::value,
105 "Only conversion to other MultiBuf types are supported.");
106 static_assert(!From::is_const() || To::is_const(),
107 "Read-only data cannot be converted to mutable data.");
108 static_assert(From::is_layerable() || !To::is_layerable(),
109 "Flat MultiBufs do not have layer-related methods.");
110 static_assert(From::is_observable() || !To::is_observable(),
111 "Untracked MultiBufs do not have observer-related methods.");
112 }
113}
114
115} // namespace internal
116} // namespace pw::multibuf
Definition: multibuf_v2.h:217
Type trait to identify MultiBuf types.
Definition: properties.h:78