Skip to content

Commit

Permalink
Initial Support for C++11 Mapping
Browse files Browse the repository at this point in the history
For #9, Uses OpenDDS/OpenDDS#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.
  • Loading branch information
iguessthislldo committed Jun 19, 2020
1 parent dc379dd commit 2583f5a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
33 changes: 30 additions & 3 deletions pyopendds/dev/include/pyopendds/user.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ template<> class Type<i32>: public IntegerType<i32> {};

// 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<typename T>
class StringType {
public:
Expand All @@ -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;
}
Expand All @@ -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<s8>: public StringType<s8> {};
// TODO: Put Other String/Chare Types Here
// TODO: Put Other String/Char Types Here

// TODO: FloatingType for floating point type

Expand Down
18 changes: 11 additions & 7 deletions pyopendds/dev/itl2py/CppOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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<long>(cpp)));',
]),
'to_lines': '',
'from_lines': '\n'.join([
Expand Down
6 changes: 6 additions & 0 deletions pyopendds/dev/itl2py/templates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 13 additions & 5 deletions tests/basic_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
19 changes: 16 additions & 3 deletions tests/basic_test/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand Down

0 comments on commit 2583f5a

Please sign in to comment.