From ea942c67918759a013b4ae4716aaa50404413809 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Wed, 23 Oct 2024 15:06:38 +0200 Subject: [PATCH 1/5] Add first test and code for cpp_mapping annotation * tests/idl4/struct/client.cpp: * tests/idl4/struct/run_test.pl: * tests/idl4/struct/test.idl: * tests/idl4/struct/test.mpc: Added. * ridlbe/c++11/templates/cli/hdr/struct_idl_traits_def.erb: * ridlbe/c++11/templates/cli/hdr/struct_post.erb: * ridlbe/c++11/templates/cli/hdr/struct_pre.erb: * ridlbe/c++11/templates/cli/inl/struct_inl.erb: * ridlbe/c++11/templates/cli/src/struct_cdr.erb: * ridlbe/c++11/visitors/struct.rb: --- .../cli/hdr/struct_idl_traits_def.erb | 2 +- .../c++11/templates/cli/hdr/struct_post.erb | 16 ++++++++++ ridlbe/c++11/templates/cli/hdr/struct_pre.erb | 6 +++- ridlbe/c++11/templates/cli/inl/struct_inl.erb | 2 ++ ridlbe/c++11/templates/cli/src/struct_cdr.erb | 4 +-- ridlbe/c++11/visitors/struct.rb | 10 ++++++ tests/idl4/struct/client.cpp | 22 +++++++++++++ tests/idl4/struct/run_test.pl | 31 +++++++++++++++++++ tests/idl4/struct/test.idl | 16 ++++++++++ tests/idl4/struct/test.mpc | 20 ++++++++++++ 10 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 tests/idl4/struct/client.cpp create mode 100755 tests/idl4/struct/run_test.pl create mode 100644 tests/idl4/struct/test.idl create mode 100644 tests/idl4/struct/test.mpc diff --git a/ridlbe/c++11/templates/cli/hdr/struct_idl_traits_def.erb b/ridlbe/c++11/templates/cli/hdr/struct_idl_traits_def.erb index fc044550..7a96a126 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_idl_traits_def.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_idl_traits_def.erb @@ -39,7 +39,7 @@ struct formatter<<%= scoped_cxxtype %>, OStrm_> % elsif _m.external? %# TODO % else - << "<%= _sep %><%= _m.cxxname %>=" << IDL::traits<<%= _m.scoped_cxxtype %>>::write(val_.<%= _m.cxxname %> ()) + << "<%= _sep %><%= _m.cxxname %>=" << IDL::traits<<%= _m.scoped_cxxtype %>>::write(val_.<%= _m.cxxname %><%= member_accessor %>) % end % end % _sep = ',' unless _sep diff --git a/ridlbe/c++11/templates/cli/hdr/struct_post.erb b/ridlbe/c++11/templates/cli/hdr/struct_post.erb index 98c24c32..ce3eb665 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_post.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_post.erb @@ -1,4 +1,5 @@ // generated from <%= ridl_template_path %> +%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' <%= cxxname %> () = default; ~<%= cxxname %> () = default; % unless base.nil? @@ -46,6 +47,21 @@ private: % members.each do |_m| <%= _m.cxx_member_type %> <%= _m.cxxname %>_<%= _m.value_initializer %>; % end +%else +% members.each do |_m| + <%= _m.cxx_member_type %> <%= _m.cxxname %><%= _m.value_initializer %>; +% end +%end };// <%= cxxname %> +%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' inline void swap (<%= scoped_cxx_out_type %> m1, <%= scoped_cxx_out_type %> m2) { m1.swap (m2); } +%else +inline void swap (<%= scoped_cxx_out_type %> m1, <%= scoped_cxx_out_type %> m2) +{ +%# Add support for base +% members.each do |_m| + std::swap (m1.<%= _m.cxxname %>, m2.<%= _m.cxxname %>); +% end +} +%end diff --git a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb index c318d4f0..1b7e584c 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb @@ -1,6 +1,10 @@ - // generated from <%= ridl_template_path %> /// @copydoc <%= doc_scoped_name %> +%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' class <%= stub_export_macro %><%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %> { public: +%else +struct <%= stub_export_macro %><%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %> +{ +%end diff --git a/ridlbe/c++11/templates/cli/inl/struct_inl.erb b/ridlbe/c++11/templates/cli/inl/struct_inl.erb index c2c5f6da..5e2730d5 100644 --- a/ridlbe/c++11/templates/cli/inl/struct_inl.erb +++ b/ridlbe/c++11/templates/cli/inl/struct_inl.erb @@ -1,3 +1,4 @@ +%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' // generated from <%= ridl_template_path %> %# @@ -40,3 +41,4 @@ inline void <%= scoped_cxxtype %>::swap (<%= scoped_cxx_out_type %><% if member_ std::swap (this-><%= _m.cxxname %>_, s.<%= _m.cxxname %>_); % end } +%end diff --git a/ridlbe/c++11/templates/cli/src/struct_cdr.erb b/ridlbe/c++11/templates/cli/src/struct_cdr.erb index ccdafe2a..104f84f7 100644 --- a/ridlbe/c++11/templates/cli/src/struct_cdr.erb +++ b/ridlbe/c++11/templates/cli/src/struct_cdr.erb @@ -12,7 +12,7 @@ TAO_CORBA::Boolean operator<< (TAO_OutputCDR &<% if member_count > 0 || !base.ni % if _m.optional? (taox11_optional_cdr_in<<%= _m.cxx_member_type%><%= !_m.cdr_from_helper_type.nil? ? ', ' : '' %><%= _m.cdr_from_helper_type %>>::insert (strm, _tao_aggregate.<%= _m.cxxname %> ()))<%= ((_i < _n ) ? ' &&' : ';') %> % else - (strm << <%= _m.cdr_from_fmt % "_tao_aggregate.#{_m.cxxname} ()" %>)<%= ((_i < _n ) ? ' &&' : ';') %> + (strm << <%= _m.cdr_from_fmt % "_tao_aggregate.#{_m.cxxname}#{member_accessor}" %>)<%= ((_i < _n ) ? ' &&' : ';') %> % end % end % else @@ -32,7 +32,7 @@ TAO_CORBA::Boolean operator>> (TAO_InputCDR &<% if member_count > 0 || !base.nil % if _m.optional? (taox11_optional_cdr_out<<%= _m.cxx_member_type%><%= !_m.cdr_to_helper_type.nil? ? ', ' : '' %><%= _m.cdr_to_helper_type %>>::extract (strm, _tao_aggregate.<%= _m.cxxname %> ()))<%= ((_i < _n ) ? ' &&' : ';') %> % else - (strm >> <%= _m.cdr_to_fmt % "_tao_aggregate.#{_m.cxxname} ()" %>)<%= ((_i < _n) ? ' &&' : ';') %> + (strm >> <%= _m.cdr_to_fmt % "_tao_aggregate.#{_m.cxxname}#{member_accessor}" %>)<%= ((_i < _n) ? ' &&' : ';') %> % end % end % else diff --git a/ridlbe/c++11/visitors/struct.rb b/ridlbe/c++11/visitors/struct.rb index c9ddd0b9..fb8d4506 100644 --- a/ridlbe/c++11/visitors/struct.rb +++ b/ridlbe/c++11/visitors/struct.rb @@ -34,6 +34,16 @@ def base node.base end + def cpp_mapping + return 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' unless node.annotations[:cpp_mapping].first.nil? + node.annotations[:cpp_mapping].first + end + + def member_accessor + ' ()' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + '' + end + # template mapping map_template :typecode, :typecode diff --git a/tests/idl4/struct/client.cpp b/tests/idl4/struct/client.cpp new file mode 100644 index 00000000..5523b5f0 --- /dev/null +++ b/tests/idl4/struct/client.cpp @@ -0,0 +1,22 @@ +/** + * @file client.cpp + * @author Johnny Willemsen + * + * @brief CORBA C++11 client application + * + * @copyright Copyright (c) Remedy IT Expertise BV + */ + +#include "testC.h" +#include "testlib/taox11_testlog.h" + +int main (int /*argc*/, char* /*argv*/[]) +{ + bar_struct bs1; + bar_class bc1; + + // Just compilation test + TAOX11_TEST_INFO << "bs1: " << bs1 << std::endl; + + return 0; +} diff --git a/tests/idl4/struct/run_test.pl b/tests/idl4/struct/run_test.pl new file mode 100755 index 00000000..1a1bcc10 --- /dev/null +++ b/tests/idl4/struct/run_test.pl @@ -0,0 +1,31 @@ +#--------------------------------------------------------------------- +# @file run_test.pl +# @author Marcel Smit +# +# @copyright Copyright (c) Remedy IT Expertise BV +#--------------------------------------------------------------------- +eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' + & eval 'exec perl -S $0 $argv:q' + if 0; + +# -*- perl -*- + +use lib "$ENV{ACE_ROOT}/bin"; +use PerlACE::TestTarget; + +my $target = PerlACE::TestTarget::create_target(2) || die "Create target 2 failed\n"; + +$status = 0; + +$SV = $target->CreateProcess ("client"); + +$server = $SV->SpawnWaitKill ($target->ProcessStartWaitInterval()); + +if ($server != 0) { + print STDERR "ERROR: client returned $server\n"; + $status = 1; +} + +$target->GetStderrLog(); + +exit $status; diff --git a/tests/idl4/struct/test.idl b/tests/idl4/struct/test.idl new file mode 100644 index 00000000..dbdb1284 --- /dev/null +++ b/tests/idl4/struct/test.idl @@ -0,0 +1,16 @@ +/** + * @file test.idl + * @author Johnny Willemsen + * + * @copyright Copyright (c) Remedy IT Expertise BV + */ + +@struct_mapping(STRUCT_WITH_PUBLIC_MEMBERS) +struct bar_struct { + short b; +}; + +@struct_mapping(CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS) +struct bar_class { + short b; +}; diff --git a/tests/idl4/struct/test.mpc b/tests/idl4/struct/test.mpc new file mode 100644 index 00000000..7077d2f1 --- /dev/null +++ b/tests/idl4/struct/test.mpc @@ -0,0 +1,20 @@ +// -*- MPC -*- + +project(*structmap_gen_idl): ridl_ostream_defaults { + IDL_Files { + test.idl + idlflags += --idl-version=4 + } + custom_only = 1 +} + +project(*structmap_client): taox11_client { + after += *structmap_gen_idl + Source_Files { + client.cpp + } + Source_Files { + testC.cpp + } +} + From 5ec19f3ebed62134adc15e263625b4ac33859d14 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Wed, 23 Oct 2024 15:07:23 +0200 Subject: [PATCH 2/5] Add test * tests/idl4/struct/client.cpp -> tests/idl4/struct_cppmapping/client.cpp: * tests/idl4/struct/run_test.pl -> tests/idl4/struct_cppmapping/run_test.pl: * tests/idl4/struct/test.idl -> tests/idl4/struct_cppmapping/test.idl: * tests/idl4/struct/test.mpc -> tests/idl4/struct_cppmapping/test.mpc: Moved. * bin/taox11_tests.lst: --- bin/taox11_tests.lst | 1 + tests/idl4/{struct => struct_cppmapping}/client.cpp | 0 tests/idl4/{struct => struct_cppmapping}/run_test.pl | 0 tests/idl4/{struct => struct_cppmapping}/test.idl | 0 tests/idl4/{struct => struct_cppmapping}/test.mpc | 0 5 files changed, 1 insertion(+) rename tests/idl4/{struct => struct_cppmapping}/client.cpp (100%) rename tests/idl4/{struct => struct_cppmapping}/run_test.pl (100%) rename tests/idl4/{struct => struct_cppmapping}/test.idl (100%) rename tests/idl4/{struct => struct_cppmapping}/test.mpc (100%) diff --git a/bin/taox11_tests.lst b/bin/taox11_tests.lst index f528de63..dd981231 100644 --- a/bin/taox11_tests.lst +++ b/bin/taox11_tests.lst @@ -88,6 +88,7 @@ tests/idl4/illegal_idl/run_test.pl: tests/idl4/map/run_test.pl: tests/idl4/map/run_illegal_idl3_test.pl: tests/idl4/optional/run_test.pl: +tests/idl4/struct_cppmapping/run_test.pl: tests/idl4/struct_inheritance/run_test.pl: tests/idl4/struct_inheritance/run_illegal_idl3_test.pl: tests/idl4/union/run_test.pl: diff --git a/tests/idl4/struct/client.cpp b/tests/idl4/struct_cppmapping/client.cpp similarity index 100% rename from tests/idl4/struct/client.cpp rename to tests/idl4/struct_cppmapping/client.cpp diff --git a/tests/idl4/struct/run_test.pl b/tests/idl4/struct_cppmapping/run_test.pl similarity index 100% rename from tests/idl4/struct/run_test.pl rename to tests/idl4/struct_cppmapping/run_test.pl diff --git a/tests/idl4/struct/test.idl b/tests/idl4/struct_cppmapping/test.idl similarity index 100% rename from tests/idl4/struct/test.idl rename to tests/idl4/struct_cppmapping/test.idl diff --git a/tests/idl4/struct/test.mpc b/tests/idl4/struct_cppmapping/test.mpc similarity index 100% rename from tests/idl4/struct/test.mpc rename to tests/idl4/struct_cppmapping/test.mpc From 360e1f10a599d1cb61c9f841a1c81d7146120042 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Wed, 23 Oct 2024 15:08:09 +0200 Subject: [PATCH 3/5] Add newline back * ridlbe/c++11/templates/cli/hdr/struct_pre.erb: --- ridlbe/c++11/templates/cli/hdr/struct_pre.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb index 1b7e584c..379f5efc 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb @@ -1,3 +1,4 @@ + // generated from <%= ridl_template_path %> /// @copydoc <%= doc_scoped_name %> %if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' From a840a2ea4f80e4f51ad4d8daa3c49b47e4ea74eb Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Wed, 23 Oct 2024 15:17:34 +0200 Subject: [PATCH 4/5] Simplifications and docs * ridlbe/c++11/templates/cli/hdr/struct_post.erb: * ridlbe/c++11/templates/cli/hdr/struct_pre.erb: * ridlbe/c++11/visitors/struct.rb: --- ridlbe/c++11/templates/cli/hdr/struct_post.erb | 8 ++------ ridlbe/c++11/templates/cli/hdr/struct_pre.erb | 7 ++----- ridlbe/c++11/visitors/struct.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ridlbe/c++11/templates/cli/hdr/struct_post.erb b/ridlbe/c++11/templates/cli/hdr/struct_post.erb index ce3eb665..a6e71d62 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_post.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_post.erb @@ -44,14 +44,10 @@ inline void swap (<%= cxx_inout_type %> s); private: +%end % members.each do |_m| - <%= _m.cxx_member_type %> <%= _m.cxxname %>_<%= _m.value_initializer %>; -% end -%else -% members.each do |_m| - <%= _m.cxx_member_type %> <%= _m.cxxname %><%= _m.value_initializer %>; + <%= _m.cxx_member_type %> <%= _m.cxxname %><%= member_postfix %><%= _m.value_initializer %>; % end -%end };// <%= cxxname %> %if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' diff --git a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb index 379f5efc..12359c07 100644 --- a/ridlbe/c++11/templates/cli/hdr/struct_pre.erb +++ b/ridlbe/c++11/templates/cli/hdr/struct_pre.erb @@ -1,11 +1,8 @@ // generated from <%= ridl_template_path %> /// @copydoc <%= doc_scoped_name %> -%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' -class <%= stub_export_macro %><%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %> +<%= class_struct %> <%= stub_export_macro %><%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %> { +%if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' public: -%else -struct <%= stub_export_macro %><%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %> -{ %end diff --git a/ridlbe/c++11/visitors/struct.rb b/ridlbe/c++11/visitors/struct.rb index fb8d4506..0aae92f0 100644 --- a/ridlbe/c++11/visitors/struct.rb +++ b/ridlbe/c++11/visitors/struct.rb @@ -34,16 +34,30 @@ def base node.base end + # Which cpp_mapping we should use, default to class with methods def cpp_mapping return 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' unless node.annotations[:cpp_mapping].first.nil? node.annotations[:cpp_mapping].first end + # How can we access a struct member, directly or through a method call def member_accessor ' ()' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' '' end + # Possible postfix for all members in C++ + def member_postfix + '_' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + '' + end + + # Do we map to a C++ class or struct + def class_struct + 'class' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + 'struct' + end + # template mapping map_template :typecode, :typecode From 6094278ebfed20c995fb933cec331f388f81b703 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Wed, 23 Oct 2024 15:35:57 +0200 Subject: [PATCH 5/5] Fixes * ridlbe/c++11/visitors/struct.rb: * tests/idl4/struct_cppmapping/test.idl: --- ridlbe/c++11/visitors/struct.rb | 10 +++++----- tests/idl4/struct_cppmapping/test.idl | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ridlbe/c++11/visitors/struct.rb b/ridlbe/c++11/visitors/struct.rb index 0aae92f0..7d7febf8 100644 --- a/ridlbe/c++11/visitors/struct.rb +++ b/ridlbe/c++11/visitors/struct.rb @@ -36,25 +36,25 @@ def base # Which cpp_mapping we should use, default to class with methods def cpp_mapping - return 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' unless node.annotations[:cpp_mapping].first.nil? - node.annotations[:cpp_mapping].first + return 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' if node.annotations[:cpp_mapping].first.nil? + node.annotations[:cpp_mapping].first.fields[:value] end # How can we access a struct member, directly or through a method call def member_accessor - ' ()' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + return ' ()' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' '' end # Possible postfix for all members in C++ def member_postfix - '_' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + return '_' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' '' end # Do we map to a C++ class or struct def class_struct - 'class' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' + return 'class' if cpp_mapping == 'CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS' 'struct' end diff --git a/tests/idl4/struct_cppmapping/test.idl b/tests/idl4/struct_cppmapping/test.idl index dbdb1284..12e9f425 100644 --- a/tests/idl4/struct_cppmapping/test.idl +++ b/tests/idl4/struct_cppmapping/test.idl @@ -5,12 +5,12 @@ * @copyright Copyright (c) Remedy IT Expertise BV */ -@struct_mapping(STRUCT_WITH_PUBLIC_MEMBERS) +@cpp_mapping(STRUCT_WITH_PUBLIC_MEMBERS) struct bar_struct { short b; }; -@struct_mapping(CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS) +@cpp_mapping(CLASS_WITH_PUBLIC_ACCESSORS_AND_MODIFIERS) struct bar_class { short b; };