Allowing overwriting of default __deepcopy__ for SWIG-wrapped structures
When LAL structures get SWIG-wrapped, they get extended with a default __deepcopy__
method. However, in some cases, when the structures are not easily copied due to containing a variety of pointers, this will not work.
An example is the PulsarParameters
structure in lalpulsar/src/ReadPulsarParFile.h
, which contains a hash table. (Assuming the merge of !904 (merged)) This structure has a function to create a copy of itself, so one could try extending the SWIG-wrapped stricture, e.g., in the SWIGLALPulsarOmega.i
file, with a new __deepcopy__
method, such as:
%extend tagPulsarParameters{
/// Return a copy of the ::PulsarParameters in <tt>$self</tt>
%typemap(in, noblock=1) const void *memo "";
struct tagPulsarParameters * __deepcopy__(const void *memo) {
// create new empty structure
PulsarParameters * pcopy = %swiglal_new_instance(PulsarParameters);
// copy the structure
PulsarCopyParams( *$self, pcopy );
return pcopy;
}
%clear const void *memo;
}
However, if I try this it won't compile due to a conflict with already having a __deepcopy__
method defined.
Would it be possible to maybe define a COPYFUNC
value for each structure if applicable, similar to the DTORFUNC
for use in SWIGPython.i
, which can be set as the copying function for a particular structure if given. E.g., allowing something like:
#if !OPAQUE && #DTORFUNC == "" && #COPYFUNC == ""
%extend TAGNAME {
%typemap(in, noblock=1) const void *memo "";
struct TAGNAME *__deepcopy__(const void *memo) {
return %swiglal_new_copy(*$self, struct TAGNAME);
}
%clear const void *memo;
}
#elif !OPAQUE && #COPYFUNC != ""
%extend TAGNAME {
%typemap(in, noblock=1) const void *memo "";
struct TAGNAME *__deepcopy__(const void *memo) {
TAGNAME * scopy = %swiglal_new_instance(TAGNAME);
COPYFUNC( *$self, scopy );
return scopy;
}
%clear const void *memo;
}
#else
%extend TAGNAME {
%typemap(in, noblock=1) const void *memo "";
struct TAGNAME *__deepcopy__(const void *memo) {
XLALSetErrno(XLAL_ENOSYS); /* Silently signal an error to wrapper function */
return NULL;
}
%clear const void *memo;
}
#endif
Of course, this assumes all copy functions follow the same convention!
cc @karl-wette