Skip to content

Commit 1222926

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Use List<const Whatever> for encodable lists. (#11006)
We should not be requiring creation of non-const data to encode a list if it can be backed by const data instead. Another option would be to make List<T> inherit from Span<const T>, but that makes some assumptions about whether people will want to write to a buffer via its List representation. The change to the List::operator= is because importing _all_ operator= from the super-class, including the implicitly-defined ones, causes assignments from List<T> to List<const T> to be ambiguous. Forwarding just the one operator= we want to forward fixes that.
1 parent 79a4b27 commit 1222926

File tree

3 files changed

+106
-92
lines changed

3 files changed

+106
-92
lines changed

src/app/data-model/List.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ struct List : public Span<T>
4848
// List<T> instances as though they were just Spans.
4949
//
5050
using Span<T>::Span;
51-
using Span<T>::operator=;
51+
52+
template <size_t N>
53+
constexpr List & operator=(T (&databuf)[N])
54+
{
55+
Span<T>::operator=(databuf);
56+
return (*this);
57+
}
5258
};
5359

5460
template <typename X>

src/app/zap-templates/templates/app/helper.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,19 @@ async function zapTypeToClusterObjectType(type, isDecodable, options)
375375
let promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this));
376376
if ((this.isList || this.isArray || this.entryType) && !options.hash.forceNotList) {
377377
passByReference = true;
378-
let listType = isDecodable ? "DecodableList" : "List";
379378
// If we did not have a namespace provided, we can assume we're inside
380379
// chip::app.
381380
let listNamespace = options.hash.ns ? "chip::app::" : ""
382-
promise = promise.then(typeStr => `${listNamespace}DataModel::${listType}<${typeStr}>`);
381+
if (isDecodable)
382+
{
383+
promise = promise.then(typeStr => `${listNamespace}DataModel::DecodableList<${typeStr}>`);
384+
}
385+
else
386+
{
387+
// Use const ${typeStr} so that consumers don't have to create non-const
388+
// data to encode.
389+
promise = promise.then(typeStr => `${listNamespace}DataModel::List<const ${typeStr}>`);
390+
}
383391
}
384392
if (this.isNullable && !options.hash.forceNotNullable) {
385393
passByReference = true;

0 commit comments

Comments
 (0)