Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : /**
19 : * @file
20 : * This is a list of attribute. Each entry is an attribute of type T.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <lib/core/CHIPError.h>
26 : #include <lib/support/CodeUtils.h>
27 :
28 : namespace chip {
29 : namespace DeviceLayer {
30 :
31 : template <typename T, size_t N>
32 : class AttributeList
33 : {
34 : public:
35 : /* The iterator */
36 : class Iterator
37 : {
38 : public:
39 : Iterator(const AttributeList<T, N> * AttributeList, int index);
40 : const T & operator*() const;
41 : Iterator & operator++();
42 : bool operator!=(const Iterator & other) const;
43 : bool operator==(const Iterator & other) const;
44 :
45 : private:
46 : const AttributeList<T, N> * mAttributeListPtr;
47 : int mIndex = -1;
48 : };
49 :
50 7 : AttributeList() = default;
51 7 : ~AttributeList() { mSize = 0; }
52 :
53 : CHIP_ERROR add(const T & label);
54 :
55 : size_t size() const;
56 : const T & operator[](int index) const;
57 :
58 : Iterator begin() const;
59 : Iterator end() const;
60 :
61 : private:
62 : T mList[N];
63 : int mSize = 0;
64 : };
65 :
66 : /*
67 : * AttributeList methods
68 : **/
69 : template <typename T, size_t N>
70 17 : inline CHIP_ERROR AttributeList<T, N>::add(const T & label)
71 : {
72 17 : if (mSize == N)
73 : {
74 1 : return CHIP_ERROR_NO_MEMORY;
75 : }
76 :
77 : // add the new element
78 16 : mList[mSize] = label;
79 16 : ++mSize;
80 16 : return CHIP_NO_ERROR;
81 : }
82 :
83 : template <typename T, size_t N>
84 9 : inline size_t AttributeList<T, N>::size() const
85 : {
86 9 : return static_cast<size_t>(mSize);
87 : }
88 :
89 : template <typename T, size_t N>
90 9 : inline const T & AttributeList<T, N>::operator[](int index) const
91 : {
92 9 : VerifyOrDie(index < mSize);
93 9 : return mList[index];
94 : }
95 :
96 : template <typename T, size_t N>
97 9 : inline typename AttributeList<T, N>::Iterator AttributeList<T, N>::begin() const
98 : {
99 9 : return AttributeList<T, N>::Iterator{ this, 0 };
100 : }
101 :
102 : template <typename T, size_t N>
103 6 : inline typename AttributeList<T, N>::Iterator AttributeList<T, N>::end() const
104 : {
105 6 : return AttributeList<T, N>::Iterator{ this, mSize };
106 : }
107 :
108 : /*
109 : * Iterator methods
110 : **/
111 : template <typename T, size_t N>
112 15 : inline AttributeList<T, N>::Iterator::Iterator(const AttributeList<T, N> * pAttributeList, int index) :
113 15 : mAttributeListPtr(pAttributeList), mIndex(index)
114 15 : {}
115 :
116 : template <typename T, size_t N>
117 6 : inline const T & AttributeList<T, N>::Iterator::operator*() const
118 : {
119 6 : return mAttributeListPtr->operator[](mIndex);
120 : }
121 :
122 : template <typename T, size_t N>
123 6 : inline typename AttributeList<T, N>::Iterator & AttributeList<T, N>::Iterator::operator++()
124 : {
125 6 : ++mIndex;
126 6 : return *this;
127 : }
128 :
129 : template <typename T, size_t N>
130 8 : inline bool AttributeList<T, N>::Iterator::operator!=(const AttributeList<T, N>::Iterator & other) const
131 : {
132 8 : return !(*this == other);
133 : }
134 :
135 : template <typename T, size_t N>
136 11 : inline bool AttributeList<T, N>::Iterator::operator==(const AttributeList<T, N>::Iterator & other) const
137 : {
138 11 : return mAttributeListPtr == other.mAttributeListPtr && mIndex == other.mIndex;
139 : }
140 :
141 : } // namespace DeviceLayer
142 : } // namespace chip
|