3
$\begingroup$

I'm writing a c code by using WSTP library. In my c codes, I have a struct named Person, and I have a function named Foo, which returns an object of Person.

struct Person
{
   int id;
   char* name;
};

Person* Foo()
{
   Person* person = new Perosn();
   ...
   return person; 
}

My problem is in the .tm file, I don't know how can I determine the return type. Please guide me.

$\endgroup$
  • 1
    $\begingroup$ You have used the Manual return type in your last question, so I assume you are familiar with it. This is what you need to do here. BTW you never responded to the comments on your last question. $\endgroup$ – Szabolcs Aug 25 '18 at 17:34
6
$\begingroup$

In file.tm, you need to set the return type to Manual, and declare your C(++) function to be void.

Then return each piece of data making up the struct separately, using low-level MathLink functions.

In this case, you could return a List of an integer and a string, i.e. use

WSPutFunction(stdlink, "List", 2);
  WSPutInteger(stdlink, person->id);
  WSPutString(stdlink, person->name);

at the end of the function.

$\endgroup$
  • $\begingroup$ This is a promising answer, though may need some more background info or a link to the relevant doc. What is stdlink? Is it an instance of WSLINK? $\endgroup$ – Anti Earth Dec 13 '18 at 16:12
  • 1
    $\begingroup$ @AntiEarth edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf library.wolfram.com/infocenter/Demos/174 or search for WSTP in the docs. $\endgroup$ – Szabolcs Dec 13 '18 at 16:14
  • 1
    $\begingroup$ @AntiEarth reference.wolfram.com/language/tutorial/… WSTP and MathLink are the same, but incompatible (= non-mixable) due to different naming (ML vs WS). There's no functional difference. $\endgroup$ – Szabolcs Dec 13 '18 at 16:15
  • 1
    $\begingroup$ @AntiEarth Currently MathLink has no (documented) support for atomic associations. This means that you need to put the compound expression on the link: head Association, followed by several Rules. This is true for several atomic types, e.g. sparse arrays. $\endgroup$ – Szabolcs Dec 13 '18 at 17:05
  • 1
    $\begingroup$ @AntiEarth The WXF format does support associations directly, and might be used for this purpose. You can transfer the WXF data through MathLink or LibraryLink. I have not done this yet. It is a recently introduced format. reference.wolfram.com/language/tutorial/… $\endgroup$ – Szabolcs Dec 13 '18 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.