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 :
44 : private:
45 : const AttributeList<T, N> * mAttributeListPtr;
46 : int mIndex = -1;
47 : };
48 :
49 : AttributeList() = default;
50 : ~AttributeList() { mSize = 0; }
51 :
52 : CHIP_ERROR add(const T & label);
53 :
54 : size_t size() const;
55 : const T & operator[](int index) const;
56 :
57 : Iterator begin() const;
58 : Iterator end() const;
59 :
60 : private:
61 : T mList[N];
62 : int mSize = 0;
63 : };
64 :
65 : /*
66 : * AttributeList methods
67 : **/
68 : template <typename T, size_t N>
69 : inline CHIP_ERROR AttributeList<T, N>::add(const T & label)
70 : {
71 : if (mSize == N)
72 : {
73 : return CHIP_ERROR_NO_MEMORY;
74 : }
75 :
76 : // add the new element
77 : mList[mSize] = label;
78 : ++mSize;
79 : return CHIP_NO_ERROR;
80 : }
81 :
82 : template <typename T, size_t N>
83 0 : inline size_t AttributeList<T, N>::size() const
84 : {
85 0 : return static_cast<size_t>(mSize);
86 : }
87 :
88 : template <typename T, size_t N>
89 0 : inline const T & AttributeList<T, N>::operator[](int index) const
90 : {
91 0 : VerifyOrDie(index < mSize);
92 0 : return mList[index];
93 : }
94 :
95 : template <typename T, size_t N>
96 0 : inline typename AttributeList<T, N>::Iterator AttributeList<T, N>::begin() const
97 : {
98 0 : return AttributeList<T, N>::Iterator{ this, 0 };
99 : }
100 :
101 : template <typename T, size_t N>
102 0 : inline typename AttributeList<T, N>::Iterator AttributeList<T, N>::end() const
103 : {
104 0 : return AttributeList<T, N>::Iterator{ this, mSize };
105 : }
106 :
107 : /*
108 : * Iterator methods
109 : **/
110 : template <typename T, size_t N>
111 0 : inline AttributeList<T, N>::Iterator::Iterator(const AttributeList<T, N> * pAttributeList, int index) :
112 0 : mAttributeListPtr(pAttributeList), mIndex(index)
113 0 : {}
114 :
115 : template <typename T, size_t N>
116 0 : inline const T & AttributeList<T, N>::Iterator::operator*() const
117 : {
118 0 : return mAttributeListPtr->operator[](mIndex);
119 : }
120 :
121 : template <typename T, size_t N>
122 0 : inline typename AttributeList<T, N>::Iterator & AttributeList<T, N>::Iterator::operator++()
123 : {
124 0 : ++mIndex;
125 0 : return *this;
126 : }
127 :
128 : template <typename T, size_t N>
129 0 : inline bool AttributeList<T, N>::Iterator::operator!=(const AttributeList<T, N>::Iterator & other) const
130 : {
131 0 : return mIndex != other.mIndex;
132 : }
133 :
134 : } // namespace DeviceLayer
135 : } // namespace chip
|