From 6420ab31b6746281e913ad411d595d608d34262d Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 17 Aug 2023 14:43:55 +0100 Subject: [PATCH] DynamicObject: Update signature of clone to return unique_ptr --- BREAKING-CHANGES.txt | 27 +++++++++++++++++++ .../containers/juce_DynamicObject.cpp | 8 +++--- .../juce_core/containers/juce_DynamicObject.h | 2 +- modules/juce_core/containers/juce_Variant.cpp | 2 +- .../juce_core/javascript/juce_Javascript.cpp | 2 +- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/BREAKING-CHANGES.txt b/BREAKING-CHANGES.txt index 5835c94a0a..8602cbbfaf 100644 --- a/BREAKING-CHANGES.txt +++ b/BREAKING-CHANGES.txt @@ -1,6 +1,33 @@ JUCE breaking changes ===================== +develop +======= + +Change +------ +DynamicObject::clone now returns unique_ptr instead of +ReferenceCountedObjectPtr. + +Possible Issues +--------------- +Overrides of this function using the old signature will fail to compile. +The result of this function may need to be manually converted to a +ReferenceCountedObjectPtr. + +Workaround +---------- +Update overrides to use the new signature. +If necessary, manually construct a ReferenceCountedObjectPtr at call sites. + +Rationale +--------- +It's easy to safely upgrade a unique_ptr to a shared/refcounted pointer. +However, it's not so easy to convert safely in the opposite direction. +Generally, returning unique_ptrs rather than refcounted pointers leads to more +flexible APIs. + + Version 7.0.7 ============= diff --git a/modules/juce_core/containers/juce_DynamicObject.cpp b/modules/juce_core/containers/juce_DynamicObject.cpp index 544884580c..19aff6b29e 100644 --- a/modules/juce_core/containers/juce_DynamicObject.cpp +++ b/modules/juce_core/containers/juce_DynamicObject.cpp @@ -87,11 +87,11 @@ void DynamicObject::cloneAllProperties() *v = v->clone(); } -DynamicObject::Ptr DynamicObject::clone() +std::unique_ptr DynamicObject::clone() const { - Ptr d (new DynamicObject (*this)); - d->cloneAllProperties(); - return d; + auto result = std::make_unique (*this); + result->cloneAllProperties(); + return result; } void DynamicObject::writeAsJSON (OutputStream& out, const int indentLevel, const bool allOnOneLine, int maximumDecimalPlaces) diff --git a/modules/juce_core/containers/juce_DynamicObject.h b/modules/juce_core/containers/juce_DynamicObject.h index e9c090dcfe..463cc1bb67 100644 --- a/modules/juce_core/containers/juce_DynamicObject.h +++ b/modules/juce_core/containers/juce_DynamicObject.h @@ -107,7 +107,7 @@ public: with a (deep) copy of all of its properties. Subclasses can override this to implement their own custom copy routines. */ - virtual Ptr clone(); + virtual std::unique_ptr clone() const; //============================================================================== /** Writes this object to a text stream in JSON format. diff --git a/modules/juce_core/containers/juce_Variant.cpp b/modules/juce_core/containers/juce_Variant.cpp index 12b1394f83..9f6e1c2a38 100644 --- a/modules/juce_core/containers/juce_Variant.cpp +++ b/modules/juce_core/containers/juce_Variant.cpp @@ -314,7 +314,7 @@ struct var::VariantType static var objectClone (const var& original) { if (auto* d = original.getDynamicObject()) - return d->clone().get(); + return d->clone().release(); jassertfalse; // can only clone DynamicObjects! return {}; diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 3a1f9ba8b9..79a60f2ff6 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -833,7 +833,7 @@ struct JavascriptEngine::RootObject : public DynamicObject tb.parseFunctionParamsAndBody (*this); } - DynamicObject::Ptr clone() override { return *new FunctionObject (*this); } + std::unique_ptr clone() const override { return std::make_unique (*this); } void writeAsJSON (OutputStream& out, int /*indentLevel*/, bool /*allOnOneLine*/, int /*maximumDecimalPlaces*/) override {