From: William S F. <ws...@fu...> - 2009-11-25 01:15:34
|
Anton Lauridsen wrote: > Hi > > I thought I was pretty good at c++, but I've stumpled upon something I > don't quite understand, first a little back-ground: > > I'm trying to revive the Swig bindings for the Ogre3d rendering engine, > things are progressing nicely, but, I've stumpled upon a problem: > > The Ogre3D library comes with the ability to track memory allocations, > and report leaks when exiting the program, naturally I'd like to use > that feature in order to ensure that the wrappers doesn't leak. > > for stl collections, this is done by wrapping the stl collections in a > template class, I've provided a small sample here: > > //for stl containter > namespace Ogre > { > template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > struct deque > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::deque<T, A> type; > #else > typedef typename std::deque<T> type; > #endif > }; > > template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > struct vector > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::vector<T, A> type; > #else > typedef typename std::vector<T> type; > #endif > }; > > template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > struct list > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::list<T, A> type; > #else > typedef typename std::list<T> type; > #endif > }; > > template <typename T, typename P = std::less<T>, typename A = > STLAllocator<T, GeneralAllocPolicy> > > struct set > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::set<T, P, A> type; > #else > typedef typename std::set<T, P> type; > #endif > }; > > template <typename K, typename V, typename P = std::less<K>, typename A > = STLAllocator<std::pair<const K, V>, GeneralAllocPolicy> > > struct map > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::map<K, V, P, A> type; > #else > typedef typename std::map<K, V, P> type; > #endif > }; > > template <typename K, typename V, typename P = std::less<K>, typename A > = STLAllocator<std::pair<const K, V>, GeneralAllocPolicy> > > struct multimap > { > #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR > typedef typename std::multimap<K, V, P, A> type; > #else > typedef typename std::multimap<K, V, P> type; > #endif > }; > > } // Ogre > > > The STLAllocator<....> has some defines, taking care of the different > allocation scenarios, i.e. whether memory tracking is enabled or not. > > here comes the part which I don't really understand: > > typedef map<String, String>::type NameValuePairList; > > I didn't even know that "::type" was legal c++ > It is just a typedef of another type. You see this a lot in the STL, for example the containers have value_type, pointer, const_reference etc. > I tried to wrap it using: > %template(NameValuePairList) Ogre::map<Ogre::String, > Ogre::String>::type; > > but Swig doesn't like the "::type" either, if I leave it out, > The syntax for %template must match that of a template, so a typedef to a template does not work. This is a limitation, but possibly this can be improved in the future. So for now you have to provide the exact template instantiation: #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR %template(NameValuePairList) std::map<K, V, P, A>; #else %template(NameValuePairList) std::map<K, V, P>; #endif or you could probably do something with the preprocessor to make this less tedious for all your templates: #if OGRE_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR # define OGRE_ALLOCATORS ,A #else # define OGRE_ALLOCATORS #endif %template(NameValuePairList) std::map<K, V, P OGRE_ALLOCATORS >; > void f(const Ogre::NameValuePairList *p); > > gets wrapped as: > > public void > f(SWIGTYPE_p_Ogre__mapT_Ogre__String_Ogre__String_std__lessT_Ogre__String_t_STLAllocatorT_std__pairT_Ogre__String_const_Ogre__String_t_Ogre__GeneralAllocPolicy_t_t__type p) > > I'd prefer the signature to be: > public void f(NameValuePairList p); > > Is there a simple way to achieve this or do I need to manually define > the wrappers? > > Any help would be greatly appreciated, especially this doing this > manually would be quite a daunting task, given all the places that > collections are used. Hope that helps. William |