Add std_auto_ptr.i defining typemaps for returning std::auto_ptr<>.

These typemaps are currently defined for C#, Java and Python only and the
tests are provided only for these languages.

Also add a brief description of the new header to the documentation.
This commit is contained in:
Vadim Zeitlin 2013-11-27 15:44:46 +01:00
commit ed28725a15
10 changed files with 282 additions and 0 deletions

View file

@ -246,6 +246,7 @@ CPP_TEST_CASES += \
li_carrays \
li_cdata \
li_cpointer \
li_std_auto_ptr \
li_stdint \
li_typemaps \
li_typemaps_apply \

View file

@ -0,0 +1,37 @@
using System;
using li_std_auto_ptrNamespace;
public class li_std_auto_ptr_runme {
private static void WaitForGC()
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(10);
}
public static void Main()
{
Klass k1 = li_std_auto_ptr.makeKlassAutoPtr("first");
if (k1.getLabel() != "first")
throw new Exception("wrong object label");
Klass k2 = li_std_auto_ptr.makeKlassAutoPtr("second");
if (Klass.getTotal_count() != 2)
throw new Exception("number of objects should be 2");
k1 = null;
WaitForGC();
if (Klass.getTotal_count() != 1)
throw new Exception("number of objects should be 1");
if (k2.getLabel() != "second")
throw new Exception("wrong object label");
k2 = null;
WaitForGC();
if (Klass.getTotal_count() != 0)
throw new Exception("no objects should be left");
}
}

View file

@ -0,0 +1,48 @@
import li_std_auto_ptr.*;
public class li_std_auto_ptr_runme {
static {
try {
System.loadLibrary("li_std_auto_ptr");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
private static void WaitForGC()
{
System.gc();
System.runFinalization();
try {
java.lang.Thread.sleep(1);
} catch (java.lang.InterruptedException e) {
}
}
public static void main(String argv[]) throws Throwable
{
Klass k1 = li_std_auto_ptr.makeKlassAutoPtr("first");
if (!k1.getLabel().equals("first"))
throw new RuntimeException("wrong object label");
Klass k2 = li_std_auto_ptr.makeKlassAutoPtr("second");
if (Klass.getTotal_count() != 2)
throw new RuntimeException("number of objects should be 2");
k1 = null;
WaitForGC();
if (Klass.getTotal_count() != 1)
throw new RuntimeException("number of objects should be 1");
if (!k2.getLabel().equals("second"))
throw new RuntimeException("wrong object label");
k2 = null;
WaitForGC();
if (Klass.getTotal_count() != 0)
throw new RuntimeException("no objects should be left");
}
}

View file

@ -0,0 +1,56 @@
%module li_std_auto_ptr
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON)
%include "std_auto_ptr.i"
%auto_ptr(Klass)
%inline %{
#include <memory>
#include <string>
#include "swig_examples_lock.h"
class Klass {
public:
explicit Klass(const char* label) :
m_label(label)
{
SwigExamples::Lock lock(critical_section);
total_count++;
}
const char* getLabel() const { return m_label.c_str(); }
~Klass()
{
SwigExamples::Lock lock(critical_section);
total_count--;
}
static int getTotal_count() { return total_count; }
private:
static SwigExamples::CriticalSection critical_section;
static int total_count;
std::string m_label;
};
SwigExamples::CriticalSection Klass::critical_section;
int Klass::total_count = 0;
%}
%template(KlassAutoPtr) std::auto_ptr<Klass>;
%inline %{
std::auto_ptr<Klass> makeKlassAutoPtr(const char* label) {
return std::auto_ptr<Klass>(new Klass(label));
}
%}
#endif

View file

@ -0,0 +1,17 @@
from li_std_auto_ptr import *
k1 = makeKlassAutoPtr("first")
k2 = makeKlassAutoPtr("second")
if Klass.getTotal_count() != 2:
raise "number of objects should be 2"
del k1
if Klass.getTotal_count() != 1:
raise "number of objects should be 1"
if k2.getLabel() != "second":
raise "wrong object label"
del k2
if Klass.getTotal_count() != 0:
raise "no objects should be left"