Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : * All rights reserved.
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 : #include <app/server-cluster/AttributeListBuilder.h>
18 :
19 : #include <app/server-cluster/DefaultServerCluster.h>
20 :
21 : namespace chip {
22 : namespace app {
23 :
24 11 : CHIP_ERROR AttributeListBuilder::Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
25 : Span<const OptionalAttributeEntry> optionalAttributes)
26 : {
27 : // determine how much data to append. This should only be called if generally we have something to append
28 11 : size_t append_size = mandatoryAttributes.size();
29 38 : for (const auto & entry : optionalAttributes)
30 : {
31 27 : if (entry.enabled)
32 : {
33 13 : append_size++;
34 : }
35 : }
36 :
37 11 : if (append_size > 0)
38 : {
39 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
40 : // exists in the builder. This is why we ensure AppendCapacity for everything
41 : // so that we do not perform extra allocations.
42 9 : ReturnErrorOnFailure(mBuilder.EnsureAppendCapacity(append_size + DefaultServerCluster::GlobalAttributes().size()));
43 9 : ReturnErrorOnFailure(mBuilder.ReferenceExisting(mandatoryAttributes));
44 :
45 32 : for (const auto & entry : optionalAttributes)
46 : {
47 23 : if (entry.enabled)
48 : {
49 13 : ReturnErrorOnFailure(mBuilder.Append(entry.metadata));
50 : }
51 : }
52 : }
53 :
54 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
55 : // exists in the builder.
56 11 : return mBuilder.ReferenceExisting(DefaultServerCluster::GlobalAttributes());
57 : }
58 :
59 : } // namespace app
60 : } // namespace chip
|