summaryrefslogtreecommitdiff
path: root/include/hashmember.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-09 10:08:29 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-09 10:08:29 +0200
commit3953d8833e070fa1d6cc7b264dd19ed56c909f02 (patch)
treeb57091d0bfc1dd6234be9d09b6ba55e77948c8e4 /include/hashmember.h
parent614d95ad69f4e1d3457f2831abc94ecb0809c12e (diff)
added support for accessing array keys by other value objects, support for unsetting array members, support for array_key_exists(), isset() and unset() functions (feature request in from issue #71)
Diffstat (limited to 'include/hashmember.h')
-rw-r--r--include/hashmember.h118
1 files changed, 117 insertions, 1 deletions
diff --git a/include/hashmember.h b/include/hashmember.h
index 9a2d5e8..1a29548 100644
--- a/include/hashmember.h
+++ b/include/hashmember.h
@@ -381,7 +381,7 @@ public:
/**
* Check if a certain index exists in the array/object
- * @param key
+ * @param index
* @return bool
*/
virtual bool contains(int index) const override
@@ -389,6 +389,17 @@ public:
// object must exist, and the value must contain the key
return exists() && value().contains(index);
}
+
+ /**
+ * Check if a certain index exists in the array/object
+ * @param key
+ * @return bool
+ */
+ virtual bool contains(const Value &key) const override
+ {
+ // object must exist, and the value must contain the key
+ return exists() && value().contains(key);
+ }
/**
* Retrieve the value at a string index
@@ -417,6 +428,20 @@ public:
// ask the value
return value().get(index);
}
+
+ /**
+ * Retrieve the value at a variant index
+ * @param key
+ * @return Value
+ */
+ virtual Value get(const Value &key) const override
+ {
+ // return null if it does not exist
+ if (!exists()) return nullptr;
+
+ // ask the value
+ return value().get(key);
+ }
/**
* Overwrite the value at a certain string index
@@ -452,6 +477,97 @@ public:
_parent->set(_index, current);
}
+ /**
+ * Overwrite the value at a certain variant index
+ * @param key
+ * @param value
+ */
+ virtual void set(const Value &key, const Value &value) override
+ {
+ // get the current value
+ Value current(this->value());
+
+ // add the value
+ current[key] = value;
+
+ // pass this to the base
+ _parent->set(_index, current);
+ }
+
+ /**
+ * Unset the member
+ */
+ void unset()
+ {
+ _parent->unset(_index);
+ }
+
+ /**
+ * Unset a member by its index
+ * @param index
+ */
+ virtual void unset(int index) override
+ {
+ // if the current property does not even exist, we do not have to add anything
+ if (!exists()) return;
+
+ // get the current value
+ Value current(this->value());
+
+ // skip if the property does not exist
+ if (!current.contains(index)) return;
+
+ // remove the index
+ current.unset(index);
+
+ // pass the new value to the base
+ _parent->set(_index, current);
+ }
+
+ /**
+ * Unset a member by its key
+ * @param key
+ */
+ virtual void unset(const std::string &key) override
+ {
+ // if the current property does not even exist, we do not have to add anything
+ if (!exists()) return;
+
+ // get the current value
+ Value current(this->value());
+
+ // skip if the property does not exist
+ if (!current.contains(key)) return;
+
+ // remove the index
+ current.unset(key);
+
+ // pass the new value to the base
+ _parent->set(_index, current);
+ }
+
+ /**
+ * Unset a member by its key
+ * @param key
+ */
+ virtual void unset(const Value &key) override
+ {
+ // if the current property does not even exist, we do not have to add anything
+ if (!exists()) return;
+
+ // get the current value
+ Value current(this->value());
+
+ // skip if the property does not exist
+ if (!current.contains(key)) return;
+
+ // remove the index
+ current.unset(key);
+
+ // pass the new value to the base
+ _parent->set(_index, current);
+ }
+
protected:
/**
* Protected copy constructor