Q: accessing method name from within a c-function

Jan Boelsche jan at muskelfisch.com
Thu Jan 17 10:22:36 EST 2002


jan at muskelfisch.com (Jan Boelsche, that's me) wrote:

> I hope I at least succeeded half-way in trying to point out my problem
> :)

I'll post some potential client (pseudo-) code to illustrate what I want to achieve:

---8<---
// Some declarations to make things a bit clearer

class Variant {	// holds all kind of data: strings, integers, floats etc	
	Variant(int i);
	Variant(const string& str);
	...
public:
	// These functions do conversions or, if not applicable, 
	// throw exceptions
	int getAsInteger() const;
	string getAsString() const;
	...
};

// Signature of all native functions
typedef Variant (*NativeFunction)(vector<Variant>& args);

// abstract interface of an interpreter implementation
struct Interpreter {
	virtual void registerFunction(NativeFunction, const string& name) = 0;
	virtual Variant evaluate(const string& script) = 0;
};


// Client code could look like this:

// this is the native function that shall be callable from scripts
Variant helloWorld(vector<Variant>& args) {
	cout << "Hello World!" << endl;
	return Variant(true);
}

void main() {
	Interpreter* interp = createInterpreter("python");
	interp->registerFunction(helloWorld, "hello_world");
	interp->evaluate("hello_world()");
}

---8<---

jan



More information about the Python-list mailing list