Programas C/C++ utilizando apontadores opacos

Autor: rubens
Data: 30/04/2009

Exemplo

Módulo C++

 1 struct RCM {
 2     int i;
 3     int j;
 4 };
 5 
 6 RCM *new_rcm(int a, int b)
 7 {
 8     RCM *rcm = new RCM;
 9     rcm->i = a + 10;
10     rcm->j = b + 100;
11     return rcm;
12 }
13 void get_rcm(RCM *rcm, int *i, int *j)
14 {
15     *i = rcm->i;
16     *j = rcm->j;
17 }
18 void del_rcm(RCM *rcm)
19 {
20     delete rcm;
21 }
OK

--------------------------------------------------------------------------------
    Thu Oct  3 14:58:17 2013
--------------------------------------------------------------------------------
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found SWIG: /usr/bin/swig2.0 (found version "2.0.4")
-- Configuring done
-- Generating done
-- Build files have been written to: <pkg_dir>/_build/_opaque
make: Warning: File `Makefile' has modification time 7.4 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 7.4 s in the future
make[2]: Warning: File `CMakeFiles/__opaque.dir/flags.make' has modification time 7.4 s in the future
[ 25%] Swig source
Scanning dependencies of target __opaque
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/__opaque.dir/flags.make' has modification time 6.7 s in the future
[ 50%] Building CXX object CMakeFiles/__opaque.dir/_opaquePYTHON_wrap.o
[ 75%] Building CXX object CMakeFiles/__opaque.dir/_opaque.o
[100%] Building CXX object CMakeFiles/__opaque.dir<pkg_dir>/_build/simple_arrays.o
Linking CXX shared module __opaque.so
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target __opaque
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.

Módulo Python

1 class RCM(object):
2     def __init__(self, a, b):
3         self._rcm = new_rcm(a, b)
4     def __del__(self):
5         del_rcm(self._rcm)
6     def get(self):
7         return get_rcm(self._rcm)

Teste

<opaque.RCM object at 0x7f42e41e6c10> _d0eb24e4427f0000_p_RCM
[35, 100]