summaryrefslogtreecommitdiff
path: root/ipl/packs/loadfuncpp/examples/iterate3.cpp
blob: 1b1dd704492f925652b9d2f88bf78fdd14d6e1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/* Example of a C++ extension to icon via loadfunc,
 * without garbage collection difficulties.
 * Type 'make iexample' to build.
 * Carl Sturtivant, 2008/3/16
 */

#include "loadfuncpp.h"
using namespace Icon;


struct addup: public iterate {
    safe total;
	int count;
	addup(): total((long)0) {
		count = 0;
	}
	virtual void takeNext(const value& x) {
		total = total + x;
	}
	virtual bool wantNext(const value& x) {
		return ++count <= 3;
	}
};

extern "C" int iexample(value argv[]) {
 	addup sum;
 	sum.bang(argv[1]);
 	argv[0] = sum.total;
    return SUCCEEDED;
}