'Generate proper SWIG wrappers for pointer reference

I am trying to learn SWIG from the documentation. At the moment I am learning how to wrap pointers using SWIG and generate JAVA wrappers. To do so, I took the example given here and converted the Butler struct to a Butler class:

class Butler
{
private:
    int hoursAvailable;
    char *greeting;

public:
    Butler() { hoursAvailable = 0; greeting = "Hey there! What can I do for you?"; };
    ~Butler() { hoursAvailable = 0; greeting = "Have a good day sir!"; };
    int HireButler(Butler *&ppButler);
    void FireButler(Butler *pButler);
    char *getGreeting();
};

void Butler::FireButler(Butler* pButler)
{
    delete pButler;
}

int Butler::HireButler(Butler*& ppButler)
{
    Butler *pButler = new Butler();
    pButler->hoursAvailable = 24;
    pButler->greeting = ppButler->getGreeting();
    ppButler = pButler;
    delete pButler;
    return 1;
}

char* Butler::getGreeting()
{
    return greeting;
}

class ButlerBoss
{
private:
    Butler* myButler;

public:
    ButlerBoss() { myButler = new Butler(); }
    ~ButlerBoss() { myButler = nullptr; }
    Butler*& getMyButler() { return myButler; }
    void setMyButler(Butler*& pButler) { myButler = pButler; }
};

As you can see what I am trying to do is to wrap Butler *& in HireButler() function. Here is my interface file:

%module ButlerSWIG

// Do not generate the default proxy constructor or destructor
%nodefaultctor Butler;
%nodefaultdtor Butler;

// Add in pure Java code proxy constructor
%typemap(javacode) Butler %{
  /** This constructor creates the proxy which initially does not create nor own any C memory */
  public Butler() {
    this(0, false);
  }
%}

 %typemap(jni) Butler *& "jobject"
 %typemap(jtype) Butler *& "Butler"
 %typemap(jstype) Butler *& "Butler"

 %typemap(javain) Butler *& "$javainput"
 %typemap(javaout) Butler *& {
    return new Butler($jnicall, $owner);
 }

 %{
 #include "Butler.h"
 %}

 class ButlerBoss {
 public:
    Butler*& getMyButler();
    void setMyButler(Butler*& pButler);
 };

The issue is Java classes generated has compilation errors. Removing typemap marshaling lines of Butler*& creates 'SWIGTYPE_p_p_Butler' additional class. Question is: How do I generate proper Java wrappers for pointer references like this?

Edit Made a proper C++ implementation, though not that sophisticated, but should do for the example.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source