Skip to content

Commit

Permalink
Use name prefix/type name to shorten enum/bitmask constant names
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Sep 4, 2023
1 parent 4673dc2 commit 3b03007
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 9 deletions.
26 changes: 24 additions & 2 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -28317,6 +28317,10 @@ int meta_parse_constants(

const char *ptr = desc;
const char *name = ecs_get_name(world, t);
int32_t name_len = ecs_os_strlen(name);
const ecs_world_info_t *info = ecs_get_world_info(world);
const char *name_prefix = info->name_prefix;
int32_t name_prefix_len = name_prefix ? ecs_os_strlen(name_prefix) : 0;

meta_parse_ctx_t ctx = {
.name = name,
Expand All @@ -28337,6 +28341,18 @@ int meta_parse_constants(
goto error;
}

if (name_prefix) {
if (!ecs_os_strncmp(token.name, name_prefix, name_prefix_len)) {
ecs_os_memmove(token.name, token.name + name_prefix_len,
ecs_os_strlen(token.name) - name_prefix_len + 1);
}
}

if (!ecs_os_strncmp(token.name, name, name_len)) {
ecs_os_memmove(token.name, token.name + name_len,
ecs_os_strlen(token.name) - name_len + 1);
}

ecs_entity_t c = ecs_entity(world, {
.name = token.name
});
Expand Down Expand Up @@ -53055,6 +53071,9 @@ int json_typeinfo_ser_type_ops(
ecs_strbuf_t *str,
const EcsStruct *st)
{
const EcsStruct *stack[64] = {st};
int32_t sp = 1;

for (int i = 0; i < op_count; i ++) {
ecs_meta_type_op_t *op = &ops[i];

Expand All @@ -53072,13 +53091,16 @@ int json_typeinfo_ser_type_ops(
i += op->op_count - 1;
continue;
}

switch(op->kind) {
case EcsOpPush:
flecs_json_object_push(str);
ecs_assert(sp < 63, ECS_INVALID_OPERATION, "type nesting too deep");
stack[sp ++] = ecs_get(world, op->type, EcsStruct);
break;
case EcsOpPop:
flecs_json_object_pop(str);
sp --;
break;
case EcsOpArray:
case EcsOpVector:
Expand All @@ -53102,7 +53124,7 @@ int json_typeinfo_ser_type_ops(
case EcsOpEntity:
case EcsOpString:
case EcsOpOpaque:
if (json_typeinfo_ser_type_op(world, op, str, st)) {
if (json_typeinfo_ser_type_op(world, op, str, stack[sp - 1])) {
goto error;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5512,7 +5512,7 @@ FLECS_API
void* ecs_get_mut_modified_id(
ecs_world_t *world,
ecs_entity_t entity,
ecs_id_t id);
ecs_id_t id);

/** Begin exclusive write access to entity.
* This operation provides safe exclusive access to the components of an entity
Expand Down
16 changes: 16 additions & 0 deletions src/addons/meta_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ int meta_parse_constants(

const char *ptr = desc;
const char *name = ecs_get_name(world, t);
int32_t name_len = ecs_os_strlen(name);
const ecs_world_info_t *info = ecs_get_world_info(world);
const char *name_prefix = info->name_prefix;
int32_t name_prefix_len = name_prefix ? ecs_os_strlen(name_prefix) : 0;

meta_parse_ctx_t ctx = {
.name = name,
Expand All @@ -749,6 +753,18 @@ int meta_parse_constants(
goto error;
}

if (name_prefix) {
if (!ecs_os_strncmp(token.name, name_prefix, name_prefix_len)) {
ecs_os_memmove(token.name, token.name + name_prefix_len,
ecs_os_strlen(token.name) - name_prefix_len + 1);
}
}

if (!ecs_os_strncmp(token.name, name, name_len)) {
ecs_os_memmove(token.name, token.name + name_len,
ecs_os_strlen(token.name) - name_len + 1);
}

ecs_entity_t c = ecs_entity(world, {
.name = token.name
});
Expand Down
5 changes: 4 additions & 1 deletion test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,10 @@
"struct_nospace",
"identifier_w_underscore",
"struct_w_ptr",
"private_members"
"private_members",
"enum_constant_w_name_prefix",
"enum_constant_w_type_prefix",
"enum_constant_w_name_type_prefix"
]
}, {
"id": "Vars",
Expand Down
12 changes: 12 additions & 0 deletions test/meta/src/BitmaskTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,15 @@ void BitmaskTypes_bitmask_w_short_notation(void) {

ecs_fini(world);
}

void BitmaskTypes_constant_w_name_prefix(void) {
// Implement testcase
}

void BitmaskTypes_constant_w_type_prefix(void) {
// Implement testcase
}

void BitmaskTypes_constant_w_name_type_prefix(void) {
// Implement testcase
}
12 changes: 12 additions & 0 deletions test/meta/src/EnumTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,15 @@ void EnumTypes_enum_w_short_notation(void) {

ecs_fini(world);
}

void EnumTypes_constant_w_name_prefix(void) {
// Implement testcase
}

void EnumTypes_constant_w_type_prefix(void) {
// Implement testcase
}

void EnumTypes_constant_w_name_type_prefix(void) {
// Implement testcase
}
112 changes: 108 additions & 4 deletions test/meta/src/MetaUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,27 @@ ECS_BITMASK(Bitmask, {
Bacon = 1, Lettuce = 2, Tomato = 4, Blt = 7
});

ECS_ENUM(Enum_Nospace, {Enum_Nospace_A,Enum_Nospace_B,Enum_Nospace_C});
ECS_ENUM(Enum_Nospace, {EnumNospace_A,EnumNospace_B,EnumNospace_C});
ECS_STRUCT(Struct_Nospace, {float x;float y;});

ECS_ENUM(TestNamePrefix, {
TestA,
TestB,
TestC
});

ECS_ENUM(TestTypePrefix, {
TestTypePrefixA,
TestTypePrefixB,
TestTypePrefixC
});

ECS_ENUM(TestNameTypePrefix, {
TestNameTypePrefixA,
TestNameTypePrefixB,
TestNameTypePrefixC
});

ECS_STRUCT(Struct_3_enum, {
Enum_Default one;
Enum_Default two;
Expand Down Expand Up @@ -332,13 +350,13 @@ void MetaUtils_enum_nospace(void) {
test_str(ecs_get_name(world, e), "Enum_Nospace");
test_assert(ecs_has(world, e, EcsEnum));

ecs_entity_t c_a = ecs_lookup_child(world, e, "Enum_Nospace_A");
ecs_entity_t c_a = ecs_lookup_child(world, e, "EnumNospace_A");
test_assert(c_a != 0);

ecs_entity_t c_b = ecs_lookup_child(world, e, "Enum_Nospace_B");
ecs_entity_t c_b = ecs_lookup_child(world, e, "EnumNospace_B");
test_assert(c_b != 0);

ecs_entity_t c_c = ecs_lookup_child(world, e, "Enum_Nospace_C");
ecs_entity_t c_c = ecs_lookup_child(world, e, "EnumNospace_C");
test_assert(c_c != 0);

ecs_fini(world);
Expand Down Expand Up @@ -438,3 +456,89 @@ void MetaUtils_private_members(void) {

ecs_fini(world);
}

void MetaUtils_enum_constant_w_name_prefix(void) {
ecs_world_t *world = ecs_init();

ecs_set_name_prefix(world, "Test");

ECS_META_COMPONENT(world, TestNamePrefix);

test_assert(ecs_id(TestNamePrefix) != 0);
ecs_entity_t e = ecs_id(TestNamePrefix);

test_str(ecs_get_name(world, e), "NamePrefix");

{
ecs_entity_t c_a = ecs_lookup_child(world, e, "A");
test_assert(c_a != 0);
}

{
ecs_entity_t c_b = ecs_lookup_child(world, e, "B");
test_assert(c_b != 0);
}

{
ecs_entity_t c_c = ecs_lookup_child(world, e, "C");
test_assert(c_c != 0);
}

ecs_fini(world);
}

void MetaUtils_enum_constant_w_type_prefix(void) {
ecs_world_t *world = ecs_init();

ECS_META_COMPONENT(world, TestTypePrefix);

test_assert(ecs_id(TestTypePrefix) != 0);
ecs_entity_t e = ecs_id(TestTypePrefix);

{
ecs_entity_t c_a = ecs_lookup_child(world, e, "A");
test_assert(c_a != 0);
}

{
ecs_entity_t c_b = ecs_lookup_child(world, e, "B");
test_assert(c_b != 0);
}

{
ecs_entity_t c_c = ecs_lookup_child(world, e, "C");
test_assert(c_c != 0);
}

ecs_fini(world);
}

void MetaUtils_enum_constant_w_name_type_prefix(void) {
ecs_world_t *world = ecs_init();

ecs_set_name_prefix(world, "Test");

ECS_META_COMPONENT(world, TestNameTypePrefix);

test_assert(ecs_id(TestNameTypePrefix) != 0);
ecs_entity_t e = ecs_id(TestNameTypePrefix);

test_str(ecs_get_name(world, e), "NameTypePrefix");

{
ecs_entity_t c_a = ecs_lookup_child(world, e, "A");
test_assert(c_a != 0);
}

{
ecs_entity_t c_b = ecs_lookup_child(world, e, "B");
test_assert(c_b != 0);
}

{
ecs_entity_t c_c = ecs_lookup_child(world, e, "C");
test_assert(c_c != 0);
}

ecs_fini(world);
}
17 changes: 16 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,9 @@ void MetaUtils_struct_nospace(void);
void MetaUtils_identifier_w_underscore(void);
void MetaUtils_struct_w_ptr(void);
void MetaUtils_private_members(void);
void MetaUtils_enum_constant_w_name_prefix(void);
void MetaUtils_enum_constant_w_type_prefix(void);
void MetaUtils_enum_constant_w_name_type_prefix(void);

// Testsuite 'Vars'
void Vars_declare_1_var(void);
Expand Down Expand Up @@ -4128,6 +4131,18 @@ bake_test_case MetaUtils_testcases[] = {
{
"private_members",
MetaUtils_private_members
},
{
"enum_constant_w_name_prefix",
MetaUtils_enum_constant_w_name_prefix
},
{
"enum_constant_w_type_prefix",
MetaUtils_enum_constant_w_type_prefix
},
{
"enum_constant_w_name_type_prefix",
MetaUtils_enum_constant_w_name_type_prefix
}
};

Expand Down Expand Up @@ -4892,7 +4907,7 @@ static bake_test_suite suites[] = {
"MetaUtils",
NULL,
NULL,
19,
22,
MetaUtils_testcases
},
{
Expand Down

0 comments on commit 3b03007

Please sign in to comment.