Re: %newobject and typemap(out)

"Langer, Stephen A. \(Fed\) via Swig-user" <[email protected]> Fri, 8 Jul 2022 00:25:38 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
You're right.  For some reason it seemed not to work when I first tried it.  I must have been doing something wrong.  Thanks!

-- Steve

From: "Cherry, Josh (NIH/NLM/NCBI) [E]" <[email protected]>
Date: Thursday, July 7, 2022 at 4:25 PM
To: "[email protected]" <[email protected]>, "Langer, Stephen A. (Fed)" <[email protected]>
Subject: Re: [Swig-user] %newobject and typemap(out)

Might a "newfree" typemap get you what you want?  I would think so from the std::string example here:

https://www.swig.org/Doc4.0/Typemaps.html#Typemaps_nn34<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.swig.org%2FDoc4.0%2FTypemaps.html%23Typemaps_nn34&data=05%7C01%7Cstephen.langer%40nist.gov%7Cc1ab6dc5460443983e6808da6056caec%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637928223269523285%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=HYcjkd33mNtwbGCBc5fuvo0iNSm5xR2vdGlszL%2FBu%2FA%3D&reserved=0>


________________________________
From: Langer, Stephen A. (Fed) via Swig-user <[email protected]>
Sent: Thursday, July 7, 2022 3:14 PM
To: [email protected] <[email protected]>
Subject: [EXTERNAL] Re: [Swig-user] %newobject and typemap(out)

I understand why what I expected to happen doesn’t happen, so I'll rephrase the question:  Is there a way to write a typemap(out) so that it either deletes its source or doesn't, depending on a flag in the swig file that indicates whether or not the object was newly allocated before being returned?

To be specific, I'm trying to do something like this:

%typemap(out) MyClass * {
  $result = some_operations_on($1);
  if(newly_allocated)  // <<-- how???
    delete $1;
}

so that I can do this:

MyClass *getOldObject();
%newobject getNewObject;
MyClass *getNewObject();

or something along those lines.

I know that I can write two typemaps, one for MyClass and one for NewMyClass, put the delete statement only in the NewMyClass typemap, and typedef NewMyClass to MyClass.  Is there a better way?

Thanks,

 -- Steve



-----Original Message-----
From: "Langer, Stephen A. (Fed) via Swig-user" <[email protected]>
Reply-To: "Langer, Stephen A. (Fed)" <[email protected]>
Date: Thursday, July 7, 2022 at 10:20 AM
To: "[email protected]" <[email protected]>
Subject: [Swig-user] %newobject and typemap(out)

    Hi --

    Is it possible for a typemap to know whether or not it's being invoked in conjunction with %newobject?

    If %newobject is used on a function where the return value has a typemap(newfree) but no typemap(out), the wrapper will correctly delete the return value.  But if there's a typemap(out), newfree doesn't seem to be used.  How can a typemap(out) know whether or not it should delete the return value?

    Thanks.
     -- Steve



    _______________________________________________
    Swig-user mailing list
    [email protected]
    https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fswig-user&amp;data=05%7C01%7Cjcherry%40ncbi.nlm.nih.gov%7C7a5f2917d893469ddfba08da604d1cb9%7C14b77578977342d58507251ca2dc2b06%7C0%7C0%7C637928181556625188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=yJa82ISBB1VHdL2ivrzKCzC2uYWtCTygDdJ4iQt0SAU%3D&amp;reserved=0<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fswig-user&data=05%7C01%7Cstephen.langer%40nist.gov%7Cc1ab6dc5460443983e6808da6056caec%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637928223269523285%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=w1p2lC25JVvpkZ3kGTRt0X4tH8j9dwvxTfqayz%2BPIkM%3D&reserved=0>


_______________________________________________
Swig-user mailing list
[email protected]
https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fswig-user&amp;data=05%7C01%7Cjcherry%40ncbi.nlm.nih.gov%7C7a5f2917d893469ddfba08da604d1cb9%7C14b77578977342d58507251ca2dc2b06%7C0%7C0%7C637928181556625188%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=yJa82ISBB1VHdL2ivrzKCzC2uYWtCTygDdJ4iQt0SAU%3D&amp;reserved=0<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.sourceforge.net%2Flists%2Flistinfo%2Fswig-user&data=05%7C01%7Cstephen.langer%40nist.gov%7Cc1ab6dc5460443983e6808da6056caec%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637928223269523285%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=w1p2lC25JVvpkZ3kGTRt0X4tH8j9dwvxTfqayz%2BPIkM%3D&reserved=0>
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and are confident the content is safe.

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user