]> granicus.if.org Git - icinga2/commitdiff
Implement Object#clone and rename Array/Dictionary#clone to shallow_clone
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 17 Aug 2015 11:59:49 +0000 (13:59 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 17 Aug 2015 11:59:49 +0000 (13:59 +0200)
fixes #9931

12 files changed:
doc/20-library-reference.md
lib/base/array-script.cpp
lib/base/array.cpp
lib/base/array.hpp
lib/base/dictionary-script.cpp
lib/base/dictionary.cpp
lib/base/dictionary.hpp
lib/base/object-script.cpp
lib/base/object.cpp
lib/base/object.hpp
lib/base/value.cpp
lib/base/value.hpp

index 22e69504051f28f977465baaded0d430915a9aa6..dca6294cd64de4162e646e88923306480a0dce00 100644 (file)
@@ -514,9 +514,9 @@ Signature:
 
 Removes all elements from the array.
 
-### <a id="array-clone"></a> Array#clone
+### <a id="array-shallow-clone"></a> Array#shallow_clone
 
-    function clone();
+    function shallow_clone();
 
 Returns a copy of the array. Note that for elements which are reference values (e.g. objects such
 as arrays and dictionaries) only the references are copied.
@@ -582,11 +582,11 @@ Joins all elements of the array using the specified separator.
 
 ## <a id="dictionary-type"></a> Dictionary type
 
-### <a id="dictionary-clone"></a> Dictionary#clone
+### <a id="dictionary-shallow-clone"></a> Dictionary#shallow_clone
 
 Signature:
 
-    function clone();
+    function shallow_clone();
 
 Returns a copy of the dictionary. Note that for elements which are reference values (e.g. objects such
 as arrays and dictionaries) only the references are copied.
index 2efde98b6a0b0897806709b03fdc30be01db69e4..e80042aad4365bbb14b088de3a917bb265509615 100644 (file)
@@ -101,7 +101,7 @@ static Array::Ptr ArraySort(const std::vector<Value>& args)
        return arr;
 }
 
-static Array::Ptr ArrayClone(void)
+static Array::Ptr ArrayShallowClone(void)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
        Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
@@ -144,7 +144,7 @@ Object::Ptr Array::GetPrototype(void)
                prototype->Set("contains", new Function(WrapFunction(ArrayContains), true));
                prototype->Set("clear", new Function(WrapFunction(ArrayClear)));
                prototype->Set("sort", new Function(WrapFunction(ArraySort), true));
-               prototype->Set("clone", new Function(WrapFunction(ArrayClone), true));
+               prototype->Set("shallow_clone", new Function(WrapFunction(ArrayShallowClone), true));
                prototype->Set("join", new Function(WrapFunction(ArrayJoin), true));
        }
 
index 4365b3dd8a2c81579a7935047a212bbd479fa932..399d442b86d0a77af53a68defdcbcd1424bf909b 100644 (file)
@@ -182,3 +182,21 @@ Array::Ptr Array::ShallowClone(void) const
        return clone;
 }
 
+/**
+ * Makes a deep clone of an array
+ * and its elements.
+ * 
+ * @returns a copy of the array.
+ */
+Object::Ptr Array::Clone(void) const
+{
+       Array::Ptr arr = new Array();
+       
+       ObjectLock olock(this);
+       BOOST_FOREACH(const Value& val, m_Data) {
+               arr->Add(val.Clone());
+       }
+       
+       return arr;
+}
+
index b224fba31a7445ee42ce28ba6cbae94cca1e55bc..88eb3fdde28462c6dc42cb6ed376d80d061a3634 100644 (file)
@@ -109,6 +109,8 @@ public:
                std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
                return result;
        }
+       
+       virtual Object::Ptr Clone(void) const;
 
 private:
        std::vector<Value> m_Data; /**< The data for the array. */
index 8636783a1c3c9e3aa40416e50d5dc1805b8341c0..2fedf4b315886750468abe480b75a8c9263d751e 100644 (file)
@@ -61,7 +61,7 @@ static bool DictionaryContains(const String& key)
        return self->Contains(key);
 }
 
-static Dictionary::Ptr DictionaryClone(void)
+static Dictionary::Ptr DictionaryShallowClone(void)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
        Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
@@ -91,7 +91,7 @@ Object::Ptr Dictionary::GetPrototype(void)
                prototype->Set("get", new Function(WrapFunction(DictionaryGet)));
                prototype->Set("remove", new Function(WrapFunction(DictionaryRemove)));
                prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true));
