Skip to content

Commit 508fe04

Browse files
committed
Add Python-specific SWIG wrapper with vector template instantiations
The upstream libcdoc.i only defines %template(LockVector) for std::vector<libcdoc::Lock>. The ByteVector, ByteVectorVector, and StringVector types used by pycdoc's __init__.py have no corresponding %template directives — the upstream handles these via Java-specific typemaps (#ifdef SWIGJAVA) which don't apply to Python. This creates swig/pycdoc.i, a thin wrapper that: - Includes the upstream libcdoc.i - Adds %template(ByteVector) for std::vector<uint8_t> - Adds %template(ByteVectorVector) for std::vector<std::vector<uint8_t>> - Adds %template(StringVector) for std::vector<std::string> - Enables %feature("director") for Python subclassing of C++ classes CMakeLists.txt is updated to use our wrapper as the SWIG source, with include paths pointing to both libcdoc/ (for libcdoc.i) and libcdoc/cdoc/ (for the C++ headers). https://claude.ai/code/session_01KccqZFv6MqP2JVWabYn3mb
1 parent 81171b4 commit 508fe04

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,24 @@ macro(find_package)
4747
_find_package(${ARGV})
4848
endmacro()
4949

50-
# Set up SWIG for Python - use libcdoc as module name (matches SWIG %module directive)
50+
# Set up SWIG for Python using our custom wrapper that adds Python-specific
51+
# template instantiations (ByteVector, StringVector, etc.) and director support
52+
set(PYCDOC_SWIG_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/swig/pycdoc.i)
5153
set(CMAKE_SWIG_FLAGS "")
52-
set_property(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/libcdoc.i PROPERTY CPLUSPLUS ON)
53-
set_property(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/libcdoc.i PROPERTY SWIG_MODULE_NAME libcdoc)
54-
# Set SWIG include directories
55-
set_property(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/libcdoc.i
56-
PROPERTY INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/cdoc)
54+
set_property(SOURCE ${PYCDOC_SWIG_SOURCE} PROPERTY CPLUSPLUS ON)
55+
set_property(SOURCE ${PYCDOC_SWIG_SOURCE} PROPERTY SWIG_MODULE_NAME libcdoc)
56+
# Set SWIG include directories - need both libcdoc/ (for libcdoc.i) and libcdoc/cdoc/ (for headers)
57+
set_property(SOURCE ${PYCDOC_SWIG_SOURCE}
58+
PROPERTY INCLUDE_DIRECTORIES
59+
${CMAKE_CURRENT_SOURCE_DIR}/libcdoc
60+
${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/cdoc)
5761

5862
# Create the Python extension
5963
swig_add_library(pycdoc_swig
6064
TYPE MODULE
6165
LANGUAGE python
6266
OUTPUT_DIR ${CMAKE_BINARY_DIR}/pycdoc
63-
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/libcdoc.i
67+
SOURCES ${PYCDOC_SWIG_SOURCE}
6468
)
6569

6670
# Include directories for C++ compilation
@@ -74,7 +78,7 @@ set_target_properties(pycdoc_swig PROPERTIES
7478
OUTPUT_NAME "_libcdoc"
7579
PREFIX ""
7680
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/pycdoc
77-
SWIG_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/cdoc
81+
SWIG_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/libcdoc;${CMAKE_CURRENT_SOURCE_DIR}/libcdoc/cdoc"
7882
)
7983

8084
# Link against cdoc and Python

swig/pycdoc.i

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* pycdoc - Python-specific SWIG interface for libcdoc
3+
*
4+
* This wraps the upstream libcdoc.i and adds Python-specific
5+
* template instantiations and director support.
6+
*/
7+
8+
/* Enable directors for Python subclassing of C++ classes */
9+
%feature("director") libcdoc::DataSource;
10+
%feature("director") libcdoc::CryptoBackend;
11+
%feature("director") libcdoc::PKCS11Backend;
12+
%feature("director") libcdoc::NetworkBackend;
13+
%feature("director") libcdoc::Configuration;
14+
%feature("director") libcdoc::ILogger;
15+
16+
/* Include the upstream libcdoc SWIG interface */
17+
%include "libcdoc.i"
18+
19+
/* Python-specific std::vector template instantiations */
20+
%template(ByteVector) std::vector<uint8_t>;
21+
%template(ByteVectorVector) std::vector<std::vector<uint8_t>>;
22+
%template(StringVector) std::vector<std::string>;

0 commit comments

Comments
 (0)