annotate R/rdyncall/demo/callbacks.R @ 66:7a61dd082341 default tip

pydc: - fix double free triggered by capsule destructor, when freeing pydc and callback objects, manually
author Tassilo Philipp
date Fri, 24 May 2024 18:16:29 +0200
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 # Package: rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 # File: demo/callbacks.R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 # Description: Creating a callback and call it via .dyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 # The function to wrap:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 f <- function(x,y) x+y
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 # Create the callback:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 cb <- new.callback("ii)i", f)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 # Call the callback
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 r <- .dyncall(cb, "ii)i", 20, 3)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 r == 23
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 # Recursive callback example:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 f <- function(x,y,f,i)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 if (i > 1) .dyncall(f, "iipi)i", x,y,f,i-1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 x+y
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 cb <- new.callback("iipi)i", f)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 r <- .dyncall(cb, "iipi)i", 1,1,cb,100 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 r == 2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27