Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to get "define" for enum type #231

Closed
yunhanw-google opened this issue Sep 20, 2021 · 11 comments
Closed

Need to get "define" for enum type #231

yunhanw-google opened this issue Sep 20, 2021 · 11 comments

Comments

@yunhanw-google
Copy link
Contributor

yunhanw-google commented Sep 20, 2021

<struct name="SimpleStruct">
        <cluster code="0x050F"/>
        <item name="a" type="UINT8U" optional="false"/>
        <item name="b" type="BOOLEAN" optional="false"/>
        <item name="c" define="SimpleEnum" type="ENUM8" optional="false"/>
DROP TABLE IF EXISTS "STRUCT_ITEM";
CREATE TABLE IF NOT EXISTS "STRUCT_ITEM" (
  "STRUCT_REF" integer,
  "FIELD_IDENTIFIER" integer,
  "NAME" text,
  "TYPE" text,
  "IS_ARRAY" integer,
  "MIN_LENGTH" integer,
  "MAX_LENGTH" integer,
  "IS_WRITABLE" integer,
  foreign key (STRUCT_REF) references STRUCT(STRUCT_ID)
);

looks like currently zap don't support struct item with define ?

for enum type, we would like to get "define"

then I can write {{asType label}} = static_cast<{{define}}>(v);

@yunhanw-google yunhanw-google changed the title Need to get "define" for enum type, Need to get "define" for enum type Sep 20, 2021
@brdandu
Copy link
Collaborator

brdandu commented Sep 21, 2021

<struct name="SimpleStruct">
        <cluster code="0x050F"/>
        <item name="a" type="UINT8U" optional="false"/>
        <item name="b" type="BOOLEAN" optional="false"/>
        <item name="c" define="SimpleEnum" type="ENUM8" optional="false"/>
DROP TABLE IF EXISTS "STRUCT_ITEM";
CREATE TABLE IF NOT EXISTS "STRUCT_ITEM" (
  "STRUCT_REF" integer,
  "FIELD_IDENTIFIER" integer,
  "NAME" text,
  "TYPE" text,
  "IS_ARRAY" integer,
  "MIN_LENGTH" integer,
  "MAX_LENGTH" integer,
  "IS_WRITABLE" integer,
  foreign key (STRUCT_REF) references STRUCT(STRUCT_ID)
);

looks like currently zap don't support struct item with define ?

for enum type, we would like to get "define"

then I can write {{asType label}} = static_cast<{{define}}>(v);

An alternate way to do this with the existing version can be {{asType label}} = static_cast<{{as_delimited_macro name}}>(v);

@yunhanw-google
Copy link
Contributor Author

it does not work as expected if using {{asType label}} = static_cast<{{as_delimited_macro name}}>(v), which returns c = static_cast(v);

@brdandu
Copy link
Collaborator

brdandu commented Sep 21, 2021

Could you give me the example template on how you are using this?

@tecimovic
Copy link
Collaborator

I'm a bit confused with this, @yunhanw-google :
why would we not simply do:

<item name="c"  type="SimpleEnum" optional="false"/>

instead of:

<item name="c" define="SimpleEnum" type="ENUM8" optional="false"/>

@yunhanw-google
Copy link
Contributor Author

If I define this way,

how can I check SimpleEnum is enum type?

{{#if (isEnum type) }}
uint8_t v;
ReturnErrorOnFailure(ClusterObjectUtils::Decode(reader, v));
//zap issue 231 to return right enum type
{{asType label}} = static_cast<{{asUnderlyingZclType type}}>(v);
break;

i need to write this kind of code
check if it is enum type, then generate some code

@yunhanw-google
Copy link
Contributor Author

Could you give me the example template on how you are using this?

        {{#zcl_struct_items}}
        case k{{asType label}}FieldId:
            {{#if (isEnum type) }}
            uint8_t v;
            ReturnErrorOnFailure(ClusterObjectUtils::Decode(reader, v));
            //zap issue 231 to return right enum type
            {{asType label}} = static_cast<{{define}}>(v);
            break;
            {{else if isArray}}
            return CHIP_ERROR_NOT_IMPLEMENTED;
            {{else}}
            ReturnErrorOnFailure(ClusterObjectUtils::Decode(reader, {{asType label}}));
            break;
            {{/if}}
        {{/zcl_struct_items}}

@yunhanw-google
Copy link
Contributor Author

yunhanw-google commented Sep 21, 2021

another alternative is that, we add one additional flag, isEnum similar to isArray for struct item(#229)

@brdandu
Copy link
Collaborator

brdandu commented Sep 22, 2021

There is this helper which may be helpful.
{{#if_is_enum type}}
type is enum
{{else}}
type is not enum
{{/if_is_enum}}

Regarding "static_cast<{{as_delimited_macro name}}>(v), which returns c = static_cast(v);" I am not sure why this happened. Is this for a particular struct item or all of them?

@yunhanw-google
Copy link
Contributor Author

There is this helper which may be helpful.
{{#if_is_enum type}}
type is enum
{{else}}
type is not enum
{{/if_is_enum}}

Just tried, it cannot detect SimpleEnum is enum.
The issue here is that the internal zap don't have knowledge about the customized enum type currently. Either zap know how to parse define, or parse isEnum or some other way zap get knowledge from xml to know it is enum

@tecimovic
Copy link
Collaborator

So if I do this, does this meet your meeds:
1.) Add the IS_ENUM field in the DB.
2.) Allow for isEnum="true" in the XML.
3.) load and populate these flags.
4.) Give you a helper is_enum_type or something like that that will allow to modify how these types are generated.

@tecimovic tecimovic mentioned this issue Sep 22, 2021
@tecimovic
Copy link
Collaborator

Closed in PR 235

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants