Director Pass by value

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Director Pass by value

Andrew Haining

Hi, I’ve been trying to solve this problem for a while now and have run out of ideas.

 

What I would like to be able to do is pass an object, by value, to the managed language (java & c#) director class. Allowing the garbage collector to take control of a copy of the object that was passed to it. This would mean it would be impossible for the managed code to be working on a dangling pointer.

 

Here is an example of what I mean:

 

 

---DirectorByValue.h---

class Director;

 

class Object

{

                private:

                Director *m_director;

                public:

                void SetDirector(Director *director);      

                void CallDirector();

};

 

%feature("director") Director;

class Director

{

                public:

                virtual void PassByValue(Object object) = 0;

};

 

---DirectorByValue.cpp---

#include "DirectorByValue.h"

void Object::SetDirector(Director *director)

{

                m_director = director;

}

 

void Object::CallDirector()

{

                m_director->PassByValue(*this);

}

 

---DirectorByValue.i---

%module(directors="1") DirectorByValue

%include "DirectorByValue.h"

 

%{

#include "DirectorByValue.h"

%}

 

 

This produces the following relevant code in C#:

 

--- generated C++ snippet ---

SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void * jarg2) {

  Director *arg1 = (Director *) 0 ;

  Object arg2 ;

  Object *argp2 ;

 

  arg1 = (Director *)jarg1;

  argp2 = (Object *)jarg2;

  if (!argp2) {

    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null Object", 0);

    return ;

  }

  arg2 = *argp2;

  (arg1)->PassByValue(arg2);

}

 

void SwigDirector_Director::PassByValue(Object object) {

  void * jarg0  ;

 

  if (!swig_callbackPassByValue) {

    throw Swig::DirectorPureVirtualException("Director::PassByValue");

  } else {

    jarg0 = (void *)&object;

    swig_callbackPassByValue(jarg0);

  }

}

 

--- generated C# snippet ---

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(arg0, false));

  }

 

Ideally what I would like to generate, is instead, this:

 

--- ideal C++ snippet ---

SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void * jarg2) {

  Director *arg1 = (Director *) 0 ;

  Object *arg2 ;

  Object *argp2 ;

 

  arg1 = (Director *)jarg1;

  argp2 = (Object *)jarg2;

  if (!argp2) {

    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null Object", 0);

    return ;

  }

  arg2 = new Object(*argp2);

  (arg1)->PassByValue(arg2);

}

 

void SwigDirector_Director::PassByValue(Object *object) {

  void * jarg0  ;

 

  if (!swig_callbackPassByValue) {

    throw Swig::DirectorPureVirtualException("Director::PassByValue");

  } else {

    jarg0 = (void *)object;

    swig_callbackPassByValue(jarg0);

  }

}

 

--- ideal C# snippet ---

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(arg0, true));

  }

 

I can solve this problem with the directorin typemap below but I feel the above is a truer representation of passing by value to Directors and was wondering if there was a way to achieve it.

%copyctor Object;

%typemap(csdirectorin,

         pre="Object managed_object = new Object($iminput);",

         post=""

         ) Object object "managed_object"

 



Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Director Pass by value

Andrew Haining

Incase anyone comes up against this in the future, this is what I came up with in the end –

 

#define %directorpassinvalue(Type) %typemap(csdirectorin) Type "new Type(new Type($iminput, false))"\

                                                                       %typemap(javadirectorin) Type "new Type(new Type($jniinput, false))"

 

Which means you can use

%directorpassinvalue(Object)

 

To change:

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(arg0, false));

  }

To:

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(new Object(arg0, false)));

  }

 

To have it invoke a copy constructor (requires Object to have %copyctor) rather than pass a reference to a temporary, I think it’s a fairly nice solution, any feedback or suggested improvements is welcome!

 

From: Andrew Haining [mailto:[hidden email]]
Sent: 28 July 2016 09:04
To: [hidden email]
Subject: [Swig-user] Director Pass by value

 

