Skip to content

Commit 7986db4

Browse files
committed
Refs #23128: Review - Add default_literal annotation implementation and tests
Signed-off-by: Carlosespicur <carlosespicur@proton.me>
1 parent fa7af26 commit 7986db4

File tree

10 files changed

+183
-3
lines changed

10 files changed

+183
-3
lines changed

include/fastdds/dds/xtypes/dynamic_types/MemberDescriptor.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ class FASTDDS_EXPORTED_API MemberDescriptor
277277
virtual void is_default_label(
278278
bool is_default_label) = 0;
279279

280+
/*!
281+
* Returns if the member is a default literal.
282+
* @return If the member is a default literal
283+
*/
284+
virtual bool is_default_literal() const = 0;
285+
286+
/*!
287+
* Returns if the member is a default literal.
288+
* @return If the member is a default literal
289+
*/
290+
virtual bool& is_default_literal() = 0;
291+
292+
/*!
293+
* Modifies if the member is a default literal.
294+
* @param [in] is_default_literal Boolean
295+
*/
296+
virtual void is_default_literal(
297+
bool is_default_literal) = 0;
298+
280299
/*!
281300
* Overwrites the contents of this descriptor with those of another descriptor (see [standard] 7.5.2.7.1).
282301
* @param [in] descriptor reference.

src/cpp/fastdds/xtypes/dynamic_types/DynamicTypeBuilderImpl.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ ReturnCode_t DynamicTypeBuilderImpl::add_member(
287287

288288
RollbackSetting<uint32_t> id_reverter{next_id_}, index_reverter{next_index_};
289289
RollbackSetting<int32_t> default_value_reverter{default_value_};
290+
RollbackSetting<ObjectName> default_literal_reverter{default_literal_};
290291

291292
if (TK_ANNOTATION != type_descriptor_kind &&
292293
TK_BITMASK != type_descriptor_kind &&
@@ -525,6 +526,26 @@ ReturnCode_t DynamicTypeBuilderImpl::add_member(
525526
}
526527
}
527528

529+
if (descriptor_impl->is_default_literal())
530+
{
531+
for (auto member : members_)
532+
{
533+
const auto member_impl {traits<DynamicTypeMember>::narrow<DynamicTypeMemberImpl>(member)};
534+
535+
// Check that there isn't already any member marked as default.
536+
if (member_impl->get_descriptor().is_default_literal())
537+
{
538+
EPROSIMA_LOG_ERROR(DYN_TYPES,
539+
"Member " << member_impl->member_descriptor_.name().c_str() <<
540+
" has already been annotated as default");
541+
return RETCODE_BAD_PARAMETER;
542+
}
543+
}
544+
545+
default_literal_ = member_name;
546+
default_literal_reverter.activate = true;
547+
}
548+
528549
if (!descriptor->default_value().empty())
529550
{
530551
for (auto member : members_)
@@ -602,6 +623,7 @@ ReturnCode_t DynamicTypeBuilderImpl::add_member(
602623
id_reverter.activate = false;
603624
index_reverter.activate = false;
604625
default_value_reverter.activate = false;
626+
default_literal_reverter.activate = false;
605627
return RETCODE_OK;
606628
}
607629

@@ -703,6 +725,7 @@ traits<DynamicType>::ref_type DynamicTypeBuilderImpl::build() noexcept
703725
ret_val->members_ = members_;
704726
ret_val->default_value_ = default_value_;
705727
ret_val->default_union_member_ = default_union_member_;
728+
ret_val->default_literal_ = default_literal_;
706729
ret_val->index_own_members_ = index_own_members_;
707730
}
708731
}

src/cpp/fastdds/xtypes/dynamic_types/DynamicTypeBuilderImpl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ class DynamicTypeBuilderImpl : public traits<DynamicTypeBuilder>::base_type
128128
//! Points to the default union member.
129129
MemberId default_union_member_ {MEMBER_ID_INVALID};
130130

131+
//! Name of the enumeration's default literal.
132+
ObjectName default_literal_;
133+
131134
//! Index pointing the first own member, not inherited from a base_type.
132135
uint32_t index_own_members_ {0};
133136

src/cpp/fastdds/xtypes/dynamic_types/DynamicTypeImpl.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ class DynamicTypeImpl : public virtual traits<DynamicType>::base_type
116116
return default_union_member_;
117117
}
118118

119+
ObjectName default_literal() const noexcept
120+
{
121+
return default_literal_;
122+
}
123+
119124
traits<DynamicTypeImpl>::ref_type resolve_alias_enclosed_type() noexcept;
120125

121126
protected:
@@ -134,6 +139,9 @@ class DynamicTypeImpl : public virtual traits<DynamicType>::base_type
134139
//! Points to the default union member.
135140
MemberId default_union_member_ {MEMBER_ID_INVALID};
136141

142+
//! Name of the enumeration's default literal.
143+
ObjectName default_literal_;
144+
137145
//! Index pointing the first own member, not inherited from a base_type.
138146
uint32_t index_own_members_ {0};
139147

src/cpp/fastdds/xtypes/dynamic_types/MemberDescriptorImpl.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ReturnCode_t MemberDescriptorImpl::copy_from(
9999
is_must_understand_ = descriptor.is_must_understand_;
100100
is_shared_ = descriptor.is_shared_;
101101
is_default_label_ = descriptor.is_default_label_;
102+
is_default_literal_ = descriptor.is_default_literal_;
102103
is_try_construct_kind_set_ = descriptor.is_try_construct_kind_set_;
103104

104105
return RETCODE_OK;
@@ -124,7 +125,9 @@ bool MemberDescriptorImpl::equals(
124125
is_optional_ == descriptor.is_optional_ &&
125126
is_must_understand_ == descriptor.is_must_understand_ &&
126127
is_shared_ == descriptor.is_shared_ &&
127-
is_default_label_ == descriptor.is_default_label_;
128+
is_default_label_ == descriptor.is_default_label_ &&
129+
is_default_literal_ == descriptor.is_default_literal_ &&
130+
is_try_construct_kind_set_ == descriptor.is_try_construct_kind_set_;
128131
}
129132

130133
bool MemberDescriptorImpl::equal_labels(
@@ -296,6 +299,16 @@ bool MemberDescriptorImpl::is_consistent() noexcept
296299
}
297300
}
298301

302+
// Check default_literal built-in annotation.
303+
if (is_default_literal_)
304+
{
305+
if (TK_ENUM != parent_kind_)
306+
{
307+
EPROSIMA_LOG_ERROR(DYN_TYPES, "is_default_literal is set but parent type of member is not TK_ENUM.");
308+
return false;
309+
}
310+
}
311+
299312
// Check is_shared built-in annotation.
300313
if (is_shared_)
301314
{

src/cpp/fastdds/xtypes/dynamic_types/MemberDescriptorImpl.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class MemberDescriptorImpl : public virtual MemberDescriptor
4040
//! If the union member is default.
4141
bool is_default_label_ {false};
4242

43+
//! If the enumeration member is default.
44+
bool is_default_literal_ {false};
45+
4346
//! If the member is key.
4447
bool is_key_ {false};
4548

@@ -308,6 +311,22 @@ class MemberDescriptorImpl : public virtual MemberDescriptor
308311
is_default_label_ = is_default_label;
309312
}
310313

314+
bool is_default_literal() const noexcept override
315+
{
316+
return is_default_literal_;
317+
}
318+
319+
bool& is_default_literal() noexcept override
320+
{
321+
return is_default_literal_;
322+
}
323+
324+
void is_default_literal(
325+
bool is_default_literal) noexcept override
326+
{
327+
is_default_literal_ = is_default_literal;
328+
}
329+
311330
ReturnCode_t copy_from(
312331
traits<MemberDescriptor>::ref_type descriptor) noexcept override;
313332

src/cpp/fastdds/xtypes/dynamic_types/idl_parser/Annotations/DefaultLiteralAnnotation.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ class DefaultLiteralAnnotation final : public BuiltinAnnotation
6969
{
7070
static_cast<void>(parameters);
7171
assert(descriptor != nullptr);
72-
73-
descriptor->is_default_label(true);
72+
descriptor->is_default_literal(true);
7473
return true;
7574
}
7675
};

test/feature/idl_parser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ add_custom_command(
8383
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/nested_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
8484
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/try_construct_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
8585
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/value_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
86+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/default_literal_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
8687
)

test/feature/idl_parser/IdlParserTests.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,56 @@ TEST_F(IdlParserTests, value_builtin_annotation)
28752875
// ASSERT_FALSE(builder);
28762876
}
28772877

2878+
TEST_F(IdlParserTests, default_literal_builtin_annotation)
2879+
{
2880+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2881+
MemberDescriptor::_ref_type member_descriptor{traits<MemberDescriptor>::make_shared()};
2882+
std::vector<std::string> include_paths;
2883+
2884+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2885+
2886+
// Set @default_literal annotation on a enumeration's member and check that it is correctly parsed
2887+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_valid_enum",
2888+
include_paths);
2889+
ASSERT_TRUE(builder);
2890+
DynamicTypeMember::_ref_type member;
2891+
EXPECT_EQ(builder->get_member_by_name(member, "ENUM_VALUE_2"), RETCODE_OK);
2892+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2893+
EXPECT_TRUE(member_descriptor->is_default_literal());
2894+
DynamicType::_ref_type type = builder->build();
2895+
ASSERT_TRUE(type);
2896+
2897+
// Negative case: Trying to annotate multiple members with @default_literal
2898+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_multiple_default_members",
2899+
include_paths);
2900+
ASSERT_FALSE(builder);
2901+
2902+
// Negative case: Trying to annotate a member with @default_literal using parameters
2903+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_extra_parameter",
2904+
include_paths);
2905+
ASSERT_FALSE(builder);
2906+
2907+
// Negative case: Trying to annotate a constructed type with @default_literal
2908+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_on_enum",
2909+
include_paths);
2910+
ASSERT_FALSE(builder);
2911+
2912+
// Negative case: Trying to annotate a struct member with @default_literal
2913+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_on_struct_member",
2914+
include_paths);
2915+
ASSERT_FALSE(builder);
2916+
2917+
// Negative case: Trying to annotate a union member with @default_literal
2918+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_on_union_member",
2919+
include_paths);
2920+
ASSERT_FALSE(builder);
2921+
2922+
// Negative case: Trying to annotate a union discriminator with @default_literal
2923+
builder = factory->create_type_w_uri("IDL/default_literal_annotation.idl", "default_literal_ann_on_union_discriminator",
2924+
include_paths);
2925+
ASSERT_FALSE(builder);
2926+
}
2927+
28782928
int main(
28792929
int argc,
28802930
char** argv)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
enum default_literal_ann_valid_enum
2+
{
3+
ENUM_VALUE_1,
4+
@default_literal ENUM_VALUE_2
5+
};
6+
7+
enum default_literal_ann_multiple_default_members
8+
{
9+
ENUM_VALUE_1,
10+
@default_literal ENUM_VALUE_2,
11+
@default_literal ENUM_VALUE_3
12+
};
13+
14+
enum default_literal_ann_extra_parameter
15+
{
16+
ENUM_VALUE_1,
17+
@default_literal(value = "extra") ENUM_VALUE_2
18+
};
19+
20+
@default_literal enum default_literal_ann_on_enum
21+
{
22+
ENUM_VALUE_1,
23+
ENUM_VALUE_2
24+
};
25+
26+
struct default_literal_ann_on_struct_member
27+
{
28+
@default_literal long invalid_member;
29+
};
30+
31+
union default_literal_ann_on_union_member switch(long)
32+
{
33+
case 0:
34+
@default_literal long invalid_member;
35+
default:
36+
float invalid_member;
37+
};
38+
39+
union default_literal_ann_on_union_discriminator switch(@default_literal long)
40+
{
41+
case 0:
42+
long first;
43+
default:
44+
string second;
45+
};

0 commit comments

Comments
 (0)