From: David P. <dpi...@me...> - 2012-05-18 22:13:21
|
Well, I don't appreciate that you sent this to me instead of to the mailing list, but I investigated anyway... The problem is that %template has very specific requirements (and of course, that when you give SWIG code that it does not accept, it often does not give you any error or warning message, unfortunately; in this case %template does not work but it does not complain either.) Your %template command must come after the classes mentioned in the command have been defined, but before the template is used as a base class. In your case the %template command must therefore go between the definition of CustomItem and CustomList. Like so: template <class T, class T2> class MyArray { public: MyArray() {} ~MyArray() {} // nested class omitted. it is not the cause of the error. int Run() { return 100; } bool IsEmpty() { return true; } }; namespace AK { namespace SoundEngine { namespace DynamicDialogue { class CustomItem { public: CustomItem(); ~CustomItem(); void Run() { printf("Hello CustomItem!\n"); } }; } } } #ifdef SWIG %template(CustomArray) MyArray<AK::SoundEngine::DynamicDialogue::CustomItem,AK::SoundEngine::DynamicDialogue::CustomItem const &>; #endif namespace AK { namespace SoundEngine { namespace DynamicDialogue { template <class T, customanclass T2> class CustomList : public MyArray<CustomItem, const CustomItem &> { public: int MyRun() { return 999; } }; } } } From: Build Master [mailto:akb...@gm...] Sent: Friday, May 18, 2012 3:31 PM To: David Piepgrass Subject: Re: [Swig-user] Updates on Template with nested structure I attached an example. Say on Windows, put the two files under same folder, e.g., "C:\MyTest" and assume SWIG folder is at "C:\MyTest\SWIG" Run this command: SWIG\swig.exe -oMyWrapper_wrap.cxx -c++ -csharp hello_template.swig You will see the errors that I posted. If you comment out all the namespaces in both the .swig file and .h file, the example works perfectly. K On Fri, May 18, 2012 at 4:56 PM, David Piepgrass <dpi...@me...<mailto:dpi...@me...>> wrote: What namespace is MyArray in? Try qualifying with that namespace (or ::MyArray if no namespace). Also, I noticed the args were "aCustomType const &" in one place and "const aCustomType &" in another... better make it the same in both places, just in case. If you need further help, I suggest you reduce the problem to a simple, complete, compilable example that exhibits the problem. From: Build Master [mailto:akb...@gm...<mailto:akb...@gm...>] Sent: Friday, May 18, 2012 2:44 PM To: swi...@li...<mailto:swi...@li...> Subject: [Swig-user] Updates on Template with nested structure Hello, Here is an update about my earlier post today (see below the original) I've narrowed down the problem. My original problem was that the generated C# subclass CustomList turned out to be inherited from IDisposal (default) instead of its true parent MyArray. I found that this has nothing to do with the nested Iterator struct, but with the fact that the C++ class CustomList was defined in nested namespaces like this namespace Name1 { namespace Name2 { class CustomList : public MyArray<aCustomType, const aCustomType&, anotherCustomType, 4> { .... } } } With the namespace, I got these errors > 1>HeaderBlob.h(3397) : Warning 401: Nothing known about base class > 'MyArray< aCustomType, aCustomType const &, anotherCustomType, 4 >'. > Ignored. > 1>HeaderBlob.h(3397) : Warning 401: Maybe you forgot to instantiate > 'MyArray< aCustomType, aCustomType const &, anotherCustomType,4 >' > using %template. > Even after defined this class with namespaces in my SWIG interface like this: namespace AK { namespace SoundEngine { namespace DynamicDialogue { %template(AkPlaylist) Playlist<PlaylistItem, PlaylistItem const &, ArrayPoolDefault, 4>; } } } I still got the same errors. Any help would be appreciated. K ====================== Original question ====================== Hello, I'm generating C# bindings for C++ that need to expose templates. Now I'm having errors related to nested structures in template class. I read from SWIG doc that SWIG has only limited support to nested structures. I now have to expose a template class that contains a nest struct, something looking like this // MyArray.h template <class T, class T1, class T2, unsigned long P> class MyArray { public: MyArray() ....{} struct Iterator { T* pItem; Iterator& operator++() { ... } Iterator& operator--() { ... } ..... } Iterator Begin() const { ... } Iterator End() const { ... } ... } Then in my interface I have this statement based on this class: > %include <MyArray.h> > %template(CustomArray) MyArray<aCustomType, aCustomType const &, > anotherCustomType, 4>; > > %include <HeaderBlob.h> > In HeaderBlob.h, I have classes that looks like: > class CustomList : public MyArray<aCustomType, const aCustomType&, > anotherCustomType, 4> > When using SWIG to generate bindings, I'm getting these errors: 1>MyArray.h(84) : Warning 325: Nested struct not currently supported > (Iterator ignored) > 1>HeaderBlob.h(3397) : Warning 401: Nothing known about base class > 'MyArray< aCustomType, aCustomType const &, anotherCustomType, 4 >'. > Ignored. > 1>HeaderBlob.h(3397) : Warning 401: Maybe you forgot to instantiate > 'MyArray< aCustomType, aCustomType const &, anotherCustomType,4 >' > using %template. > SWIG generates CustomList, aCustomType C# class and CustomArray C# class that have no connections. Also two orphan types are generated > > SWIGTYPE_p_MyArrayT_aCustomType_aCustomType_const_R_anotherCustomeType > _4_t__Iteratorcs > SWIGTYPE_p_aCustomType.cs > I wonder if this is due to the nested Iterator struct, or the fact that I'm missing some required typemaps or other SWIG constructs?? Regards, K |