Debian Codegraph @

ipl/packs/loadfuncpp/examples/iterate2.cpp

raw
1
2 /* Example of a C++ extension to icon via loadfunc,
3 * without garbage collection difficulties.
4 * Type 'make iexample' to build.
5 * Carl Sturtivant, 2008/3/16
6 */
7
8 #include "loadfuncpp.h"
9 using namespace Icon;
10
11
12 struct addup: public iterate {
13 safe total;
14 int count;
15 addup(): total((long)0), count(0) {}
16
17 virtual void takeNext(const value& x) {
18 total = total + x;
19 }
20 virtual bool wantNext(const value& x) {
21 return ++count <= 3;
22 }
23 };
24
25 extern "C" int iexample(int argc, value argv[]) {
26 addup sum;
27 sum.every(argv[1], argv[2]);
28 argv[0] = sum.total;
29 return SUCCEEDED;
30 }
31