Add std::unique support
Simple copy of current auto_ptr support (just suppport for functions returning std::unique_ptr). Closes #1722
This commit is contained in:
parent
1993b334a6
commit
299880e6a6
14 changed files with 443 additions and 15 deletions
|
|
@ -612,6 +612,7 @@ CPP11_TEST_CASES += \
|
|||
cpp11_sizeof_object \
|
||||
cpp11_static_assert \
|
||||
cpp11_std_array \
|
||||
cpp11_std_unique_ptr \
|
||||
cpp11_strongly_typed_enumerations \
|
||||
cpp11_thread_local \
|
||||
cpp11_template_double_brackets \
|
||||
|
|
|
|||
53
Examples/test-suite/cpp11_std_unique_ptr.i
Normal file
53
Examples/test-suite/cpp11_std_unique_ptr.i
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
%module cpp11_std_unique_ptr
|
||||
|
||||
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY)
|
||||
|
||||
%include "std_unique_ptr.i"
|
||||
|
||||
%unique_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;
|
||||
|
||||
%}
|
||||
|
||||
%inline %{
|
||||
|
||||
std::unique_ptr<Klass> makeKlassUniquePtr(const char* label) {
|
||||
return std::unique_ptr<Klass>(new Klass(label));
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
#endif
|
||||
64
Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs
Normal file
64
Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using cpp11_std_unique_ptrNamespace;
|
||||
|
||||
public class cpp11_std_unique_ptr_runme {
|
||||
private static void WaitForGC()
|
||||
{
|
||||
System.GC.Collect();
|
||||
System.GC.WaitForPendingFinalizers();
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
Klass k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first");
|
||||
if (k1.getLabel() != "first")
|
||||
throw new Exception("wrong object label");
|
||||
|
||||
Klass k2 = cpp11_std_unique_ptr.makeKlassUniquePtr("second");
|
||||
if (Klass.getTotal_count() != 2)
|
||||
throw new Exception("number of objects should be 2");
|
||||
|
||||
using (Klass k3 = cpp11_std_unique_ptr.makeKlassUniquePtr("second")) {
|
||||
if (Klass.getTotal_count() != 3)
|
||||
throw new Exception("number of objects should be 3");
|
||||
}
|
||||
if (Klass.getTotal_count() != 2)
|
||||
throw new Exception("number of objects should be 2");
|
||||
|
||||
k1 = null;
|
||||
{
|
||||
int countdown = 500;
|
||||
int expectedCount = 1;
|
||||
while (true) {
|
||||
WaitForGC();
|
||||
if (--countdown == 0)
|
||||
break;
|
||||
if (Klass.getTotal_count() == expectedCount)
|
||||
break;
|
||||
};
|
||||
int actualCount = Klass.getTotal_count();
|
||||
if (actualCount != expectedCount)
|
||||
Console.Error.WriteLine("Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
|
||||
}
|
||||
|
||||
if (k2.getLabel() != "second")
|
||||
throw new Exception("wrong object label");
|
||||
|
||||
k2 = null;
|
||||
{
|
||||
int countdown = 500;
|
||||
int expectedCount = 0;
|
||||
while (true) {
|
||||
WaitForGC();
|
||||
if (--countdown == 0)
|
||||
break;
|
||||
if (Klass.getTotal_count() == expectedCount)
|
||||
break;
|
||||
}
|
||||
int actualCount = Klass.getTotal_count();
|
||||
if (actualCount != expectedCount)
|
||||
Console.Error.WriteLine("Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
|
||||
}
|
||||
}
|
||||
}
|
||||
68
Examples/test-suite/java/cpp11_std_unique_ptr_runme.java
Normal file
68
Examples/test-suite/java/cpp11_std_unique_ptr_runme.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import cpp11_std_unique_ptr.*;
|
||||
|
||||
public class cpp11_std_unique_ptr_runme {
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("cpp11_std_unique_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(10);
|
||||
} catch (java.lang.InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[]) throws Throwable
|
||||
{
|
||||
Klass k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first");
|
||||
if (!k1.getLabel().equals("first"))
|
||||
throw new RuntimeException("wrong object label");
|
||||
|
||||
Klass k2 = cpp11_std_unique_ptr.makeKlassUniquePtr("second");
|
||||
if (Klass.getTotal_count() != 2)
|
||||
throw new RuntimeException("number of objects should be 2");
|
||||
|
||||
k1 = null;
|
||||
{
|
||||
int countdown = 500;
|
||||
int expectedCount = 1;
|
||||
while (true) {
|
||||
WaitForGC();
|
||||
if (--countdown == 0)
|
||||
break;
|
||||
if (Klass.getTotal_count() == expectedCount)
|
||||
break;
|
||||
}
|
||||
int actualCount = Klass.getTotal_count();
|
||||
if (actualCount != expectedCount)
|
||||
System.err.println("GC failed to run (cpp11_std_unique_ptr 1). Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
|
||||
}
|
||||
|
||||
if (!k2.getLabel().equals("second"))
|
||||
throw new RuntimeException("wrong object label");
|
||||
|
||||
k2 = null;
|
||||
{
|
||||
int countdown = 500;
|
||||
int expectedCount = 0;
|
||||
while (true) {
|
||||
WaitForGC();
|
||||
if (--countdown == 0)
|
||||
break;
|
||||
if (Klass.getTotal_count() == expectedCount)
|
||||
break;
|
||||
};
|
||||
int actualCount = Klass.getTotal_count();
|
||||
if (actualCount != expectedCount)
|
||||
System.err.println("GC failed to run (cpp11_std_unique_ptr 2). Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Examples/test-suite/python/cpp11_std_unique_ptr_runme.py
Normal file
17
Examples/test-suite/python/cpp11_std_unique_ptr_runme.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from cpp11_std_unique_ptr import *
|
||||
|
||||
k1 = makeKlassUniquePtr("first")
|
||||
k2 = makeKlassUniquePtr("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"
|
||||
27
Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb
Normal file
27
Examples/test-suite/ruby/cpp11_std_unique_ptr_runme.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'swig_assert'
|
||||
|
||||
require 'cpp11_std_unique_ptr'
|
||||
|
||||
def gc_check(expected_count)
|
||||
# GC.start(full_mark: true, immediate_sweep: true)
|
||||
GC.start
|
||||
# GC is not reliably run, skip check
|
||||
# swig_assert_equal_simple(expected_count, Cpp11_std_unique_ptr::Klass::getTotal_count())
|
||||
end
|
||||
|
||||
k1 = Cpp11_std_unique_ptr::makeKlassUniquePtr("first")
|
||||
k2 = Cpp11_std_unique_ptr::makeKlassUniquePtr("second")
|
||||
swig_assert_equal_simple(2, Cpp11_std_unique_ptr::Klass::getTotal_count())
|
||||
|
||||
gc_check(2)
|
||||
k1 = nil
|
||||
gc_check(1)
|
||||
|
||||
swig_assert_equal_simple(k2.getLabel(), "second")
|
||||
gc_check(1)
|
||||
|
||||
k2 = nil
|
||||
gc_check(0)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue