From e220af8dc07d845efb81082f3159460406ece9ca Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 9 Sep 2013 15:02:22 -0700 Subject: work in progress --- tests/simple/simple.cpp | 14 +-- tests/simple/simple.php | 2 +- tests/simple/test.cpp | 298 ------------------------------------------------ 3 files changed, 7 insertions(+), 307 deletions(-) delete mode 100644 tests/simple/test.cpp (limited to 'tests') diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index a33b220..311ce4c 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -15,14 +15,12 @@ */ using namespace std; -static int my_plus(int a, int b) +static Php::Value my_plus(Php::Parameters ¶ms) { - return a + b; -} - -static std::string my_concat(std::string &a, std::string &b) -{ - return a + b; + string p1 = params[0]; + string p2 = params[1]; + + return p1 + p2; } class MyCustomFunction : public Php::Function @@ -45,7 +43,7 @@ extern "C" cout << "b" << endl; // define the functions -// extension.add("my_plus", my_plus); + extension.add("my_plus", my_plus); // extension.add("my_concat", my_concat); extension.add("my_custom", MyCustomFunction()); diff --git a/tests/simple/simple.php b/tests/simple/simple.php index bdf2b74..fc552fd 100644 --- a/tests/simple/simple.php +++ b/tests/simple/simple.php @@ -10,7 +10,7 @@ class XXX $myvar = "hoi"; -$result = hallo($myvar, 1, 2, 3, "blabla", new XXX()); +$result = my_plus($myvar, 1, 2, 3, "blabla", new XXX()); echo("myvar = $myvar\n"); diff --git a/tests/simple/test.cpp b/tests/simple/test.cpp deleted file mode 100644 index dbae91e..0000000 --- a/tests/simple/test.cpp +++ /dev/null @@ -1,298 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; - -/** - * Example function that adds numbers - * @param a A number - * @param b A different number - * @return Sum of both parameters - */ -int my_plus(int a, int b) -{ - cout << "my_plus(" << a << ", " << b << ")" << endl; - - return a + b; -} - -/** - * Another example function that concatenates strings - * @param a A text - * @param b A different text - * @return Concattenation - */ -const string my_concat(const string &a, const string &b) -{ - cout << "my_concat(" << a << ", " << b << ")" << endl; - - return a + b; -} - -/** - * Value class that can automatically convert objects between many - * different types - */ -class Value -{ -private: - /** - * Internal representation - * @var _value - */ - string _value; - -public: - /** - * Constructor based on string - * @param text - */ - Value(const string &text) : _value(text) {} - - /** - * Constructor based on string - * @param text - */ - Value(const char *text) : _value(text) {} - - /** - * Constructor based on int - * @param integer - */ - Value(int value) - { - ostringstream s; - s << value; - _value = s.str(); - } - - /** - * Destructor - */ - virtual ~Value() {} - - /** - * Cast to integer - * @return Numeric representation - */ - operator int () - { - return atoi(_value.c_str()); - } - - /** - * Cast to string - * @return String representation - */ - operator const string& () - { - return _value; - } -}; - -/** - * Simple wrapper around a function - */ -class Function -{ -private: - /** - * The actual function - * - * Note: there must be a much smarter way to achieve the same thing, - * we just want to have a single member that holds the function - * - * @var _function - */ - union F { - // union members - function r0; - function r1; - function r2; - function v0; - function v1; - function v2; - - // constructors - F() {} - F(function &f) { r0 = f; } - F(function &f) { r1 = f; } - F(function &f) { r2 = f; } - F(function &f) { v0 = f; } - F(function &f) { v1 = f; } - F(function &f) { v2 = f; } - - // destructor - ~F(); - - // assignment operator -// F &operator=(function &f) { r0 = f; return *this; } -// F &operator=(function &f) { r1 = f; return *this; } -// F &operator=(function &f) { r2 = f; return *this; } - - } _function; - - /** - * Is the object in a valid state - * @var bool - */ - bool _valid; - - /** - * The number of arguments that the function required - * @var _argc - */ - int _argc; - - /** - * Does the function return something? - * @var _return; - */ - bool _return; - -public: - /** - * Contructor - * @param f - */ - Function(function &f) : _function(f) - { - _argc = 0; - _return = true; - _valid = true; - } - - /** - * Contructor - * @param f - */ - Function(function &f) : _function(f) - { - _argc = 1; - _return = true; - } - - /** - * Contructor - * @param f - */ - Function(function &f) : _function(f) - { - _argc = 2; - _return = true; - } - - /** - * Move constructor - * @param f - */ - Function(const Function &&f) - { - // copy params - _argc = f._argc; - _return = f._return; - - switch (_argc) { - case 0: _function.r0 = f._function.r0; break; - case 1: _function.r1 = f._function.r1; break; - case 2: _function.r2 = f._function.r2; break; - } - } - - /** - * Destructor - */ - virtual ~Function() {} - - /** - * Operator to call the function - * @param args - */ - void operator()(vector args) - { - switch (_argc) { - case 0: _function.r0(); - case 1: _function.r1(args[0]); - case 2: _function.r2(args[0], args[1]); - } - } -}; - -/** - * Class that stores pointers to functions - */ -class Functions -{ -private: - /** - * Map of functions - * @var map - */ - map _functions; - -public: - /** - * Constructor - */ - Functions() {} - - /** - * Destructor - */ - virtual ~Functions() {} - - /** - * Add a function - * @param name The function name - * @param func The function to call - */ - void add(const string &name, function f) - { - _functions.insert(pair(name, Function(f))); - } - - /** - * Call the functions - * @param args - */ - Value operator()(vector args) - { - for (auto it = _functions.begin(); it != _functions.end(); it++) - { - it->second(args); - } - - return Value(123); - } - -}; - -/** - * Main function - * @param argc - * @param argv - */ -int main(int argc, char *argv[]) -{ - // create a vector of arguments - vector arguments; - - // fill the arguments - for (int i=1; i