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 <lib/support/CommonIterator.h>
23 : #include <lib/support/TypeTraits.h>
24 :
25 : namespace chip {
26 : namespace app {
27 : namespace Storage {
28 : namespace Data {
29 :
30 : // Storage index for entries in nvm
31 : typedef uint16_t EntryIndex;
32 : inline constexpr EntryIndex kUndefinedEntryIndex = 0xffff;
33 :
34 : /// @brief Struct combining both ID and data of a table entry
35 : template <class StorageId, class StorageData>
36 : struct TableEntry
37 : {
38 : // ID
39 : StorageId mStorageId;
40 :
41 : // DATA
42 : StorageData mStorageData;
43 :
44 6 : TableEntry() = default;
45 : TableEntry(StorageId id) : mStorageId(id) {}
46 15 : TableEntry(const StorageId id, const StorageData data) : mStorageId(id), mStorageData(data) {}
47 :
48 50 : bool operator==(const TableEntry & other) const
49 : {
50 50 : return (mStorageId == other.mStorageId && mStorageData == other.mStorageData);
51 : }
52 :
53 : void operator=(const TableEntry & other)
54 : {
55 : mStorageId = other.mStorageId;
56 : mStorageData = other.mStorageData;
57 : }
58 : };
59 :
60 : /// @brief Struct combining both ID and data of a table entry
61 : template <class StorageId, class StorageData>
62 : struct TableEntryRef
63 : {
64 : // ID
65 : StorageId & mStorageId;
66 :
67 : // DATA
68 : StorageData & mStorageData;
69 :
70 : TableEntryRef(StorageId & id, StorageData & data) : mStorageId(id), mStorageData(data) {}
71 : TableEntryRef(TableEntry<StorageId, StorageData> & entry) : mStorageId(entry.mStorageId), mStorageData(entry.mStorageData) {}
72 :
73 : bool operator==(const TableEntryRef & other) const
74 : {
75 : return (mStorageId == other.mStorageId && mStorageData == other.mStorageData);
76 : }
77 :
78 : void operator=(const TableEntryRef & other)
79 : {
80 : mStorageId = other.mStorageId;
81 : mStorageData = other.mStorageData;
82 : }
83 :
84 : void operator=(const TableEntry<StorageId, StorageData> & other)
85 : {
86 : mStorageId = other.mStorageId;
87 : mStorageData = other.mStorageData;
88 : }
89 : };
90 :
91 : /// @brief a CommonIterator which allows processing of just the StorageData
92 : template <class StorageId, class StorageData>
93 : class TableEntryDataConvertingIterator : public CommonIterator<StorageData>
94 : {
95 : public:
96 : TableEntryDataConvertingIterator(CommonIterator<TableEntryRef<StorageId, StorageData>> & iterator) : mIterator(iterator) {}
97 :
98 : size_t Count() override { return mIterator.Count(); }
99 :
100 : bool Next(StorageData & item) override
101 : {
102 : StorageId id;
103 : TableEntryRef ref(id, item);
104 : return mIterator.Next(ref);
105 : }
106 :
107 : void Release() override { return mIterator.Release(); }
108 :
109 : protected:
110 : CommonIterator<TableEntryRef<StorageId, StorageData>> & mIterator;
111 : };
112 : } // namespace Data
113 : } // namespace Storage
114 : } // namespace app
115 : } // namespace chip
|