Skip to content

Commit

Permalink
Add C++ codegen support for global structs/enums/bitmaps. (#34562)
Browse files Browse the repository at this point in the history
Fixes app-common and chip-tool.
  • Loading branch information
bzbarsky-apple authored Jul 29, 2024
1 parent 5e3127f commit 5ab8b3e
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 28 deletions.
2 changes: 2 additions & 0 deletions examples/chip-tool/templates/ComplexArgumentParser-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> struct_parser_impl namespace="detail"}}
{{else if has_no_clusters}}
{{> struct_parser_impl namespace="Globals"}}
{{/if}}
{{/zcl_structs}}

Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/templates/ComplexArgumentParser.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> struct_parser_decl namespace="detail"}}
{{else if has_no_clusters}}
{{> struct_parser_decl namespace="Globals"}}
{{/if}}
{{/zcl_structs}}

Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/templates/logging/DataModelLogger-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ using namespace chip::app::Clusters;
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> struct_logger_impl namespace="detail"}}
{{else if has_no_clusters}}
{{> struct_logger_impl namespace="Globals"}}
{{/if}}
{{/zcl_structs}}

Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/templates/logging/DataModelLogger.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> struct_logger_decl namespace="detail"}}
{{else if has_no_clusters}}
{{> struct_logger_decl namespace="Globals"}}
{{/if}}
{{/zcl_structs}}

Expand Down
4 changes: 4 additions & 0 deletions src/app/common/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"name": "cluster_enums_enum",
"path": "../../zap-templates/partials/cluster-enums-enum.zapt"
},
{
"name": "cluster_enums_ensure_known_value",
"path": "../../zap-templates/partials/cluster-enums-ensure-known-value.zapt"
},
{
"name": "cluster_objects_field_init",
"path": "../../zap-templates/partials/cluster-objects-field-init.zapt"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{#unless (isInConfigList (concat ns "::" label) "EnumsNotUsedAsTypeInXML")}}
static auto __attribute__((unused)) EnsureKnownEnumValue({{ns}}::{{asType label}} val)
{
using EnumType = {{ns}}::{{asType label}};
switch (val) {
{{#zcl_enum_items}}
case EnumType::k{{asUpperCamelCase label}}:
{{/zcl_enum_items}}
return val;
default:
return EnumType::kUnknownEnumValue;
}
}
{{/unless}}
33 changes: 5 additions & 28 deletions src/app/zap-templates/templates/app/cluster-enums-check.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,17 @@ namespace app {
namespace Clusters {
{{#zcl_enums}}
{{#if has_more_than_one_cluster}}
{{#unless (isInConfigList (concat "::" label) "EnumsNotUsedAsTypeInXML")}}
static auto __attribute__((unused)) EnsureKnownEnumValue(detail::{{asType label}} val)
{
using EnumType = detail::{{asType label}};
switch (val) {
{{#zcl_enum_items}}
case EnumType::k{{asUpperCamelCase label}}:
{{/zcl_enum_items}}
return val;
default:
return EnumType::kUnknownEnumValue;
}
}
{{/unless}}
{{> cluster_enums_ensure_known_value ns="detail"}}
{{else if has_no_clusters}}
{{> cluster_enums_ensure_known_value ns="Globals"}}

{{/if}}
{{/zcl_enums}}

{{#zcl_clusters}}
{{#zcl_enums}}
{{#unless has_more_than_one_cluster}}
{{#unless (isInConfigList (concat (asUpperCamelCase ../name) "::" label) "EnumsNotUsedAsTypeInXML")}}
static auto __attribute__((unused)) EnsureKnownEnumValue({{asUpperCamelCase ../name}}::{{asType label}} val)
{
using EnumType = {{asUpperCamelCase ../name}}::{{asType label}};
switch (val) {
{{#zcl_enum_items}}
case EnumType::k{{asUpperCamelCase label}}:
{{/zcl_enum_items}}
return val;
default:
return EnumType::kUnknownEnumValue;
}
}
{{/unless}}
{{> cluster_enums_ensure_known_value ns=(asUpperCamelCase ../name)}}
{{/unless}}
{{/zcl_enums}}

Expand Down
31 changes: 31 additions & 0 deletions src/app/zap-templates/templates/app/cluster-enums.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ k{{asUpperCamelCase label}} = {{asHex mask}},

} // namespace detail

namespace Globals {
// Global enums.
{{#zcl_enums}}

{{#if has_no_clusters}}

{{> cluster_enums_enum ns=""}}

{{/if}}
{{/zcl_enums}}

// Global bitmaps.
{{#zcl_bitmaps}}

{{#if has_no_clusters}}
{{! Work around https://github.com/project-chip/zap/issues/1370 and manually filter out built-in bitmap types. }}
{{#if_is_atomic label}}
{{else}}

// Bitmap for {{label}}
enum class {{asType label}} : {{asUnderlyingZclType name}} {
{{#zcl_bitmap_items}}
k{{asUpperCamelCase label}} = {{asHex mask}},
{{/zcl_bitmap_items}}
};

{{/if_is_atomic}}
{{/if}}
{{/zcl_bitmaps}}

} // namespace Globals

{{#zcl_clusters}}
namespace {{asUpperCamelCase name}} {
Expand Down
12 changes: 12 additions & 0 deletions src/app/zap-templates/templates/app/cluster-objects-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ namespace Structs {
} // namespace Structs
} // namespace detail

namespace Globals {
// Global structs
namespace Structs {
{{#zcl_structs}}
{{#if has_no_clusters}}
{{> cluster_objects_struct header=false}}

{{/if}}
{{/zcl_structs}}
} // namespace Structs
} // namespace Globals

{{#zcl_clusters}}
namespace {{asUpperCamelCase name}} {
{{#zcl_structs}}
Expand Down
12 changes: 12 additions & 0 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ namespace Structs {
} // namespace detail

namespace Globals {

// Global structs.
namespace Structs {

{{#zcl_structs}}
{{#if has_no_clusters}}
{{> cluster_objects_struct header=true}}

{{/if}}
{{/zcl_structs}}
} // namespace Structs

namespace Attributes {
{{#zcl_attributes_server}}
{{#unless clusterRef}}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions zzz_generated/app-common/app-common/zap-generated/cluster-enums.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ab8b3e

Please sign in to comment.