-               prototype->Set("clone", new Function(WrapFunction(DictionaryClone), true));
+               prototype->Set("shallow_clone", new Function(WrapFunction(DictionaryShallowClone), true));
                prototype->Set("keys", new Function(WrapFunction(DictionaryKeys), true));
        }
 
index 33d043b6dba1a71627849ad7b6f009cc0686384e..c4b9d2e4540e5a6b827f47fd8321c355e8016e59 100644 (file)
@@ -164,6 +164,24 @@ Dictionary::Ptr Dictionary::ShallowClone(void) const
        return clone;
 }
 
+/**
+ * Makes a deep clone of a dictionary
+ * and its elements.
+ * 
+ * @returns a copy of the dictionary.
+ */
+Object::Ptr Dictionary::Clone(void) const
+{
+       Dictionary::Ptr dict = new Dictionary();
+       
+       ObjectLock olock(this);
+       BOOST_FOREACH(const Dictionary::Pair& kv, m_Data) {
+               dict->Set(kv.first, kv.second.Clone());
+       }
+       
+       return dict;
+}
+
 /**
  * Returns an array containing all keys
  * which are currently set in this directory.
index 6a39f44a2015baf7353cfb089f1ac50747901dd8..152956ac11bbddb3a8ee80fbe305fdbe57aa0583 100644 (file)
@@ -112,6 +112,8 @@ public:
        std::vector<String> GetKeys(void) const;
 
        static Object::Ptr GetPrototype(void);
+       
+       virtual Object::Ptr Clone(void) const;
 
 private:
        std::map<String, Value> m_Data; /**< The data for the dictionary. */
index 0a2440894ca2978cecec0859691e81603488a19b..1ed5eaec5afcb20cc4d0d7eb138cb4726bc006fe 100644 (file)
@@ -39,6 +39,13 @@ static void ObjectNotifyAttribute(const String& attribute)
        self->NotifyField(self->GetReflectionType()->GetFieldId(attribute));
 }
 
+static Object::Ptr ObjectClone(void)
+{
+       ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+       Object::Ptr self = static_cast<Object::Ptr>(vframe->Self);
+       return self->Clone();
+}
+
 Object::Ptr Object::GetPrototype(void)
 {
        static Dictionary::Ptr prototype;
@@ -47,6 +54,7 @@ Object::Ptr Object::GetPrototype(void)
                prototype = new Dictionary();
                prototype->Set("to_string", new Function(WrapFunction(ObjectToString), true));
                prototype->Set("notify_attribute", new Function(WrapFunction(ObjectNotifyAttribute), false));
+               prototype->Set("clone", new Function(WrapFunction(ObjectClone), true));
        }
 
        return prototype;
index 1eff59e215f6a9172a06186887418e3ebc986097..062cb9c77fbbe9e7f06c6ed3971b4e1bb927b80e 100644 (file)
@@ -101,3 +101,8 @@ void Object::NotifyField(int id, const Value& cookie)
 {
        BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
 }
+
+Object::Ptr Object::Clone(void) const
+{
+       BOOST_THROW_EXCEPTION(std::runtime_error("Object cannot be cloned."));
+}
index 3b8f41b1d4b918dc11005826bcd843e0a77562fd..7d4f2a35862b7fd72f5c927867be72ef69696d6c 100644 (file)
@@ -111,6 +111,8 @@ public:
        void InflateMutex(void);
 
        static Object::Ptr GetPrototype(void);
+       
+       virtual Object::Ptr Clone(void) const;
 
 private:
        Object(const Object& other);
index 549c5cc54d5a7b697f1b7717693ab6e537fe44ca..99d679865ea6a56b4c2f1b6b86cdbdae741a1c47 100644 (file)
@@ -104,3 +104,11 @@ Type::Ptr Value::GetReflectionType(void) const
        }
 }
 
+Value Value::Clone(void) const
+{
+       if (IsObject())
+               return static_cast<Object::Ptr>(*this)->Clone();
+       else
+               return *this;
+}
+
index b2e12cc2c7cb67c0de1c8117689d5cea27dcf582..ad75dcf59426819b71cff05e67580fc5bf94da84 100644 (file)
@@ -234,6 +234,8 @@ public:
        String GetTypeName(void) const;
 
        Type::Ptr GetReflectionType(void) const;
+       
+       Value Clone(void) const;
 
 private:
        boost::variant<boost::blank, double, bool, String, Object::Ptr> m_Value;