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 16 : CHIP_ERROR AttributeListBuilder::Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
25 : Span<const DataModel::AttributeEntry> optionalAttributes,
26 : AttributeSet enabledOptionalAttributes)
27 : {
28 : // determine how much data to append. This should only be called if generally we have something to append
29 16 : size_t append_size = mandatoryAttributes.size();
30 96 : for (const auto & entry : optionalAttributes)
31 : {
32 80 : if (enabledOptionalAttributes.IsSet(entry.attributeId))
33 : {
34 35 : append_size++;
35 : }
36 : }
37 :
38 16 : if (append_size > 0)
39 : {
40 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
41 : // exists in the builder. This is why we ensure AppendCapacity for everything
42 : // so that we do not perform extra allocations.
43 14 : ReturnErrorOnFailure(mBuilder.EnsureAppendCapacity(append_size + DefaultServerCluster::GlobalAttributes().size()));
44 14 : ReturnErrorOnFailure(mBuilder.ReferenceExisting(mandatoryAttributes));
45 :
46 90 : for (const auto & entry : optionalAttributes)
47 : {
48 76 : if (enabledOptionalAttributes.IsSet(entry.attributeId))
49 : {
50 35 : ReturnErrorOnFailure(mBuilder.Append(entry));
51 : }
52 : }
53 : }
54 :
55 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
56 : // exists in the builder.
57 16 : return mBuilder.ReferenceExisting(DefaultServerCluster::GlobalAttributes());
58 : }
59 :
60 9 : CHIP_ERROR AttributeListBuilder::Append(Span<const DataModel::AttributeEntry> mandatoryAttributes,
61 : Span<const OptionalAttributeEntry> optionalAttributes)
62 : {
63 : // determine how much data to append. This should only be called if generally we have something to append
64 9 : size_t append_size = mandatoryAttributes.size();
65 14 : for (const auto & entry : optionalAttributes)
66 : {
67 5 : if (entry.enabled)
68 : {
69 3 : append_size++;
70 : }
71 : }
72 :
73 9 : if (append_size > 0)
74 : {
75 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
76 : // exists in the builder. This is why we ensure AppendCapacity for everything
77 : // so that we do not perform extra allocations.
78 8 : ReturnErrorOnFailure(mBuilder.EnsureAppendCapacity(append_size + DefaultServerCluster::GlobalAttributes().size()));
79 8 : ReturnErrorOnFailure(mBuilder.ReferenceExisting(mandatoryAttributes));
80 :
81 13 : for (const auto & entry : optionalAttributes)
82 : {
83 5 : if (entry.enabled)
84 : {
85 3 : ReturnErrorOnFailure(mBuilder.Append(entry.metadata));
86 : }
87 : }
88 : }
89 :
90 : // NOTE: ReferenceExisting will APPEND data (and use heap) when some data already
91 : // exists in the builder.
92 9 : return mBuilder.ReferenceExisting(DefaultServerCluster::GlobalAttributes());
93 : }
94 : } // namespace app
95 : } // namespace chip
|