Pigweed
 
Loading...
Searching...
No Matches
capability.h
1// Copyright 2024 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
18namespace pw::allocator {
19
26enum Capability : uint32_t {
27 // clang-format off
28 kImplementsGetRequestedLayout = 1 << 0,
29 kImplementsGetUsableLayout = 1 << 1,
30 kImplementsGetAllocatedLayout = 1 << 2,
31 kImplementsGetCapacity = 1 << 4,
32 kImplementsRecognizes = 1 << 5,
33 kSkipsDestroy = 1 << 6,
34 // clang-format on
35};
36
63 public:
64 constexpr Capabilities() : capabilities_(0) {}
65 constexpr Capabilities(uint32_t capabilities) : capabilities_(capabilities) {}
66
67 constexpr bool has(Capability capability) const {
68 return (capabilities_ & capability) == capability;
69 }
70
71 constexpr uint32_t get() const { return capabilities_; }
72
73 private:
74 const uint32_t capabilities_;
75};
76
77inline constexpr bool operator==(const Capabilities& lhs,
78 const Capabilities& rhs) {
79 return lhs.get() == rhs.get();
80}
81
82inline constexpr bool operator!=(const Capabilities& lhs,
83 const Capabilities& rhs) {
84 return lhs.get() != rhs.get();
85}
86
87inline constexpr Capabilities operator|(const Capabilities& lhs,
88 const Capabilities& rhs) {
89 return Capabilities(lhs.get() | rhs.get());
90}
91
92inline constexpr Capabilities operator&(const Capabilities& lhs,
93 const Capabilities& rhs) {
94 return Capabilities(lhs.get() & rhs.get());
95}
96
97inline constexpr Capabilities operator^(const Capabilities& lhs,
98 const Capabilities& rhs) {
99 return Capabilities(lhs.get() ^ rhs.get());
100}
101
102} // namespace pw::allocator
Definition: capability.h:62