From cf25fa0b9636d232c63f01824d66289ab9a89b81 Mon Sep 17 00:00:00 2001 From: valmat Date: Tue, 25 Nov 2014 23:36:07 +0600 Subject: Used variadic templates instead of code duplication --- include/object.h | 24 +--- include/value.h | 63 +++++---- zend/value.cpp | 380 ------------------------------------------------------- 3 files changed, 44 insertions(+), 423 deletions(-) diff --git a/include/object.h b/include/object.h index 9387408..98e2553 100644 --- a/include/object.h +++ b/include/object.h @@ -71,28 +71,10 @@ public: * number of parameters that are passed to the constructor * * @param name Name of the class to instantiate - * @param arg0 Optional argument 1 - * @param arg1 Optional argument 2 - * @param arg2 Optional argument 3 - * @param arg3 Optional argument 4 - * @param arg4 Optional argument 5 - * @param arg5 Optional argument 6 - * @param arg6 Optional argument 7 - * @param arg7 Optional argument 8 - * @param arg8 Optional argument 9 - * @param arg9 Optional argument 10 + * @param args Optional arguments */ - Object(const char *name) : Value() { if (instantiate(name)) call("__construct"); } - Object(const char *name, Value p0) : Value() { if (instantiate(name)) call("__construct", p0); } - Object(const char *name, Value p0, Value p1) : Value() { if (instantiate(name)) call("__construct", p0, p1); } - Object(const char *name, Value p0, Value p1, Value p2) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4, p5); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4, p5, p6); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4, p5, p6, p7); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4, p5, p6, p7, p8); } - Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) : Value() { if (instantiate(name)) call("__construct", p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); } + template + Object(const char *name, Args&&... args) : Value() { if (instantiate(name)) call("__construct", std::forward(args)...); } /** * Destructor diff --git a/include/value.h b/include/value.h index 5c34406..d6f8da6 100644 --- a/include/value.h +++ b/include/value.h @@ -945,24 +945,32 @@ public: return get(key.stringValue()); } - /** * Call the function in PHP - * We have ten variants of this function, depending on the number of parameters * This call operator is only useful when the variable represents a callable * @return Value */ Value operator()() const; - Value operator()(Value p0) const; - Value operator()(Value p0, Value p1) const; - Value operator()(Value p0, Value p1, Value p2) const; - Value operator()(Value p0, Value p1, Value p2, Value p3) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) const; - Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) const; + + /** + * Call the function - if the variable holds a callable thing + * @param args Optional arguments + * @return Value + */ + template + Value operator()(Args&&... args) const + { + // store arguments + Value vargs[] = { static_cast(args)... }; + //Value vargs[] = { std::forward(args)... }; + + // array of parameters + _zval_struct **params[sizeof...(Args)]; + for(unsigned i=0; i < sizeof...(Args); i++) {params[i] = &vargs[i]._val;} + + // call the function + return exec(sizeof...(Args), params); + } /** * Call a method @@ -972,16 +980,27 @@ public: * @return Value */ Value call(const char *name); - Value call(const char *name, Value p0); - Value call(const char *name, Value p0, Value p1); - Value call(const char *name, Value p0, Value p1, Value p2); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8); - Value call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9); + + /** + * + * Call the method - if the variable holds an object with the given method + * @param name name of the method to call + * @param p0 The first parameter + * @return Value + */ + template + Value call(const char *name, Args&&... args) + { + // store arguments + Value vargs[] = { static_cast(args)... }; + + // array of parameters + _zval_struct **params[sizeof...(Args)]; + for(unsigned i=0; i < sizeof...(Args); i++) {params[i] = &vargs[i]._val;} + + // call the function + return exec(name, sizeof...(Args), params); + } /** * Retrieve the original implementation diff --git a/zend/value.cpp b/zend/value.cpp index 501feb7..8d1e78f 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -850,191 +850,6 @@ Value Value::operator()() const return exec(0, NULL); } -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @return Value - */ -Value Value::operator()(Value p0) const -{ - // array of parameters - zval **params[1] = { &p0._val }; - - // call the function - return exec(1, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1) const -{ - // array of parameters - zval **params[2] = { &p0._val, &p1._val }; - - // call the function - return exec(2, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2) const -{ - // array of parameters - zval **params[3] = { &p0._val, &p1._val, &p2._val }; - - // call the function - return exec(3, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3) const -{ - // array of parameters - zval **params[4] = { &p0._val, &p1._val, &p2._val, &p3._val }; - - // call the function - return exec(4, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4) const -{ - // array of parameters - zval **params[5] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val }; - - // call the function - return exec(5, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) const -{ - // array of parameters - zval **params[6] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val }; - - // call the function - return exec(6, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) const -{ - // array of parameters - zval **params[7] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val }; - - // call the function - return exec(7, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) const -{ - // array of parameters - zval **params[8] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val }; - - // call the function - return exec(8, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @param p8 The ninth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) const -{ - // array of parameters - zval **params[9] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val }; - - // call the function - return exec(9, params); -} - -/** - * Call the function - if the variable holds a callable thing - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @param p8 The ninth parameter - * @param p9 The tenth parameter - * @return Value - */ -Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) const -{ - // array of parameters - zval **params[10] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val, &p9._val }; - - // call the function - return exec(10, params); -} - /** * Call the method - if the variable holds an object with the given method * @param name name of the method to call @@ -1046,201 +861,6 @@ Value Value::call(const char *name) return exec(name, 0, NULL); } -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @return Value - */ -Value Value::call(const char *name, Value p0) -{ - // array of parameters - zval **params[] = { &p0._val }; - - // call with zero parameters - return exec(name, 1, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val }; - - // call with zero parameters - return exec(name, 2, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val }; - - // call with zero parameters - return exec(name, 3, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val }; - - // call with zero parameters - return exec(name, 4, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val }; - - // call with zero parameters - return exec(name, 5, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val }; - - // call with zero parameters - return exec(name, 6, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val }; - - // call with zero parameters - return exec(name, 7, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val }; - - // call with zero parameters - return exec(name, 8, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @param p8 The ninth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val }; - - // call with zero parameters - return exec(name, 9, params); -} - -/** - * Call the method - if the variable holds an object with the given method - * @param name name of the method to call - * @param p0 The first parameter - * @param p1 The second parameter - * @param p2 The third parameter - * @param p3 The fourth parameter - * @param p4 The fifth parameter - * @param p5 The sixth parameter - * @param p6 The seventh parameter - * @param p7 The eighth parameter - * @param p8 The ninth parameter - * @param p9 The tenth parameter - * @return Value - */ -Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) -{ - // array of parameters - zval **params[] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val, &p9._val }; - - // call with zero parameters - return exec(name, 10, params); -} - /** * Helper function that runs the actual call * @param object The object to call it on -- cgit v1.2.3