Hi, I’ve been trying to solve this problem for a while now and have run out of ideas.

 

What I would like to be able to do is pass an object, by value, to the managed language (java & c#) director class. Allowing the garbage collector to take control of a copy of the object that was passed to it. This would mean it would be impossible for the managed code to be working on a dangling pointer.

 

Here is an example of what I mean:

 

 

---DirectorByValue.h---

class Director;

 

class Object

{

                private:

                Director *m_director;

                public:

                void SetDirector(Director *director);      

                void CallDirector();

};

 

%feature("director") Director;

class Director

{

                public:

                virtual void PassByValue(Object object) = 0;

};

 

---DirectorByValue.cpp---

#include "DirectorByValue.h"

void Object::SetDirector(Director *director)

{

                m_director = director;

}

 

void Object::CallDirector()

{

                m_director->PassByValue(*this);

}

 

---DirectorByValue.i---

%module(directors="1") DirectorByValue

%include "DirectorByValue.h"

 

%{

#include "DirectorByValue.h"

%}

 

 

This produces the following relevant code in C#:

 

--- generated C++ snippet ---

SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void * jarg2) {

  Director *arg1 = (Director *) 0 ;

  Object arg2 ;

  Object *argp2 ;

 

  arg1 = (Director *)jarg1;

  argp2 = (Object *)jarg2;

  if (!argp2) {

    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null Object", 0);

    return ;

  }

  arg2 = *argp2;

  (arg1)->PassByValue(arg2);

}

 

void SwigDirector_Director::PassByValue(Object object) {

  void * jarg0  ;

 

  if (!swig_callbackPassByValue) {

    throw Swig::DirectorPureVirtualException("Director::PassByValue");

  } else {

    jarg0 = (void *)&object;

    swig_callbackPassByValue(jarg0);

  }

}

 

--- generated C# snippet ---

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(arg0, false));

  }

 

Ideally what I would like to generate, is instead, this:

 

--- ideal C++ snippet ---

SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void * jarg2) {

  Director *arg1 = (Director *) 0 ;

  Object *arg2 ;

  Object *argp2 ;

 

  arg1 = (Director *)jarg1;

  argp2 = (Object *)jarg2;

  if (!argp2) {

    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "Attempt to dereference null Object", 0);

    return ;

  }

  arg2 = new Object(*argp2);

  (arg1)->PassByValue(arg2);

}

 

void SwigDirector_Director::PassByValue(Object *object) {

  void * jarg0  ;

 

  if (!swig_callbackPassByValue) {

    throw Swig::DirectorPureVirtualException("Director::PassByValue");

  } else {

    jarg0 = (void *)object;

    swig_callbackPassByValue(jarg0);

  }

}

 

--- ideal C# snippet ---

  private void SwigDirectorPassByValue(global::System.IntPtr arg0) {

    PassByValue(new Object(arg0, true));

  }

 

I can solve this problem with the directorin typemap below but I feel the above is a truer representation of passing by value to Directors and was wondering if there was a way to achieve it.

%copyctor Object;

%typemap(csdirectorin,

         pre="Object managed_object = new Object($iminput);",

         post=""

         ) Object object "managed_object"

 



Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________


Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.



Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Director Pass by value

William S Fulton
FYI, swig-3.0.9 included a change to director arguments were copied by
value when wrapping class parameters by value, so your solution should
not be necessary any more. See https://github.com/swig/swig/pull/434.

William

On 28 July 2016 at 11:30, Andrew Haining
<[hidden email]> wrote:

