Assigning shared pointer reference from C#

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Assigning shared pointer reference from C#


Hi all,


I’m trying to initialize, in C++,  a reference to a boost shared pointer passed from C#, while trying to maintain the inheritance tree.


Here’s my C++ test case (in file SwigTest.h) :


#include <string>

#include <boost/shared_ptr.hpp>


template<class T>

using SRef = boost::shared_ptr<T>;


class FactoryClass



                FactoryClass() {}

                ~FactoryClass() {}


                //Normally I would only like to use the return value, but SWIG uses polymorphism to simulate templates

//and polymorphism on the return type only is not supported by C#. (I get a Identifier 'getReference' redefined error on SwigTest.i compilation).

                //So I thought I’d pass the SRef& as a parameter instead of having no parameters at all.

                template <class  T>

                static SRef<T> getReference( SRef<T>& inout ) { inout = SRef<T>( new T(1) ); return inout; }


                void testing( int& toto ) {};



class BaseClass



                BaseClass() { m_flag = -1; };

                BaseClass(int i) { m_flag = i; };

                virtual ~BaseClass() {};


                virtual std::string getName() = 0;


                int m_flag;



class FirstClass : public BaseClass



                FirstClass() {};

                FirstClass(int i) : BaseClass( i ) {};

                virtual ~FirstClass() {};


                virtual std::string getName() { return "First Class"; };



class SecondClass : public BaseClass



                SecondClass() {};

                SecondClass(int i) : BaseClass( i ) {};

                virtual ~SecondClass() {};


                virtual std::string getName() { return "Second Class"; };




Here’s my current SWIG interface file :


%module SwigTest

%include "std_string.i"

%include "boost_shared_ptr.i"



#include "../include/SwigTest.h"



%shared_ptr( BaseClass );

%shared_ptr( FirstClass );

%shared_ptr( SecondClass );


%include "../include/SwigTest.h"


%template(BoostBasePtr) boost::shared_ptr<BaseClass>;

%template(SharedBase) SRef<BaseClass>;


%template(BoostFirstPtr) boost::shared_ptr<FirstClass>;




%template(BoostSecondPtr) boost::shared_ptr<SecondClass>;






And here’s what works and doesn’t in C# :


public class SwigInterface


private FirstClass m_first;

private SecondClass m_second = new SecondClass(-2);


void TestSwig ()


m_first = FactoryClass.getReference( m_first ); //The returned reference is copied from C++

                                FactoryClass.getReference( m_second ); //m_second is never changed


                                FirstClass anotherFirst = new FirstClass(-1) ;

BaseClass bc = FactoryClass.getReference( anotherFirst); //bc does equal the new instance of FirstClass but anotherFirst is not correctly replaced


//This is the case I would ideally want to use

SecondClass nothing ;

FactoryClass.getReference( nothing ) ; //NullReferenceException (because the SWIG generated interface uses nothing.CPtr() to pass by pointer as it does with all refs).





I tried a lot of %typename gymnastics to replace the signature of FactoryClass.getReference( FirstClass ) with FactoryClass.getReference( out FirstClass ) (or even ref FirstClass) but I can’t find a way to do it properly.


Any help, feedback, or even confirmation that this is not possible, is welcome.


Thank you,




Check out the vibrant tech community on one of the world's most
engaging tech sites,!
Swig-user mailing list
[hidden email]