Skip to content

Commit 3bab873

Browse files
committed
Generate type definitions for exceptions (#436)
* Refs #22768. Added test file for exceptions. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Add new flag to context. In order to know that at least one exception is to be generated. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Generate exceptions on main header file. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Generate serialization code for exceptions. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Serialization of exception message. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Update IDL parser. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Mark exceptions as DLL API. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Apply suggestion. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #22768. Update submodules. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> --------- Signed-off-by: Miguel Company <miguelcompany@eprosima.com>
1 parent 54b3859 commit 3bab873

File tree

6 files changed

+194
-3
lines changed

6 files changed

+194
-3
lines changed

src/main/java/com/eprosima/fastcdr/idl/templates/TypesHeader.stg

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ $if(ctx.thereIsUnion)$
5454
#include <fastcdr/exceptions/BadParamException.h>
5555
$endif$
5656

57+
$if(ctx.thereIsException)$
58+
#include <fastdds/dds/rpc/exceptions/RpcOperationError.hpp>
59+
$endif$
60+
5761
$ctx.directIncludeDependencies : {include | #include "$include$.hpp"}; separator="\n"$
5862

5963
#if defined(_WIN32)
@@ -108,6 +112,70 @@ namespace $annotation.name$ {
108112
$endif$
109113
>>
110114

115+
exception(ctx, parent, exception, extensions) ::= <<
116+
/*!
117+
* @brief This class implements the user exception $exception.scopedname$
118+
* @ingroup $ctx.trimfilename$
119+
*/
120+
class $ctx.fileNameUpper$_DllAPI $exception.name$ : public eprosima::fastdds::dds::rpc::RpcOperationError
121+
{
122+
public:
123+
124+
/**
125+
* Default constructor.
126+
*/
127+
$exception.name$()
128+
: $exception.name$("$exception.name$")
129+
{
130+
}
131+
132+
/**
133+
* Constructor.
134+
*/
135+
$exception.name$(
136+
const std::string& message)
137+
: eprosima::fastdds::dds::rpc::RpcOperationError(message)
138+
{
139+
}
140+
141+
/**
142+
* Constructor.
143+
*/
144+
$exception.name$(
145+
const char* message)
146+
: eprosima::fastdds::dds::rpc::RpcOperationError(message)
147+
{
148+
}
149+
150+
/**
151+
* Copy constructor.
152+
*/
153+
$exception.name$(
154+
const $exception.name$& other) noexcept = default;
155+
156+
/**
157+
* Copy assignment.
158+
*/
159+
$exception.name$& operator =(
160+
const $exception.name$& other) noexcept = default;
161+
162+
/**
163+
* Destructor.
164+
*/
165+
virtual ~$exception.name$() noexcept = default;
166+
167+
$exception.members:{ member | $public_member_declaration(member)$}; separator="\n"$
168+
169+
$extensions : { extension | $extension$}; separator="\n"$
170+
171+
private:
172+
173+
$exception.members:{ member | $private_member_declaration(member=member)$}; separator="\n"$
174+
175+
};
176+
177+
>>
178+
111179
interface(ctx, parent, interface, export_list) ::= <<
112180
/*!
113181
* @brief This class represents the interface $interface.name$ defined by the user in the IDL file.

src/main/java/com/eprosima/fastdds/fastddsgen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ private Project parseIDL(
10131013
}
10141014
}
10151015

1016-
if (ctx.isThereIsStructOrUnion())
1016+
if (ctx.isThereIsStructOrUnion() || ctx.isThereIsException())
10171017
{
10181018
if (returnedValue &=
10191019
Utils.writeFile(output_dir + ctx.getFilename() + "CdrAux.hpp",

src/main/java/com/eprosima/fastdds/idl/grammar/Context.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ public void addTypeDeclaration(
306306
}
307307
}
308308

309+
@Override
310+
public com.eprosima.idl.parser.tree.Exception createException(
311+
String name,
312+
Token token)
313+
{
314+
if (isInScopedFile())
315+
{
316+
there_is_at_least_one_exception = true;
317+
}
318+
319+
return super.createException(name, token);
320+
}
321+
322+
309323
public boolean isClient()
310324
{
311325
return m_subscribercode;
@@ -459,6 +473,11 @@ public boolean isThereIsStructOrUnion()
459473
return there_is_at_least_one_struct || there_is_at_least_one_union;
460474
}
461475

476+
public boolean isThereIsException()
477+
{
478+
return there_is_at_least_one_exception;
479+
}
480+
462481
/*** Functions inherited from FastCDR Context ***/
463482

464483
@Override
@@ -757,4 +776,6 @@ public Param createParam(
757776
private boolean there_is_at_least_one_struct = false;
758777

759778
private boolean there_is_at_least_one_union = false;
779+
780+
private boolean there_is_at_least_one_exception = false;
760781
}

src/main/java/com/eprosima/fastdds/idl/templates/TypesCdrAuxHeaderImpl.stg

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,105 @@ eProsima_user_DllExport void deserialize(
416416
}
417417
$endif$
418418
>>
419+
420+
exception(ctx, parent, exception) ::= <<
421+
template<>
422+
eProsima_user_DllExport size_t calculate_serialized_size(
423+
eprosima::fastcdr::CdrSizeCalculator& calculator,
424+
const $exception.scopedname$& data,
425+
size_t& current_alignment)
426+
{
427+
$if(!exception.scope.empty)$
428+
using namespace $exception.scope$;
429+
$endif$
430+
431+
static_cast<void>(data);
432+
433+
eprosima::fastcdr::EncodingAlgorithmFlag previous_encoding = calculator.get_encoding();
434+
size_t calculated_size {calculator.begin_calculate_type_serialized_size(
435+
eprosima::fastcdr::CdrVersion::XCDRv2 == calculator.get_cdr_version() ?
436+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2 :
437+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
438+
current_alignment)};
439+
440+
std::string msg = data.what();
441+
calculator.calculate_member_serialized_size(eprosima::fastcdr::MemberId(0), msg, current_alignment);
442+
443+
$exception.members : { member | $if(!member.annotationNonSerialized)$
444+
calculated_size += calculator.calculate_member_serialized_size(eprosima::fastcdr::MemberId($member.index$ + 1),
445+
data.$member.name$(), current_alignment);
446+
$endif$}; separator="\n"$
447+
448+
calculated_size += calculator.end_calculate_type_serialized_size(previous_encoding, current_alignment);
449+
450+
return calculated_size;
451+
}
452+
453+
template<>
454+
eProsima_user_DllExport void serialize(
455+
eprosima::fastcdr::Cdr& scdr,
456+
const $exception.scopedname$& data)
457+
{
458+
$if(!exception.scope.empty)$
459+
using namespace $exception.scope$;
460+
$endif$
461+
462+
eprosima::fastcdr::Cdr::state current_state(scdr);
463+
scdr.begin_serialize_type(current_state,
464+
eprosima::fastcdr::CdrVersion::XCDRv2 == scdr.get_cdr_version() ?
465+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2 :
466+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR);
467+
468+
scdr << eprosima::fastcdr::MemberId(0) << data.what();
469+
$if(exception.members)$
470+
scdr$exception.members : { member | $if(!member.annotationNonSerialized)$
471+
<< eprosima::fastcdr::MemberId($member.index$ + 1) << data.$member.name$()
472+
$endif$
473+
}; separator=""$;
474+
$endif$
475+
476+
scdr.end_serialize_type(current_state);
477+
}
478+
479+
template<>
480+
eProsima_user_DllExport void deserialize(
481+
eprosima::fastcdr::Cdr& cdr,
482+
$exception.scopedname$& data)
483+
{
484+
$if(!exception.scope.empty)$
485+
using namespace $exception.scope$;
486+
$endif$
487+
488+
cdr.deserialize_type(eprosima::fastcdr::CdrVersion::XCDRv2 == cdr.get_cdr_version() ?
489+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2 :
490+
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
491+
[&data](eprosima::fastcdr::Cdr& dcdr, const eprosima::fastcdr::MemberId& mid) -> bool
492+
{
493+
bool ret_value = true;
494+
switch (mid.id)
495+
{
496+
case 0:
497+
{
498+
std::string msg;
499+
dcdr \>> msg;
500+
$exception.name$ tmp{msg};
501+
data = tmp;
502+
break;
503+
}
504+
505+
$exception.members : { member |
506+
case $member.index$ + 1:
507+
$if(!member.annotationNonSerialized)$
508+
dcdr \>> data.$member.name$();
509+
$endif$
510+
break;
511+
}; separator="\n"$
512+
default:
513+
ret_value = false;
514+
break;
515+
}
516+
return ret_value;
517+
});
518+
}
519+
520+
>>

0 commit comments

Comments
 (0)