> Incase anyone comes up against this in the future, this is what I came up
> with in the end –
>
>
>
> #define %directorpassinvalue(Type) %typemap(csdirectorin) Type "new Type(new
> Type($iminput, false))"\
>
>
> %typemap(javadirectorin) Type "new Type(new Type($jniinput, false))"
>
>
>
> Which means you can use
>
> %directorpassinvalue(Object)
>
>
>
> To change:
>
>   private void SwigDirectorPassByValue(global::System.IntPtr arg0) {
>
>     PassByValue(new Object(arg0, false));
>
>   }
>
> To:
>
>   private void SwigDirectorPassByValue(global::System.IntPtr arg0) {
>
>     PassByValue(new Object(new Object(arg0, false)));
>
>   }
>
>
>
> To have it invoke a copy constructor (requires Object to have %copyctor)
> rather than pass a reference to a temporary, I think it’s a fairly nice
> solution, any feedback or suggested improvements is welcome!
>
>
>
> From: Andrew Haining [mailto:[hidden email]]
> Sent: 28 July 2016 09:04
> To: [hidden email]
> Subject: [Swig-user] Director Pass by value
>
>
>
> Hi, I’ve been trying to solve this problem for a while now and have run out
> of ideas.
>
>
>
> What I would like to be able to do is pass an object, by value, to the
> managed language (java & c#) director class. Allowing the garbage collector
> to take control of a copy of the object that was passed to it. This would
> mean it would be impossible for the managed code to be working on a dangling
> pointer.
>
>
>
> Here is an example of what I mean:
>
>
>
>
>
> ---DirectorByValue.h---
>
> class Director;
>
>
>
> class Object
>
> {
>
>                 private:
>
>                 Director *m_director;
>
>                 public:
>
>                 void SetDirector(Director *director);
>
>                 void CallDirector();
>
> };
>
>
>
> %feature("director") Director;
>
> class Director
>
> {
>
>                 public:
>
>                 virtual void PassByValue(Object object) = 0;
>
> };
>
>
>
> ---DirectorByValue.cpp---
>
> #include "DirectorByValue.h"
>
> void Object::SetDirector(Director *director)
>
> {
>
>                 m_director = director;
>
> }
>
>
>
> void Object::CallDirector()
>
> {
>
>                 m_director->PassByValue(*this);
>
> }
>
>
>
> ---DirectorByValue.i---
>
> %module(directors="1") DirectorByValue
>
> %include "DirectorByValue.h"
>
>
>
> %{
>
> #include "DirectorByValue.h"
>
> %}
>
>
>
>
>
> This produces the following relevant code in C#:
>
>
>
> --- generated C++ snippet ---
>
> SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void *
> jarg2) {
>
>   Director *arg1 = (Director *) 0 ;
>
>   Object arg2 ;
>
>   Object *argp2 ;
>
>
>
>   arg1 = (Director *)jarg1;
>
>   argp2 = (Object *)jarg2;
>
>   if (!argp2) {
>
>     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
> "Attempt to dereference null Object", 0);
>
>     return ;
>
>   }
>
>   arg2 = *argp2;
>
>   (arg1)->PassByValue(arg2);
>
> }
>
>
>
> void SwigDirector_Director::PassByValue(Object object) {
>
>   void * jarg0  ;
>
>
>
>   if (!swig_callbackPassByValue) {
>
>     throw Swig::DirectorPureVirtualException("Director::PassByValue");
>
>   } else {
>
>     jarg0 = (void *)&object;
>
>     swig_callbackPassByValue(jarg0);
>
>   }
>
> }
>
>
>
> --- generated C# snippet ---
>
>   private void SwigDirectorPassByValue(global::System.IntPtr arg0) {
>
>     PassByValue(new Object(arg0, false));
>
>   }
>
>
>
> Ideally what I would like to generate, is instead, this:
>
>
>
> --- ideal C++ snippet ---
>
> SWIGEXPORT void SWIGSTDCALL CSharp_Director_PassByValue(void * jarg1, void *
> jarg2) {
>
>   Director *arg1 = (Director *) 0 ;
>
>   Object *arg2 ;
>
>   Object *argp2 ;
>
>
>
>   arg1 = (Director *)jarg1;
>
>   argp2 = (Object *)jarg2;
>
>   if (!argp2) {
>
>     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
> "Attempt to dereference null Object", 0);
>
>     return ;
>
>   }
>
>   arg2 = new Object(*argp2);
>
>   (arg1)->PassByValue(arg2);
>
> }
>
>
>
> void SwigDirector_Director::PassByValue(Object *object) {
>
>   void * jarg0  ;
>
>
>
>   if (!swig_callbackPassByValue) {
>
>     throw Swig::DirectorPureVirtualException("Director::PassByValue");
>
>   } else {
>
>     jarg0 = (void *)object;
>
>     swig_callbackPassByValue(jarg0);
>
>   }
>
> }
>
>
>
> --- ideal C# snippet ---
>
>   private void SwigDirectorPassByValue(global::System.IntPtr arg0) {
>
>     PassByValue(new Object(arg0, true));
>
>   }
>
>
>
> I can solve this problem with the directorin typemap below but I feel the
> above is a truer representation of passing by value to Directors and was
> wondering if there was a way to achieve it.
>
> %copyctor Object;
>
> %typemap(csdirectorin,
>
>          pre="Object managed_object = new Object($iminput);",
>
>          post=""
>
>          ) Object object "managed_object"
>
>
>
>
>
> Digital Barriers e-Mail Confidentiality and Disclaimer This message contains
> confidential information and is intended only for the individual named.
> Please notify the sender immediately by e-mail if you have received this
> e-mail by mistake and delete this e-mail from your system. E-mail
> transmission cannot be guaranteed to be secure or error-free as information
> could be intercepted, corrupted, lost, destroyed, arrive late or incomplete,
> or contain viruses. The sender therefore does not accept liability for any
> errors or omissions in the contents of this message, which arise as a result
> of e-mail transmission. The recipient should check this email and any
> attachments for the presence of viruses. The company accepts no liability
> for any damage caused by any virus transmitted by this email. Digital
> Barriers plc is a company registered in England and Wales. Registered
> number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1
> 9PG, United Kingdom. For further information about Digital Barriers, please
> visit www.digitalbarriers.com.
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________
>
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________
>
>
> Digital Barriers e-Mail Confidentiality and Disclaimer This message contains
> confidential information and is intended only for the individual named.
> Please notify the sender immediately by e-mail if you have received this
> e-mail by mistake and delete this e-mail from your system. E-mail
> transmission cannot be guaranteed to be secure or error-free as information
> could be intercepted, corrupted, lost, destroyed, arrive late or incomplete,
> or contain viruses. The sender therefore does not accept liability for any
> errors or omissions in the contents of this message, which arise as a result
> of e-mail transmission. The recipient should check this email and any
> attachments for the presence of viruses. The company accepts no liability
> for any damage caused by any virus transmitted by this email. Digital
> Barriers plc is a company registered in England and Wales. Registered
> number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1
> 9PG, United Kingdom. For further information about Digital Barriers, please
> visit www.digitalbarriers.com.
>
>
>
> Digital Barriers e-Mail Confidentiality and Disclaimer This message contains
> confidential information and is intended only for the individual named.
> Please notify the sender immediately by e-mail if you have received this
> e-mail by mistake and delete this e-mail from your system. E-mail
> transmission cannot be guaranteed to be secure or error-free as information
> could be intercepted, corrupted, lost, destroyed, arrive late or incomplete,
> or contain viruses. The sender therefore does not accept liability for any
> errors or omissions in the contents of this message, which arise as a result
> of e-mail transmission. The recipient should check this email and any
> attachments for the presence of viruses. The company accepts no liability
> for any damage caused by any virus transmitted by this email. Digital
> Barriers plc is a company registered in England and Wales. Registered
> number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1
> 9PG, United Kingdom. For further information about Digital Barriers, please
> visit www.digitalbarriers.com.
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. http://sdm.link/zohodev2dev
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user