|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Need some help with parameterized constructors and SWIG
I'm in need of some expert SWIG help here. This is C++ <->
Python, btw, using SWIG 1.3.36.
I have the following C++ interface definition (relevant pieces shown): template <class T, int InterfaceId> class ComponentInterfaceReference { public: ComponentInterfaceReference(); /*implicit*/ ComponentInterfaceReference(const Component& comp); explicit ComponentInterfaceReference(const WeakReference& other); /*implicit*/ ComponentInterfaceReference(const StrongReference& other); template <class OtherT, int OtherInterfaceID> /*implicit*/ ComponentInterfaceReference(const ComponentInterfaceReference<OtherT, OtherInterfaceID>& other); template <class OtherT, int OtherInterfaceID> explicit ComponentInterfaceReference(const ComponentInterfaceWeakReference<OtherT, OtherInterfaceID>& other); [...] const Component& component() const; const StrongReference& ref() const; T* operator->() const; T* get() const; }; >From this interface definition, several types are defined, e.g.: [...] class MeshInterface; typedef ComponentInterfaceReference<MeshInterface, eRawMeshInterfaceId> RawMeshRef; typedef ComponentInterfaceReference<MeshInterface, eMeshInterfaceId> MeshRef; [...] typedef LWSDK::ComponentInterfaceReference<SceneItem_Interface, LWSDK::eSceneItemInterfaceId> SceneItemRef; As you can see in the interface definition, it has parameterized constructors defined. This means that performing something like this in C++ at compile time: void func(const Asgard::SceneItemRef& ref) { MeshRef m(ref); [...] works just fine. The appropriate parameterized constructor is constructed for that type ('SceneItemRef') when the code is compiled. Unfortunately, SWIG does not generate types for the parameterized constructors. It will generate code to convert to one of the first three "concrete" constructor types (Component, WeakReference, and StrongReference), and it will wrap the "Asgard::SceneItemRef" type correctly, because I have instantiated the template in the SWIG interface file, e.g.: %template(MeshRef) LWSDK::ComponentInterfaceReference<LWSDK::MeshInterface, eMeshInterfaceId>; [...] %template(SceneItemRef) LWSDK::ComponentInterfaceReference<Asgard::SceneItem_Interface, LWSDK::eSceneItemInterfaceId>; However, it fails in the following code, because the conversion (from "Asgard::SceneItemRef&" to one of the concrete types) fails: if (!PyTuple_Check(args)) SWIG_fail; argc = (int)PyObject_Length(args); for (ii = 0; (ii < argc) && (ii < 1); ii++) { argv[ii] = PyTuple_GET_ITEM(args,ii); } if (argc == 0) { return _wrap_new_MeshRef__SWIG_0(self, args); } if (argc == 1) { int _v; int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_LWSDK__Component, 0); _v = SWIG_CheckState(res); if (_v) { return _wrap_new_MeshRef__SWIG_1(self, args); } } if (argc == 1) { int _v; int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_LWSDK__WeakReference, 0); _v = SWIG_CheckState(res); if (_v) { return _wrap_new_MeshRef__SWIG_2(self, args); } } if (argc == 1) { int _v; int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_LWSDK__StrongReference, 0); _v = SWIG_CheckState(res); if (_v) { return _wrap_new_MeshRef__SWIG_3(self, args); } } I have not had much success understanding how to wrap this using SWIG. I thought about a typemap that would convert the "Asgard::SceneItemRef const &" reference into one of the implicit "concrete" types that the interface takes in a constructor (a Component or a StrongReference) using one of the available methods in the ComponentInterfaceReference, but I cannot seem to get the typemap to...well, do the right thing. I was wondering if any of you sage SWIG gurus could provide me with some insight, direction, or actual code to get me moving forward? :) btw, I did not design this interface with the parameterized constructors, I'm just the poor fellow who's tasked with exporting it via SWIG. So, I can try to provide more information, or answer any questions you may have, as best I can. Render me gone, |||
Bob ^(===)^
---------------------------------oOO--(_)--OOo---------------------------------
The world needs more cults. They attract stupidity like a
magnet attracts metal, and they're self cleaning.
------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Need some help with parameterized constructors and SWIGBob Hood wrote:
> I'm in need of some expert SWIG help here. This is C++ <-> Python, btw, > using SWIG 1.3.36. > > I have the following C++ interface definition (relevant pieces shown): > > template <class T, int InterfaceId> > class ComponentInterfaceReference > { > public: > ComponentInterfaceReference(); > /*implicit*/ ComponentInterfaceReference(const Component& comp); > explicit ComponentInterfaceReference(const WeakReference& other); > /*implicit*/ ComponentInterfaceReference(const StrongReference& > other); > > template <class OtherT, int OtherInterfaceID> > /*implicit*/ ComponentInterfaceReference(const > ComponentInterfaceReference<OtherT, OtherInterfaceID>& other); > > template <class OtherT, int OtherInterfaceID> > explicit ComponentInterfaceReference(const > ComponentInterfaceWeakReference<OtherT, OtherInterfaceID>& other); > > [...] > > const Component& component() const; > const StrongReference& ref() const; > > T* operator->() const; > T* get() const; > }; > > >From this interface definition, several types are defined, e.g.: > > [...] > class MeshInterface; > typedef ComponentInterfaceReference<MeshInterface, > eRawMeshInterfaceId> RawMeshRef; > typedef ComponentInterfaceReference<MeshInterface, eMeshInterfaceId> > MeshRef; > [...] > typedef LWSDK::ComponentInterfaceReference<SceneItem_Interface, > LWSDK::eSceneItemInterfaceId> SceneItemRef; > > As you can see in the interface definition, it has parameterized > constructors defined. This means that performing something like this in > C++ at compile time: > > void func(const Asgard::SceneItemRef& ref) > { > MeshRef m(ref); > [...] > > works just fine. The appropriate parameterized constructor is > constructed for that type ('SceneItemRef') when the code is compiled. > > Unfortunately, SWIG does not generate types for the parameterized > constructors. It will generate code to convert to one of the first > three "concrete" constructor types (Component, WeakReference, and > StrongReference), and it will wrap the "Asgard::SceneItemRef" type > correctly, because I have instantiated the template in the SWIG > interface file, e.g.: > > %template(MeshRef) > LWSDK::ComponentInterfaceReference<LWSDK::MeshInterface, eMeshInterfaceId>; > [...] > %template(SceneItemRef) > LWSDK::ComponentInterfaceReference<Asgard::SceneItem_Interface, > LWSDK::eSceneItemInterfaceId>; > > However, it fails in the following code, because the conversion (from > "Asgard::SceneItemRef&" to one of the concrete types) fails: > > if (!PyTuple_Check(args)) SWIG_fail; > argc = (int)PyObject_Length(args); > for (ii = 0; (ii < argc) && (ii < 1); ii++) { > argv[ii] = PyTuple_GET_ITEM(args,ii); > } > if (argc == 0) { > return _wrap_new_MeshRef__SWIG_0(self, args); > } > if (argc == 1) { > int _v; > int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_LWSDK__Component, 0); > _v = SWIG_CheckState(res); > if (_v) { > return _wrap_new_MeshRef__SWIG_1(self, args); > } > } > if (argc == 1) { > int _v; > int res = SWIG_ConvertPtr(argv[0], 0, > SWIGTYPE_p_LWSDK__WeakReference, 0); > _v = SWIG_CheckState(res); > if (_v) { > return _wrap_new_MeshRef__SWIG_2(self, args); > } > } > if (argc == 1) { > int _v; > int res = SWIG_ConvertPtr(argv[0], 0, > SWIGTYPE_p_LWSDK__StrongReference, 0); > _v = SWIG_CheckState(res); > if (_v) { > return _wrap_new_MeshRef__SWIG_3(self, args); > } > } > > I have not had much success understanding how to wrap this using SWIG. > I thought about a typemap that would convert the "Asgard::SceneItemRef > const &" reference into one of the implicit "concrete" types that the > interface takes in a constructor (a Component or a StrongReference) > using one of the available methods in the ComponentInterfaceReference, > but I cannot seem to get the typemap to...well, do the right thing. I > was wondering if any of you sage SWIG gurus could provide me with some > insight, direction, or actual code to get me moving forward? :) > > btw, I did not design this interface with the parameterized > constructors, I'm just the poor fellow who's tasked with exporting it > via SWIG. So, I can try to provide more information, or answer any > questions you may have, as best I can. > I got the following to work. The important part is in the %extend. It is manually adding in the constructor that is already there. This is more of a workaround than a solution to templated constructors. %inline %{ namespace Asgard { struct SceneItem_Interface {}; } const int eMeshInterfaceId = 2; namespace LWSDK { struct MeshInterface {}; struct WeakReference {}; struct Component {}; struct StrongReference {}; const int eSceneItemInterfaceId = 1; template <class T, int InterfaceId> struct ComponentInterfaceWeakReference {}; template <class T, int InterfaceId> class ComponentInterfaceReference { public: ComponentInterfaceReference() {} /*implicit*/ ComponentInterfaceReference(const Component& comp) {} explicit ComponentInterfaceReference(const WeakReference& other) {} /*implicit*/ ComponentInterfaceReference(const StrongReference& other) {} template <class OtherT, int OtherInterfaceID> /*implicit*/ ComponentInterfaceReference(const ComponentInterfaceReference<OtherT, OtherInterfaceID>& other) {} template <class OtherT, int OtherInterfaceID> explicit ComponentInterfaceReference(const ComponentInterfaceWeakReference<OtherT, OtherInterfaceID>& other) {} /* [...] const Component& component() const; const StrongReference& ref() const; */ T* operator->() const { return 0; } T* get() const { return 0; } }; typedef ComponentInterfaceReference<MeshInterface, eMeshInterfaceId> MeshRef; /* class MeshInterface; typedef ComponentInterfaceReference<MeshInterface, eRawMeshInterfaceId> RawMeshRef; [...] typedef LWSDK::ComponentInterfaceReference<SceneItem_Interface, LWSDK::eSceneItemInterfaceId> SceneItemRef; */ } %} namespace LWSDK { %extend ComponentInterfaceReference { ComponentInterfaceReference(MeshRef o) { return new LWSDK::ComponentInterfaceReference<T, InterfaceId>(o); } } } %template(MeshRef) LWSDK::ComponentInterfaceReference<LWSDK::MeshInterface, eMeshInterfaceId>; %template(SceneItemRef) LWSDK::ComponentInterfaceReference<Asgard::SceneItem_Interface, LWSDK::eSceneItemInterfaceId>; There is some limited buggy support for %template within %extend, but you'll have to persuade me to fix this as fixing the template handling isn't easy! William ------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Need some help with parameterized constructors and SWIGWilliam S Fulton wrote:
> Bob Hood wrote: >> I'm in need of some expert SWIG help here. This is C++ <-> Python, >> btw, using SWIG 1.3.36. >> >> I have the following C++ interface definition (relevant pieces shown): >> >> template <class T, int InterfaceId> >> class ComponentInterfaceReference >> { >> public: >> ComponentInterfaceReference(); >> /*implicit*/ ComponentInterfaceReference(const Component& comp); >> explicit ComponentInterfaceReference(const WeakReference& >> other); >> /*implicit*/ ComponentInterfaceReference(const >> StrongReference& other); >> >> template <class OtherT, int OtherInterfaceID> >> /*implicit*/ ComponentInterfaceReference(const >> ComponentInterfaceReference<OtherT, OtherInterfaceID>& other); >> >> template <class OtherT, int OtherInterfaceID> >> explicit ComponentInterfaceReference(const >> ComponentInterfaceWeakReference<OtherT, OtherInterfaceID>& other); >> >> [...] >> >> const Component& component() const; >> const StrongReference& ref() const; >> >> T* operator->() const; >> T* get() const; >> }; >> >From this interface definition, several types are defined, e.g.: >> >> [...] >> class MeshInterface; >> typedef ComponentInterfaceReference<MeshInterface, >> eRawMeshInterfaceId> RawMeshRef; >> typedef ComponentInterfaceReference<MeshInterface, >> eMeshInterfaceId> MeshRef; >> [...] >> typedef LWSDK::ComponentInterfaceReference<SceneItem_Interface, >> LWSDK::eSceneItemInterfaceId> SceneItemRef; >> >> As you can see in the interface definition, it has parameterized >> constructors defined. This means that performing something like this >> in C++ at compile time: >> >> void func(const Asgard::SceneItemRef& ref) >> { >> MeshRef m(ref); >> [...] >> >> works just fine. The appropriate parameterized constructor is >> constructed for that type ('SceneItemRef') when the code is compiled. >> >> Unfortunately, SWIG does not generate types for the parameterized >> constructors. It will generate code to convert to one of the first >> three "concrete" constructor types (Component, WeakReference, and >> StrongReference), and it will wrap the "Asgard::SceneItemRef" type >> correctly, because I have instantiated the template in the SWIG >> interface file, e.g.: >> >> %template(MeshRef) >> LWSDK::ComponentInterfaceReference<LWSDK::MeshInterface, >> eMeshInterfaceId>; >> [...] >> %template(SceneItemRef) >> LWSDK::ComponentInterfaceReference<Asgard::SceneItem_Interface, >> LWSDK::eSceneItemInterfaceId>; >> >> However, it fails in the following code, because the conversion (from >> "Asgard::SceneItemRef&" to one of the concrete types) fails: >> >> if (!PyTuple_Check(args)) SWIG_fail; >> argc = (int)PyObject_Length(args); >> for (ii = 0; (ii < argc) && (ii < 1); ii++) { >> argv[ii] = PyTuple_GET_ITEM(args,ii); >> } >> if (argc == 0) { >> return _wrap_new_MeshRef__SWIG_0(self, args); >> } >> if (argc == 1) { >> int _v; >> int res = SWIG_ConvertPtr(argv[0], 0, >> SWIGTYPE_p_LWSDK__Component, 0); >> _v = SWIG_CheckState(res); >> if (_v) { >> return _wrap_new_MeshRef__SWIG_1(self, args); >> } >> } >> if (argc == 1) { >> int _v; >> int res = SWIG_ConvertPtr(argv[0], 0, >> SWIGTYPE_p_LWSDK__WeakReference, 0); >> _v = SWIG_CheckState(res); >> if (_v) { >> return _wrap_new_MeshRef__SWIG_2(self, args); >> } >> } >> if (argc == 1) { >> int _v; >> int res = SWIG_ConvertPtr(argv[0], 0, >> SWIGTYPE_p_LWSDK__StrongReference, 0); >> _v = SWIG_CheckState(res); >> if (_v) { >> return _wrap_new_MeshRef__SWIG_3(self, args); >> } >> } >> >> I have not had much success understanding how to wrap this using >> SWIG. I thought about a typemap that would convert the >> "Asgard::SceneItemRef const &" reference into one of the implicit >> "concrete" types that the interface takes in a constructor (a >> Component or a StrongReference) using one of the available methods in >> the ComponentInterfaceReference, but I cannot seem to get the typemap >> to...well, do the right thing. I was wondering if any of you sage >> SWIG gurus could provide me with some insight, direction, or actual >> code to get me moving forward? :) >> >> btw, I did not design this interface with the parameterized >> constructors, I'm just the poor fellow who's tasked with exporting it >> via SWIG. So, I can try to provide more information, or answer any >> questions you may have, as best I can. >> > > I got the following to work. The important part is in the %extend. It > is manually adding in the constructor that is already there. This is > more of a workaround than a solution to templated constructors. > > %inline %{ > > namespace Asgard { > struct SceneItem_Interface {}; > } > const int eMeshInterfaceId = 2; > > namespace LWSDK { > > struct MeshInterface {}; > struct WeakReference {}; > struct Component {}; > struct StrongReference {}; > const int eSceneItemInterfaceId = 1; > > template <class T, int InterfaceId> struct > ComponentInterfaceWeakReference {}; > > template <class T, int InterfaceId> > class ComponentInterfaceReference > { > public: > ComponentInterfaceReference() {} > /*implicit*/ ComponentInterfaceReference(const Component& > comp) {} > explicit ComponentInterfaceReference(const WeakReference& > other) {} > /*implicit*/ ComponentInterfaceReference(const > StrongReference& other) {} > > template <class OtherT, int OtherInterfaceID> > /*implicit*/ ComponentInterfaceReference(const > ComponentInterfaceReference<OtherT, OtherInterfaceID>& other) {} > > template <class OtherT, int OtherInterfaceID> > explicit ComponentInterfaceReference(const > ComponentInterfaceWeakReference<OtherT, OtherInterfaceID>& other) {} > > /* > [...] > > const Component& component() const; > const StrongReference& ref() const; > */ > > T* operator->() const { return 0; } > T* get() const { return 0; } > }; > > typedef ComponentInterfaceReference<MeshInterface, > eMeshInterfaceId> MeshRef; > /* > class MeshInterface; > typedef ComponentInterfaceReference<MeshInterface, > eRawMeshInterfaceId> RawMeshRef; > [...] > typedef LWSDK::ComponentInterfaceReference<SceneItem_Interface, > LWSDK::eSceneItemInterfaceId> SceneItemRef; > */ > > } > > %} > > namespace LWSDK { > %extend ComponentInterfaceReference { > ComponentInterfaceReference(MeshRef o) { > return new LWSDK::ComponentInterfaceReference<T, InterfaceId>(o); > } > } > } > > %template(MeshRef) > LWSDK::ComponentInterfaceReference<LWSDK::MeshInterface, > eMeshInterfaceId>; > > %template(SceneItemRef) > LWSDK::ComponentInterfaceReference<Asgard::SceneItem_Interface, > LWSDK::eSceneItemInterfaceId>; > > There is some limited buggy support for %template within %extend, but > you'll have to persuade me to fix this as fixing the template handling > isn't easy! > Understood. Many thanks, William, for taking the time with this one. I think I might take the time to lobby for changing the interfaces. I'm not sure I want to maintain this level of wrapping overhead, especially if it contains any hidden surprises. Render me gone, ||| Bob ^(===)^ ---------------------------------oOO--(_)--OOo--------------------------------- The world needs more cults. They attract stupidity like a magnet attracts metal, and they're self cleaning. ------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
| Free embeddable forum powered by Nabble | Forum Help |