From 2583f5a91df33f392c67f7886ff42b3cd158d3ae Mon Sep 17 00:00:00 2001 From: Fred Hornsey Date: Fri, 19 Jun 2020 02:18:56 -0500 Subject: [PATCH] Initial Support for C++11 Mapping For #9, Uses https://github.com/objectcomputing/OpenDDS/pull/1728 to automatically switch the CPython mapping to use the IDL-to-C++11 mapping instead of the IDL-to-C++03 mapping Still need to refine it and setup CI testing. --- pyopendds/dev/include/pyopendds/user.hpp | 33 +++++++++++++++++-- pyopendds/dev/itl2py/CppOutput.py | 18 ++++++---- pyopendds/dev/itl2py/templates/CMakeLists.txt | 6 ++++ tests/basic_test/CMakeLists.txt | 18 +++++++--- tests/basic_test/publisher.cpp | 19 +++++++++-- 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/pyopendds/dev/include/pyopendds/user.hpp b/pyopendds/dev/include/pyopendds/user.hpp index 4ce09f2..aa7c7fd 100644 --- a/pyopendds/dev/include/pyopendds/user.hpp +++ b/pyopendds/dev/include/pyopendds/user.hpp @@ -65,6 +65,26 @@ template<> class Type: public IntegerType {}; // TODO: Put Other Integer Types Here +const char* string_data(const std::string& cpp) +{ + return cpp.data(); +} + +const char* string_data(const char* cpp) +{ + return cpp; +} + +size_t string_length(const std::string& cpp) +{ + return cpp.size(); +} + +size_t string_length(const char* cpp) +{ + return std::strlen(cpp); +} + template class StringType { public: @@ -75,7 +95,8 @@ class StringType { static void cpp_to_python(const T& cpp, PyObject*& py, const char* encoding) { - PyObject* o = PyUnicode_Decode(cpp, std::strlen(cpp), encoding, "strict"); + PyObject* o = PyUnicode_Decode( + string_data(cpp), string_length(cpp), encoding, "strict"); if (!o) throw Exception(); py = o; } @@ -88,9 +109,15 @@ class StringType { // TODO: Add seperate RawStringType where get_python_class returns PyBytes_Type -typedef ::TAO::String_Manager s8; +typedef +#ifdef CPP11_IDL + std::string +#else + ::TAO::String_Manager +#endif + s8; template<> class Type: public StringType {}; -// TODO: Put Other String/Chare Types Here +// TODO: Put Other String/Char Types Here // TODO: FloatingType for floating point type diff --git a/pyopendds/dev/itl2py/CppOutput.py b/pyopendds/dev/itl2py/CppOutput.py index a8efee0..30f48a5 100644 --- a/pyopendds/dev/itl2py/CppOutput.py +++ b/pyopendds/dev/itl2py/CppOutput.py @@ -49,13 +49,17 @@ def visit_struct(self, struct_type): to_lines = [] from_lines = [] pyopendds_type = '' + is_string = isinstance(field_node.type_node, PrimitiveType) and \ + field_node.type_node.is_string() - if isinstance(field_node.type_node, PrimitiveType) and field_node.type_node.is_string(): - to_lines.append('Type<{pyopendds_type}>::cpp_to_python(' - 'cpp.{field_name}, *field_value, "{default_encoding}");') - else: - to_lines.append('Type<{pyopendds_type}>::cpp_to_python(' - 'cpp.{field_name}, *field_value);') + to_lines = [ + 'Type<{pyopendds_type}>::cpp_to_python(cpp.{field_name}', + '#ifdef CPP11_IDL', + ' ()', + '#endif', + ' , *field_value' + + (', "{default_encoding}"' if is_string else '') + ');', + ] pyopendds_type = cpp_type_name(field_node.type_node) @@ -100,7 +104,7 @@ def visit_enum(self, enum_type): 'local_name': enum_type.local_name(), 'to_replace': True, 'new_lines': '\n'.join([ - 'args = PyTuple_Pack(1, PyLong_FromLong(cpp));', + 'args = PyTuple_Pack(1, PyLong_FromLong(static_cast(cpp)));', ]), 'to_lines': '', 'from_lines': '\n'.join([ diff --git a/pyopendds/dev/itl2py/templates/CMakeLists.txt b/pyopendds/dev/itl2py/templates/CMakeLists.txt index 2e495e8..f9d2d37 100644 --- a/pyopendds/dev/itl2py/templates/CMakeLists.txt +++ b/pyopendds/dev/itl2py/templates/CMakeLists.txt @@ -18,3 +18,9 @@ set_target_properties({{ native_package_name }} PROPERTIES LIBRARY_OUTPUT_NAME ${PYOPENDDS_NATIVE_FILENAME} SUFFIX "" ) +get_property(idl_mappings TARGET {{ idl_library_cmake_name }} + PROPERTY OPENDDS_LANGUAGE_MAPPINGS) +if("C++11" IN_LIST idl_mappings) + set_target_properties({{ native_package_name }} PROPERTIES + COMPILE_DEFINITIONS "CPP11_IDL") +endif() diff --git a/tests/basic_test/CMakeLists.txt b/tests/basic_test/CMakeLists.txt index 2455189..993b58b 100644 --- a/tests/basic_test/CMakeLists.txt +++ b/tests/basic_test/CMakeLists.txt @@ -5,14 +5,22 @@ project(PyOpenDDS_Test) list(APPEND CMAKE_MODULE_PATH "$ENV{DDS_ROOT}/cmake") find_package(OpenDDS REQUIRED) + add_library(basic_idl SHARED) -OPENDDS_TARGET_SOURCES(basic_idl basic.idl OPENDDS_IDL_OPTIONS -Gitl) +if(${CPP11_IDL}) + set(opendds_idl_mapping_option "-Lc++11") +endif() +OPENDDS_TARGET_SOURCES(basic_idl "basic.idl" + OPENDDS_IDL_OPTIONS "-Gitl" "${opendds_idl_mapping_option}") target_link_libraries(basic_idl PUBLIC OpenDDS::Dcps) export( - TARGETS basic_idl - FILE "${CMAKE_CURRENT_BINARY_DIR}/basic_idlConfig.cmake" + TARGETS basic_idl + FILE "${CMAKE_CURRENT_BINARY_DIR}/basic_idlConfig.cmake" ) add_executable(publisher publisher.cpp) -target_link_libraries(publisher OpenDDS::OpenDDS) -target_link_libraries(publisher basic_idl) +target_link_libraries(publisher OpenDDS::OpenDDS basic_idl) +if(${CPP11_IDL}) + set_target_properties(publisher PROPERTIES + COMPILE_DEFINITIONS "CPP11_IDL") +endif() diff --git a/tests/basic_test/publisher.cpp b/tests/basic_test/publisher.cpp index bf1129a..05e2552 100644 --- a/tests/basic_test/publisher.cpp +++ b/tests/basic_test/publisher.cpp @@ -92,9 +92,22 @@ int main(int argc, char* argv[]) { basic::ReadingDataWriter_var reading_writer = basic::ReadingDataWriter::_narrow(writer); basic::Reading reading; - reading.kind = basic::speed; - reading.value = -200; - reading.where = "Somewhere"; + reading.kind +#ifdef CPP11_IDL + () = basic::ReadingKind::speed; +#else + = basic::speed; +#endif + reading.value +#ifdef CPP11_IDL + () +#endif + = -200; + reading.where +#ifdef CPP11_IDL + () +#endif + = "Somewhere"; rc = reading_writer->write(reading, DDS::HANDLE_NIL); if (rc != DDS::RETCODE_OK) { std::cerr << "Error: Failed to write: "