diff options
-rw-r--r-- | include/forcedvalue.h | 10 | ||||
-rw-r--r-- | include/object.h | 26 | ||||
-rw-r--r-- | include/value.h | 24 | ||||
-rw-r--r-- | src/object.cpp | 32 | ||||
-rw-r--r-- | src/value.cpp | 26 |
5 files changed, 68 insertions, 50 deletions
diff --git a/include/forcedvalue.h b/include/forcedvalue.h index 67c339e..4fc2fcc 100644 --- a/include/forcedvalue.h +++ b/include/forcedvalue.h @@ -46,6 +46,16 @@ public: // type must be valid if (value.type() != TYPE) throw Php::Exception("Assiging a wrong value type to a forced typed variable"); } + + /** + * Move constructor from a value object + * @param value + */ + ForcedValue(Value &&value) : Value(std::move(value)) + { + // type must be valid + if (value.type() != TYPE) throw Php::Exception("Assiging a wrong value type to a forced typed variable"); + } /** * Wrap object around zval diff --git a/include/object.h b/include/object.h index f104478..e2c424f 100644 --- a/include/object.h +++ b/include/object.h @@ -21,14 +21,15 @@ class Object : public ForcedValue<Type::Object> public: /** * Constructor for an empty stdClass object - * - * @todo check if this indeed leads to a stdClass instance */ Object() : ForcedValue<Type::Object>() {} /** - * @todo copy constructor, move constructor + * Copy and move constructors are passed on to the base class + * @param that An other object */ + Object(const Value &value) : ForcedValue<Type::Object>(value) {} + Object(Value &&value) : ForcedValue<Type::Object>(std::move(value)) {} /** * Constructor to create a new instance @@ -48,12 +49,29 @@ public: * @param arg8 Optional argument 9 * @param arg9 Optional argument 10 */ - Object(const char *name); + Object(const char *name) { instantiate(name); call("__construct"); } + Object(const char *name, Value p0) { instantiate(name); call("__construct", p0); } + Object(const char *name, Value p0, Value p1) { instantiate(name); call("__construct", p0, p1); } + Object(const char *name, Value p0, Value p1, Value p2) { instantiate(name); call("__construct", p0, p1, p2); } + Object(const char *name, Value p0, Value p1, Value p2, Value p3) { instantiate(name); call("__construct", p0, p1, p2, p3); } + Object(const char *name, Value p0, Value p1, Value p2, Value p3, Value p4) { 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) { 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) { 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) { 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) { 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) { instantiate(name); call("__construct", p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); } /** * Destructor */ virtual ~Object() {} + +private: + /** + * Helper method to instantiate an object + * @param name Class name + */ + void instantiate(const char *name); }; diff --git a/include/value.h b/include/value.h index 0ebb2d5..8af95a5 100644 --- a/include/value.h +++ b/include/value.h @@ -594,17 +594,17 @@ public: * @param name Name of the function * @return Value */ - Value call(const std::string &name); - Value call(const std::string &name, Value p0); - Value call(const std::string &name, Value p0, Value p1); - Value call(const std::string &name, Value p0, Value p1, Value p2); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8); - Value call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9); + 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); /** * Retrieve the original implementation @@ -651,7 +651,7 @@ private: * @param argv The parameters * @return Value */ - Value exec(const std::string &name, int argc, struct _zval_struct ***params); + Value exec(const char *name, int argc, struct _zval_struct ***params); protected: /** diff --git a/src/object.cpp b/src/object.cpp index 99de4a0..aa99c0d 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -12,37 +12,27 @@ namespace Php { /** - * Constructor + * Internal method to instantiate an object * @param name */ -Object::Object(const char *name) +void Object::instantiate(const char *name) { - // step 1: convert the name into a class_entry + // convert the name into a class_entry auto *entry = zend_fetch_class(name, strlen(name), 0); - if (!entry) throw Php::Exception("Unknown class name"); + if (!entry) throw Php::Exception(std::string("Unknown class name ") + name); // initiate the zval (which was already allocated in the base constructor) object_init_ex(_val, entry); -// // is there a special function to create the object? -// if (entry->create_object) -// { -// // create the object -// zend_object_value value = entry->create_object(entry); -// -// // wrap this in the zval (which was already allocated in the base constructor) -// Z_TYPE_P(_val) = IS_OBJECT; -// Z_OBJVAL_P(_val) = value; -// } -// else -// { -// } + // @todo should we call methods like allocating hashtables, copying and + // initializing properties, et cetera????? In all example you always + // see such complicated and next-to-impossible-to-understand + // sequences of functions being called, but this object_init_ex + // also seems to work... - // @todo should we call methods like allocating hashtables, copyint and - // initializing properties, et cetera????? + // @todo is this a memory leak? the base class first initializes a stdClass, + // and then we overwrite it with a specific class - // call the constructor - call("__construct"); } /** diff --git a/src/value.cpp b/src/value.cpp index ab4c350..8047dee 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -895,7 +895,7 @@ Value Value::operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value * @param name name of the method to call * @return Value */ -Value Value::call(const std::string &name) +Value Value::call(const char *name) { // call with zero parameters return exec(name, 0, NULL); @@ -907,7 +907,7 @@ Value Value::call(const std::string &name) * @param p0 The first parameter * @return Value */ -Value Value::call(const std::string &name, Value p0) +Value Value::call(const char *name, Value p0) { // array of parameters zval **params[] = { &p0._val }; @@ -923,7 +923,7 @@ Value Value::call(const std::string &name, Value p0) * @param p1 The second parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1) +Value Value::call(const char *name, Value p0, Value p1) { // array of parameters zval **params[] = { &p0._val, &p1._val }; @@ -940,7 +940,7 @@ Value Value::call(const std::string &name, Value p0, Value p1) * @param p2 The third parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2) +Value Value::call(const char *name, Value p0, Value p1, Value p2) { // array of parameters zval **params[] = { &p0._val, &p1._val, &p2._val }; @@ -958,7 +958,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2) * @param p3 The fourth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3) +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 }; @@ -977,7 +977,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p4 The fifth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4) +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 }; @@ -997,7 +997,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p5 The sixth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) +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 }; @@ -1018,7 +1018,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p6 The seventh parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) +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 }; @@ -1040,7 +1040,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p7 The eighth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) +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 }; @@ -1063,7 +1063,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p8 The ninth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) +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 }; @@ -1087,7 +1087,7 @@ Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p * @param p9 The tenth parameter * @return Value */ -Value Value::call(const std::string &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) +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 }; @@ -1127,7 +1127,7 @@ Value Value::exec(int argc, zval ***params) const * @param argv The parameters * @return Value */ -Value Value::exec(const std::string &name, int argc, struct _zval_struct ***params) +Value Value::exec(const char *name, int argc, struct _zval_struct ***params) { // the method to call and the return value zval *method; @@ -1141,7 +1141,7 @@ Value Value::exec(const std::string &name, int argc, struct _zval_struct ***para // add the object and the method to call add_index_zval(method, 0, _val); - add_index_stringl(method, 1, name.c_str(), name.length(), 0); + add_index_stringl(method, 1, name, strlen(name), 0); // the current exception zval *oldException = EG(exception); |