Skip to content

Commit b76cfdc

Browse files
committed
Refs #20733: Properly fix key annotation handling
Signed-off-by: eduponz <[email protected]>
1 parent f3feee5 commit b76cfdc

File tree

5 files changed

+109
-56
lines changed

5 files changed

+109
-56
lines changed

include/fastrtps/types/MemberDescriptor.h

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
#include <fastrtps/types/TypesBase.h>
1919
#include <fastrtps/types/DynamicTypePtr.h>
2020

21-
namespace eprosima{
22-
namespace fastrtps{
23-
namespace types{
21+
namespace eprosima {
22+
namespace fastrtps {
23+
namespace types {
2424

2525
class DynamicType;
2626
class AnnotationDescriptor;
2727

2828
class MemberDescriptor
2929
{
3030
protected:
31+
3132
std::string name_; // Name of the member
3233
MemberId id_; // MemberId, it should be filled automatically when the member is added.
3334
DynamicType_ptr type_; // Member's Type.
@@ -43,11 +44,17 @@ class MemberDescriptor
4344
friend class DynamicTypeMember;
4445
friend class TypeObjectFactory;
4546

46-
bool is_default_value_consistent(const std::string& sDefaultValue) const;
47+
bool is_default_value_consistent(
48+
const std::string& sDefaultValue) const;
49+
50+
bool is_type_name_consistent(
51+
const std::string& sName) const;
4752

48-
bool is_type_name_consistent(const std::string& sName) const;
53+
void copy_annotations_from_type(
54+
const DynamicType_ptr& type);
4955

5056
public:
57+
5158
RTPS_DllAPI MemberDescriptor();
5259

5360
RTPS_DllAPI MemberDescriptor(
@@ -57,37 +64,41 @@ class MemberDescriptor
5764
RTPS_DllAPI MemberDescriptor(
5865
MemberId id,
5966
const std::string& name,
60-
DynamicType_ptr type_);
67+
DynamicType_ptr type);
6168

6269
RTPS_DllAPI MemberDescriptor(
6370
MemberId id,
6471
const std::string& name,
65-
DynamicType_ptr type_,
72+
DynamicType_ptr type,
6673
const std::string& defaultValue);
6774

6875
RTPS_DllAPI MemberDescriptor(
6976
MemberId id,
7077
const std::string& name,
71-
DynamicType_ptr type_,
78+
DynamicType_ptr type,
7279
const std::string& defaultValue,
7380
const std::vector<uint64_t>& unionLabels,
7481
bool isDefaultLabel);
7582

76-
RTPS_DllAPI MemberDescriptor(const MemberDescriptor* descriptor);
83+
RTPS_DllAPI MemberDescriptor(
84+
const MemberDescriptor* descriptor);
7785

7886
RTPS_DllAPI ~MemberDescriptor();
7987

80-
bool check_union_labels(const std::vector<uint64_t>& labels) const;
88+
bool check_union_labels(
89+
const std::vector<uint64_t>& labels) const;
8190

82-
RTPS_DllAPI ReturnCode_t copy_from(const MemberDescriptor* other);
91+
RTPS_DllAPI ReturnCode_t copy_from(
92+
const MemberDescriptor* other);
8393

84-
RTPS_DllAPI bool equals(const MemberDescriptor* other) const;
94+
RTPS_DllAPI bool equals(
95+
const MemberDescriptor* other) const;
8596

8697
RTPS_DllAPI TypeKind get_kind() const;
8798

8899
RTPS_DllAPI MemberId get_id() const;
89100

90-
RTPS_DllAPI uint32_t get_index() const;
101+
RTPS_DllAPI uint32_t get_index() const;
91102

92103
RTPS_DllAPI std::string get_name() const;
93104

@@ -105,39 +116,49 @@ class MemberDescriptor
105116

106117
RTPS_DllAPI bool is_default_union_value() const;
107118

108-
RTPS_DllAPI bool is_consistent(TypeKind parentKind) const;
119+
RTPS_DllAPI bool is_consistent(
120+
TypeKind parentKind) const;
109121

110-
RTPS_DllAPI void add_union_case_index(uint64_t value);
122+
RTPS_DllAPI void add_union_case_index(
123+
uint64_t value);
111124

112-
RTPS_DllAPI void set_id(MemberId id);
125+
RTPS_DllAPI void set_id(
126+
MemberId id);
113127

114-
RTPS_DllAPI void set_index(uint32_t index);
128+
RTPS_DllAPI void set_index(
129+
uint32_t index);
115130

116-
RTPS_DllAPI void set_name(const std::string& name);
131+
RTPS_DllAPI void set_name(
132+
const std::string& name);
117133

118-
RTPS_DllAPI void set_type(DynamicType_ptr type);
134+
RTPS_DllAPI void set_type(
135+
DynamicType_ptr type);
119136

120137
RTPS_DllAPI DynamicType_ptr get_type() const
121138
{
122139
return type_;
123140
}
124141

125-
RTPS_DllAPI void set_default_union_value(bool bDefault);
142+
RTPS_DllAPI void set_default_union_value(
143+
bool bDefault);
126144

127-
RTPS_DllAPI void set_default_value(const std::string& value)
145+
RTPS_DllAPI void set_default_value(
146+
const std::string& value)
128147
{
129148
default_value_ = value;
130149
}
131150

132151
// Annotations
133-
ReturnCode_t apply_annotation(AnnotationDescriptor& descriptor);
152+
ReturnCode_t apply_annotation(
153+
AnnotationDescriptor& descriptor);
134154

135155
ReturnCode_t apply_annotation(
136156
const std::string& annotation_name,
137157
const std::string& key,
138158
const std::string& value);
139159

140-
AnnotationDescriptor* get_annotation(const std::string& name) const;
160+
AnnotationDescriptor* get_annotation(
161+
const std::string& name) const;
141162

142163
// Annotations application
143164
RTPS_DllAPI bool annotation_is_optional() const;
@@ -168,23 +189,31 @@ class MemberDescriptor
168189
RTPS_DllAPI uint16_t annotation_get_bit_bound() const;
169190

170191
// Annotations setters
171-
RTPS_DllAPI void annotation_set_optional(bool optional);
192+
RTPS_DllAPI void annotation_set_optional(
193+
bool optional);
172194

173-
RTPS_DllAPI void annotation_set_key(bool key);
195+
RTPS_DllAPI void annotation_set_key(
196+
bool key);
174197

175-
RTPS_DllAPI void annotation_set_must_understand(bool must_understand);
198+
RTPS_DllAPI void annotation_set_must_understand(
199+
bool must_understand);
176200

177-
RTPS_DllAPI void annotation_set_non_serialized(bool non_serialized);
201+
RTPS_DllAPI void annotation_set_non_serialized(
202+
bool non_serialized);
178203

179-
RTPS_DllAPI void annotation_set_value(const std::string& value);
204+
RTPS_DllAPI void annotation_set_value(
205+
const std::string& value);
180206

181-
RTPS_DllAPI void annotation_set_default(const std::string& default_value);
207+
RTPS_DllAPI void annotation_set_default(
208+
const std::string& default_value);
182209

183210
RTPS_DllAPI void annotation_set_default_literal();
184211

185-
RTPS_DllAPI void annotation_set_position(uint16_t position);
212+
RTPS_DllAPI void annotation_set_position(
213+
uint16_t position);
186214

187-
RTPS_DllAPI void annotation_set_bit_bound(uint16_t bit_bound);
215+
RTPS_DllAPI void annotation_set_bit_bound(
216+
uint16_t bit_bound);
188217
};
189218

190219
} // namespace types

src/cpp/dynamic-types/AnnotationDescriptor.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,23 @@ bool AnnotationDescriptor::equals(
9090

9191
bool AnnotationDescriptor::key_annotation() const
9292
{
93-
auto it = value_.find(ANNOTATION_KEY_ID);
94-
if (it == value_.end())
93+
bool ret = false;
94+
95+
// Annotations @key and @Key have names "key" and "Key" respectively.
96+
if (type_ && (type_->get_name() == ANNOTATION_KEY_ID || type_->get_name() == ANNOTATION_EPKEY_ID))
9597
{
96-
it = value_.find(ANNOTATION_EPKEY_ID); // Legacy "@Key"
98+
// When an annotation is a key annotation, there is only one entry in value_.
99+
// Its map key is "value" and its value is either "true" of "false".
100+
// We cannot call get_value() directly because it is not const-qualified
101+
auto it = value_.find("value");
102+
103+
if (it != value_.end())
104+
{
105+
ret = it->second == CONST_TRUE;
106+
}
97107
}
98-
return (it != value_.end() && it->second == CONST_TRUE);
108+
109+
return ret;
99110
}
100111

101112
ReturnCode_t AnnotationDescriptor::get_value(

src/cpp/dynamic-types/DynamicType.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ ReturnCode_t DynamicType::copy_from_builder(
191191
{
192192
DynamicTypeMember* newMember = new DynamicTypeMember(it->second);
193193
newMember->set_parent(this);
194-
is_key_defined_ = newMember->key_annotation();
194+
is_key_defined_ |= newMember->key_annotation();
195195
member_by_id_.insert(std::make_pair(newMember->get_id(), newMember));
196196
member_by_name_.insert(std::make_pair(newMember->get_name(), newMember));
197197
}
@@ -245,14 +245,7 @@ TypeDescriptor* DynamicType::get_descriptor()
245245

246246
bool DynamicType::key_annotation() const
247247
{
248-
for (auto anIt = descriptor_->annotation_.begin(); anIt != descriptor_->annotation_.end(); ++anIt)
249-
{
250-
if ((*anIt)->key_annotation())
251-
{
252-
return true;
253-
}
254-
}
255-
return false;
248+
return descriptor_->annotation_get_key();
256249
}
257250

258251
bool DynamicType::equals(

src/cpp/dynamic-types/MemberDescriptor.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ MemberDescriptor::MemberDescriptor()
3131
, index_(INDEX_INVALID)
3232
, default_label_(false)
3333
{
34+
copy_annotations_from_type(type_);
3435
}
3536

3637
MemberDescriptor::MemberDescriptor(
@@ -43,6 +44,7 @@ MemberDescriptor::MemberDescriptor(
4344
, index_(index)
4445
, default_label_(false)
4546
{
47+
copy_annotations_from_type(type_);
4648
}
4749

4850
MemberDescriptor::MemberDescriptor(
@@ -55,51 +57,54 @@ MemberDescriptor::MemberDescriptor(
5557
, default_label_(false)
5658
{
5759
copy_from(descriptor);
60+
copy_annotations_from_type(type_);
5861
}
5962

6063
MemberDescriptor::MemberDescriptor(
6164
MemberId id,
6265
const std::string& name,
63-
DynamicType_ptr type_)
66+
DynamicType_ptr type)
6467
: name_(name)
6568
, id_(id)
66-
, type_(type_)
69+
, type_(type)
6770
, default_value_("")
6871
, index_(INDEX_INVALID)
6972
, default_label_(false)
7073
{
71-
74+
copy_annotations_from_type(type_);
7275
}
7376

7477
MemberDescriptor::MemberDescriptor(
7578
MemberId id,
7679
const std::string& name,
77-
DynamicType_ptr type_,
80+
DynamicType_ptr type,
7881
const std::string& defaultValue)
7982
: name_(name)
8083
, id_(id)
81-
, type_(type_)
84+
, type_(type)
8285
, default_value_(defaultValue)
8386
, index_(INDEX_INVALID)
8487
, default_label_(false)
8588
{
89+
copy_annotations_from_type(type_);
8690
}
8791

8892
MemberDescriptor::MemberDescriptor(
8993
MemberId id,
9094
const std::string& name,
91-
DynamicType_ptr type_,
95+
DynamicType_ptr type,
9296
const std::string& defaultValue,
9397
const std::vector<uint64_t>& unionLabels,
9498
bool isDefaultLabel)
9599
: name_(name)
96100
, id_(id)
97-
, type_(type_)
101+
, type_(type)
98102
, default_value_(defaultValue)
99103
, index_(INDEX_INVALID)
100104
, default_label_(isDefaultLabel)
101105
{
102106
labels_ = unionLabels;
107+
copy_annotations_from_type(type_);
103108
}
104109

105110
MemberDescriptor::~MemberDescriptor()
@@ -411,6 +416,25 @@ bool MemberDescriptor::is_type_name_consistent(
411416
return TypeDescriptor::is_type_name_consistent(sName);
412417
}
413418

419+
void MemberDescriptor::copy_annotations_from_type(
420+
const DynamicType_ptr& type)
421+
{
422+
if (type)
423+
{
424+
// Copy annotation from type
425+
uint32_t num_annotations = type->get_annotation_count();
426+
for (uint32_t i = 0; i < num_annotations; ++i)
427+
{
428+
AnnotationDescriptor ann;
429+
type->get_annotation(ann, i);
430+
AnnotationDescriptor* pNewDescriptor = new AnnotationDescriptor();
431+
pNewDescriptor->copy_from(&ann);
432+
annotation_.push_back(pNewDescriptor);
433+
}
434+
}
435+
436+
}
437+
414438
void MemberDescriptor::set_id(
415439
MemberId id)
416440
{

src/cpp/rtps/xmlparser/XMLDynamicParser.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,11 +1464,7 @@ p_dynamictypebuilder_t XMLParser::parseXMLMemberDynamicType(
14641464
{
14651465
if (strncmp(memberTopicKey, "true", 5) == 0)
14661466
{
1467-
memberBuilder->apply_annotation(types::ANNOTATION_KEY_ID, "key", "true");
1468-
if (p_dynamictype != nullptr)
1469-
{
1470-
p_dynamictype->apply_annotation(types::ANNOTATION_KEY_ID, "key", "true");
1471-
}
1467+
memberBuilder->apply_annotation(types::ANNOTATION_KEY_ID, "value", "true");
14721468
}
14731469
}
14741470

0 commit comments

Comments
 (0)