From 70cb080c649ab16ce83de63522d3683e5337fd3c Mon Sep 17 00:00:00 2001 From: andot Date: Thu, 10 Jul 2014 22:25:48 +0800 Subject: [PATCH 1/3] Add hash method for Php::Value If you want to use Php::Value as a key of unordered_map, you can use this method to implement the hash function. But it only works correctly when patched #114. --- include/value.h | 6 ++++++ zend/value.cpp | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/value.h b/include/value.h index f72304cf..dfaeb231 100644 --- a/include/value.h +++ b/include/value.h @@ -965,6 +965,12 @@ class Value : private HashParent return dynamic_cast(base); } + /** + * Return a hash value for unordered_map. + * @return size_t + */ + size_t hash() const; + private: /** * Call function with a number of parameters diff --git a/zend/value.cpp b/zend/value.cpp index 9a1a5dbb..50c0c7d1 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -2025,3 +2025,24 @@ std::ostream &operator<<(std::ostream &stream, const Value &value) */ } +/** + * get a hash value for unordered_map. + * @return bool + */ +size_t Value::hash() const { + static std::hash stringhash; + switch (type()) { + case Type::Null: return 0; break; + case Type::Numeric: return Z_LVAL_P(_val); break; + case Type::Float: return (size_t)Z_DVAL_P(_val); break; + case Type::Bool: return (size_t)Z_BVAL_P(_val); break; + case Type::Array: return (size_t)(intptr_t)Z_ARRVAL_P(_val); break; + case Type::Object: return (size_t)((intptr_t)Z_OBJ_HANDLE_P(_val) ^ (intptr_t)Z_OBJ_HT_P(_val)); break; + case Type::String: return stringhash(stringValue()); break; + case Type::Resource: throw FatalError("Resource types can not be handled by the PHP-CPP library"); break; + case Type::Constant: throw FatalError("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::ConstantArray: throw FatalError("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::Callable: throw FatalError("Callable types can not be assigned to a PHP-CPP library variable"); break; + } + return 0; +} From 6c07a08f42f1f089f1197c5545bd942ae3adbd0a Mon Sep 17 00:00:00 2001 From: andot Date: Thu, 10 Jul 2014 22:30:35 +0800 Subject: [PATCH 2/3] Add className method for Php::Value This method can return the calss name of the object. and return empty string when the value is not an object. --- include/value.h | 7 +++++++ zend/value.cpp | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/value.h b/include/value.h index dfaeb231..0dabad99 100644 --- a/include/value.h +++ b/include/value.h @@ -971,6 +971,13 @@ class Value : private HashParent */ size_t hash() const; + /** + * Return the class name of the object. + * Return empty string when the value is not an object. + * @return std::string + */ + std::string className() const; + private: /** * Call function with a number of parameters diff --git a/zend/value.cpp b/zend/value.cpp index 50c0c7d1..ede3a934 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -2020,11 +2020,6 @@ std::ostream &operator<<(std::ostream &stream, const Value &value) return stream << value.stringValue(); } -/** - * End of namespace - */ -} - /** * get a hash value for unordered_map. * @return bool @@ -2046,3 +2041,17 @@ size_t Value::hash() const { } return 0; } + +/** + * Return the class name of the object. + * Return empty string when the value is not an object. + * @return std::string + */ +std::string Value::className() const { + return Z_OBJ_CLASS_NAME_P(_val); +} + +/** + * End of namespace + */ +} \ No newline at end of file From 65548c6107e466331531c97471a149666aa3d2a8 Mon Sep 17 00:00:00 2001 From: andot Date: Thu, 10 Jul 2014 22:35:35 +0800 Subject: [PATCH 3/3] Add id method of Php::Value This method return an id of the object. It is like spl_object_hash, it return empty string when the value is not an object. --- include/value.h | 6 ++++++ zend/value.cpp | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/value.h b/include/value.h index 0dabad99..035ea89f 100644 --- a/include/value.h +++ b/include/value.h @@ -978,6 +978,12 @@ class Value : private HashParent */ std::string className() const; + /** + * Return an id of the object. (It is like spl_object_hash) + * Return empty string when the value is not an object. + * @return std::string + */ + std::string id() const; private: /** * Call function with a number of parameters diff --git a/zend/value.cpp b/zend/value.cpp index ede3a934..37e797ef 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -2051,6 +2051,22 @@ std::string Value::className() const { return Z_OBJ_CLASS_NAME_P(_val); } +/** + * Return an id of the object. (It is like spl_object_hash) + * Return empty string when the value is not an object. + * @return std::string + */ +std::string Value::id() const { + if (!isObject()) return ""; + static intptr_t hash_mask_handle = rand(); + static intptr_t hash_mask_handlers = rand(); + char id[33]; + intptr_t hash_handle, hash_handlers; + hash_handle = hash_mask_handle ^ (intptr_t)Z_OBJ_HANDLE_P(_val); + hash_handlers = hash_mask_handlers ^ (intptr_t)Z_OBJ_HT_P(_val); + sprintf(id, "%016lx%016lx", (long)hash_handle, (long)hash_handlers); + return id; +} /** * End of namespace */