0

I have a bunch of utility functions that are not part of any class, which return a unique_ptr<MyGraph>

I have been using the %extend directive for other similar cases where the function is part of a class. But %extend is specifically used to extend classes.

How can I extend my class-less utility functions to return a raw pointer?

n00shie
  • 1,741
  • 1
  • 12
  • 23
  • 1
    Personally I wouldn't hide the unique ptr from the target language at all: http://stackoverflow.com/a/27699663/168175 – Flexo Sep 23 '15 at 20:14

1 Answers1

0

You probably don't need to use %extend in that case. You can just create new methods and return the pointer directly. For example, if your code looks like:

unique_ptr<MyGraph> getMyGraph();

You could simply add a new method:

MyGraph* getGraph() { return getMyGraph().get(); }

If you need to control access to this when using SWIG or not, you could do this:

#ifdef SWIG
    MyGraph* getGraph() { return myGraphPtr.get(); }
#else
    unique_ptr<MyGraph> getMyGraph();
#endif  // SWIG

The 'SWIG' define is used by swig when determining which methods to expose to the target language.

Devan Williams
  • 1,329
  • 2
  • 16
  • 18