diff options
-rw-r--r-- | include/value.h | 11 | ||||
-rw-r--r-- | zend/value.cpp | 80 |
2 files changed, 91 insertions, 0 deletions
diff --git a/include/value.h b/include/value.h index 63e8e6f..c478eb2 100644 --- a/include/value.h +++ b/include/value.h @@ -322,6 +322,17 @@ public: bool operator> (const char *value) const { return ::strcmp(rawValue(), value) > 0; } /** + * Comparison operators for hardcoded Value + * @param value + */ + bool operator==(const Value &value) const; + bool operator!=(const Value &value) const { return !operator==(value); } + bool operator< (const Value &value) const; + bool operator> (const Value &value) const; + bool operator<=(const Value &value) const { return !operator>(value); } + bool operator>=(const Value &value) const { return !operator<(value); } + + /** * Comparison operators * @param value */ diff --git a/zend/value.cpp b/zend/value.cpp index 7fc45d6..477017a 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -1311,6 +1311,86 @@ Value Value::exec(const char *name, int argc, struct _zval_struct ***params) return do_exec(&_val, method._val, argc, params); } + +/** + * Comparison operators for hardcoded Value + * @param value + */ +bool Value::operator==(const Value &value) const +{ + auto tp = type(); + if(tp != value.type()) + return false; + + switch (tp) { + case Type::Null: return true; + case Type::Numeric: return (_val->value.lval == value._val->value.lval); + case Type::Float: return (_val->value.dval == value._val->value.dval); + case Type::Bool: return (_val->value.lval == value._val->value.lval); + case Type::String: return (_val->value.str.len && value._val->value.str.len) && (::strcmp(_val->value.str.val, value._val->value.str.val) == 0); + // @todo + case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; + // @todo + case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; + case Type::Resource: return (_val->value.lval == value._val->value.lval); + case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; + + } + return false; +} +bool Value::operator< (const Value &value) const +{ + auto tp = type(); + if(tp != value.type()){ + // @todo maybe this can be improved + throw Php::Exception("It is not possible to compare different types"); + return false; + } + switch (tp) { + case Type::Null: return false; + case Type::Numeric: return (_val->value.lval < value._val->value.lval); + case Type::Float: return (_val->value.dval < value._val->value.dval); + case Type::Bool: return (_val->value.lval < value._val->value.lval); + case Type::String: return (::strcmp(_val->value.str.val, value._val->value.str.val) < 0); + // @todo + case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; + // @todo + case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; + case Type::Resource: throw Php::Exception("Resource types is not comparable"); break; + case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; + } + return true; +} +bool Value::operator> (const Value &value) const +{ + auto tp = type(); + if(tp != value.type()){ + // @todo maybe this can be improved + throw Php::Exception("It is not possible to compare different types"); + return false; + } + switch (tp) { + case Type::Null: return false; + case Type::Numeric: return (_val->value.lval > value._val->value.lval); + case Type::Float: return (_val->value.dval > value._val->value.dval); + case Type::Bool: return (_val->value.lval > value._val->value.lval); + case Type::String: return (::strcmp(_val->value.str.val, value._val->value.str.val) > 0); + // @todo + case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; + // @todo + case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; + case Type::Resource: throw Php::Exception("Resource types is not comparable"); break; + case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; + case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; + } + return true; +} + /** * The type of object * @return Type |