From 41bc46fd0b00b587559798bedde082b0bbae7e2e Mon Sep 17 00:00:00 2001 From: tpoole Date: Sat, 13 May 2017 10:03:22 +0100 Subject: [PATCH] Code cleanup --- .../juce_core/misc/juce_StdFunctionCompat.cpp | 76 +++++++++++++------ .../juce_core/misc/juce_StdFunctionCompat.h | 50 +++++------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/modules/juce_core/misc/juce_StdFunctionCompat.cpp b/modules/juce_core/misc/juce_StdFunctionCompat.cpp index a4d85c671d..035aeeeeed 100644 --- a/modules/juce_core/misc/juce_StdFunctionCompat.cpp +++ b/modules/juce_core/misc/juce_StdFunctionCompat.cpp @@ -35,9 +35,40 @@ namespace FunctionTestsHelpers void incrementArgument (int& x) { x++; }; double multiply (double x, double a) noexcept { return a * x; }; - struct AddOne + struct BigData { - int operator()(int i) const { return i + 1; } + BigData() + { + for (auto i = 0; i < bigDataSize; ++i) + content[i] = i + 1; + } + + int sum() const + { + int result = 0; + for (auto i = 0; i < bigDataSize; ++i) + result += content[i]; + + return result; + } + + static const int bigDataSize = 32, + bigDataSum = bigDataSize * (bigDataSize + 1) / 2; + int content[bigDataSize]; + }; + + struct FunctionObject + { + FunctionObject() {} + + FunctionObject (const FunctionObject& other) + { + bigData = new BigData (*other.bigData); + } + + int operator()(int i) const { return bigData->sum() + i; } + + ScopedPointer bigData { new BigData() }; }; } @@ -48,12 +79,7 @@ public: void runTest() override { - struct BigData - { - int content[32]; - }; - BigData bigData; - bigData.content[0] = 8; + FunctionTestsHelpers::BigData bigData; { beginTest ("Functions"); @@ -71,8 +97,8 @@ public: { beginTest ("Function objects"); - std::function f1 = FunctionTestsHelpers::AddOne(); - expectEquals (f1 (5), 6); + std::function f1 = FunctionTestsHelpers::FunctionObject(); + expectEquals (f1 (5), FunctionTestsHelpers::BigData::bigDataSum + 5); } { @@ -81,8 +107,8 @@ public: std::function fStack ([]() { return 3; }); expectEquals (fStack(), 3); - std::function fHeap ([=]() { return bigData.content[0]; }); - expectEquals (fHeap(), 8); + std::function fHeap ([=]() { return bigData.sum(); }); + expectEquals (fHeap(), FunctionTestsHelpers::BigData::bigDataSum); } { @@ -103,7 +129,7 @@ public: std::function fStack ([]() { return 3; }); - std::function fHeap ([=]() { return bigData.content[0]; }); + std::function fHeap ([=]() { return bigData.sum(); }); { beginTest ("copy constructor"); @@ -112,7 +138,7 @@ public: expectEquals (f1(), 3); std::function f2 (fHeap); - expectEquals (f2(), 8); + expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum); std::function f3 (fEmpty); if (f3) @@ -128,10 +154,10 @@ public: std::function f2; f2 = fHeap; - expectEquals (f2(), 8); + expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum); f1 = fHeap; - expectEquals (f1(), 8); + expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum); f2 = fStack; expectEquals (f2(), 3); @@ -144,21 +170,21 @@ public: { beginTest ("move constructor"); - ScopedPointer> fStackTmp = new std::function (fStack); + ScopedPointer> fStackTmp (new std::function (fStack)); std::function f1 (static_cast&&> (*fStackTmp)); fStackTmp = nullptr; expectEquals (f1(), 3); - ScopedPointer> fHeapTmp = new std::function (fHeap); + ScopedPointer> fHeapTmp (new std::function (fHeap)); std::function f2 (static_cast&&> (*fHeapTmp)); if (*fHeapTmp) expect (false); fHeapTmp = nullptr; - expectEquals (f2(), 8); + expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum); - ScopedPointer> fEmptyTmp = new std::function(); + ScopedPointer> fEmptyTmp (new std::function()); std::function f3 (static_cast&&> (*fEmptyTmp)); fEmptyTmp = nullptr; if (f3) @@ -169,23 +195,23 @@ public: beginTest ("move assignment"); std::function f1 (fHeap); - ScopedPointer> fStackTmp = new std::function (fStack); + ScopedPointer> fStackTmp (new std::function (fStack)); f1 = static_cast&&> (*fStackTmp); fStackTmp = nullptr; expectEquals (f1(), 3); std::function f2 (fStack); - ScopedPointer> fHeapTmp = new std::function (fHeap); + ScopedPointer> fHeapTmp (new std::function (fHeap)); f2 = static_cast&&> (*fHeapTmp); if (*fHeapTmp) expect (false); fHeapTmp = nullptr; - expectEquals (f2(), 8); + expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum); std::function f3 (fHeap); - ScopedPointer> fEmptyTmp = new std::function (); + ScopedPointer> fEmptyTmp (new std::function()); f3 = static_cast&&> (*fEmptyTmp); fEmptyTmp = nullptr; if (f3) @@ -218,7 +244,7 @@ public: std::function f3 (fHeap); f3.swap (f1); expectEquals (f3(), 3); - expectEquals (f1(), 8); + expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum); } } }; diff --git a/modules/juce_core/misc/juce_StdFunctionCompat.h b/modules/juce_core/misc/juce_StdFunctionCompat.h index e16fee6df3..7aadfdac55 100644 --- a/modules/juce_core/misc/juce_StdFunctionCompat.h +++ b/modules/juce_core/misc/juce_StdFunctionCompat.h @@ -55,7 +55,7 @@ namespace std template function (Functor f) { - functorHolderHelper = createFunctorStorage (sizeof (FunctorHolder)); + functorHolderHelper = getFunctorStorage (sizeof (FunctorHolder)); new (functorHolderHelper) FunctorHolder (f); } @@ -125,17 +125,17 @@ namespace std struct FunctorHolderBase { virtual ~FunctorHolderBase() {}; - virtual size_t getSize() const noexcept = 0; + virtual int getSize() const noexcept = 0; virtual void copy (void*) const = 0; virtual ReturnType operator()(Args...) = 0; }; template - struct FunctorHolder : FunctorHolderBase + struct FunctorHolder : FunctorHolderBase { FunctorHolder (Functor func) : f (func) {} - size_t getSize() const noexcept override final + int getSize() const noexcept override final { return sizeof (*this); } @@ -153,48 +153,29 @@ namespace std Functor f; }; - FunctorHolderBase* createFunctorStorage (size_t size) + FunctorHolderBase* getFunctorStorage (int size) { - void* storagePointer; - - if (size > functorHolderStackSize) - { - if (heapFunctorStorage != nullptr) - { - delete [] heapFunctorStorage; - heapFunctorStorage = nullptr; - } - - heapFunctorStorage = new char [size]; - storagePointer = heapFunctorStorage; - } - else - { - storagePointer = &(stackFunctorStorage[0]); - } - - return reinterpret_cast*> (storagePointer); + return reinterpret_cast*> + (size > functorHolderStackSize ? new char [size] + : &(stackFunctorStorage[0])); } void copy (function const& other) { if (other.functorHolderHelper != nullptr) { - functorHolderHelper = createFunctorStorage (other.functorHolderHelper->getSize()); + functorHolderHelper = getFunctorStorage (other.functorHolderHelper->getSize()); other.functorHolderHelper->copy (functorHolderHelper); } } void move (function& other) { - functorHolderHelper = other.functorHolderHelper; - - if (functorHolderHelper != nullptr) + if (other.functorHolderHelper != nullptr) { - if (functorHolderHelper->getSize() > functorHolderStackSize) + if (other.functorHolderHelper->getSize() > functorHolderStackSize) { - heapFunctorStorage = other.heapFunctorStorage; - other.heapFunctorStorage = nullptr; + functorHolderHelper = other.functorHolderHelper; } else { @@ -211,14 +192,17 @@ namespace std { if (functorHolderHelper != nullptr) { - functorHolderHelper->~FunctorHolderBase(); + if (functorHolderHelper->getSize() > functorHolderStackSize) + delete[] reinterpret_cast (functorHolderHelper); + else + functorHolderHelper->~FunctorHolderBase(); + functorHolderHelper = nullptr; } } static const int functorHolderStackSize = 24; char stackFunctorStorage[functorHolderStackSize]; - char* heapFunctorStorage = nullptr; FunctorHolderBase* functorHolderHelper = nullptr; };