Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 :
17 : /**
18 : * This file contains common storage structures for FabricTableImpl
19 : */
20 : #pragma once
21 :
22 : #include <cstdint>
23 : #include <lib/support/CommonIterator.h>
24 : #include <lib/support/TypeTraits.h>
25 :
26 : namespace chip {
27 : namespace app {
28 : namespace Storage {
29 : namespace Data {
30 :
31 : // Storage index for entries in nvm
32 : typedef uint16_t EntryIndex;
33 : inline constexpr EntryIndex kUndefinedEntryIndex = 0xffff;
34 :
35 : /// @brief Struct combining both ID and data of a table entry
36 : template <class StorageId, class StorageData>
37 : struct TableEntry
38 : {
39 : // ID
40 : StorageId mStorageId;
41 :
42 : // DATA
43 : StorageData mStorageData;
44 :
45 6 : TableEntry() = default;
46 : TableEntry(StorageId id) : mStorageId(id) {}
47 15 : TableEntry(const StorageId id, const StorageData data) : mStorageId(id), mStorageData(data) {}
48 :
49 50 : bool operator==(const TableEntry & other) const
50 : {
51 50 : return (mStorageId == other.mStorageId && mStorageData == other.mStorageData);
52 : }
53 :
54 : void operator=(const TableEntry & other)
55 : {
56 : mStorageId = other.mStorageId;
57 : mStorageData = other.mStorageData;
58 : }
59 : };
60 :
61 : /// @brief Struct combining both ID and data of a table entry
62 : template <class StorageId, class StorageData>
63 : struct TableEntryRef
64 : {
65 : // ID
66 : StorageId & mStorageId;
67 :
68 : // DATA
69 : StorageData & mStorageData;
70 :
71 : TableEntryRef(StorageId & id, StorageData & data) : mStorageId(id), mStorageData(data) {}
72 : TableEntryRef(TableEntry<StorageId, StorageData> & entry) : mStorageId(entry.mStorageId), mStorageData(entry.mStorageData) {}
73 :
74 : bool operator==(const TableEntryRef & other) const
75 : {
76 : return (mStorageId == other.mStorageId && mStorageData == other.mStorageData);
77 : }
78 :
79 : void operator=(const TableEntryRef & other)
80 : {
81 : mStorageId = other.mStorageId;
82 : mStorageData = other.mStorageData;
83 : }
84 :
85 : void operator=(const TableEntry<StorageId, StorageData> & other)
86 : {
87 : mStorageId = other.mStorageId;
88 : mStorageData = other.mStorageData;
89 : }
90 : };
91 :
92 : /// @brief a CommonIterator which allows processing of just the StorageData
93 : template <class StorageId, class StorageData>
94 : class TableEntryDataConvertingIterator : public CommonIterator<StorageData>
95 : {
96 : public:
97 : TableEntryDataConvertingIterator(CommonIterator<TableEntryRef<StorageId, StorageData>> & iterator) : mIterator(iterator) {}
98 :
99 : size_t Count() override { return mIterator.Count(); }
100 :
101 : bool Next(StorageData & item) override
102 : {
103 : StorageId id;
104 : TableEntryRef ref(id, item);
105 : return mIterator.Next(ref);
106 : }
107 :
108 : void Release() override { return mIterator.Release(); }
109 :
110 : protected:
111 : CommonIterator<TableEntryRef<StorageId, StorageData>> & mIterator;
112 : };
113 : } // namespace Data
114 : } // namespace Storage
115 : } // namespace app
116 : } // namespace chip
|