Monday, May 29, 2006

Axon in C++

Axon is the core of Kamaelia - the stuff that co-ordinates everything else, ensuring messages go from outboxes to their recipients' inboxes and running the components in turn. Kamaelia is primarily written in Python but there is proof-of-concept code for C++ in the CVS.
I've been creating my own (not-so-)MiniAxon in C++, focusing on efficiency unless it would overcomplicate the API. A Kamaelia implementation in C++ must provide much that the Python developer takes for granted. For example, Duff's device serves as a potent and yet intrinsically sinful implementation of generators:
#include <iostream>

static int generatorstate = 0, x = 1;
int generator()
{
switch (generatorstate)
{
case 0:
std::cout << "Hello World\n";
generatorstate = 1; return 1;
case 1:
for (;;)
{
std::cout << x << "\n";
x = x + 1;
generatorstate = 2; return 1;
case 2:
std::cout << x << "\n";
x = x * x;
generatorstate = 3; return 1;
case 3:
;
}
}
}

int main()
{
generator();
std::cout << "Run 1 time\n";
generator();
std::cout << "Run 2 times\n";
generator();
generator();
generator();
std::cout << "Run 5 times\n";
generator();
generator();
generator();
generator();
generator();
std::cout << "Run 10 times\n";
}

Labels: ,

0 Comments:

Post a Comment

<< Home