mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-11 23:54:18 +00:00
Code cleanup
This commit is contained in:
parent
4c03a9079b
commit
41bc46fd0b
2 changed files with 68 additions and 58 deletions
|
|
@ -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> 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<int(int)> f1 = FunctionTestsHelpers::AddOne();
|
||||
expectEquals (f1 (5), 6);
|
||||
std::function<int(int)> f1 = FunctionTestsHelpers::FunctionObject();
|
||||
expectEquals (f1 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -81,8 +107,8 @@ public:
|
|||
std::function<int()> fStack ([]() { return 3; });
|
||||
expectEquals (fStack(), 3);
|
||||
|
||||
std::function<int()> fHeap ([=]() { return bigData.content[0]; });
|
||||
expectEquals (fHeap(), 8);
|
||||
std::function<int()> fHeap ([=]() { return bigData.sum(); });
|
||||
expectEquals (fHeap(), FunctionTestsHelpers::BigData::bigDataSum);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -103,7 +129,7 @@ public:
|
|||
|
||||
std::function<int()> fStack ([]() { return 3; });
|
||||
|
||||
std::function<int()> fHeap ([=]() { return bigData.content[0]; });
|
||||
std::function<int()> fHeap ([=]() { return bigData.sum(); });
|
||||
|
||||
{
|
||||
beginTest ("copy constructor");
|
||||
|
|
@ -112,7 +138,7 @@ public:
|
|||
expectEquals (f1(), 3);
|
||||
|
||||
std::function<int()> f2 (fHeap);
|
||||
expectEquals (f2(), 8);
|
||||
expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
|
||||
|
||||
std::function<int()> f3 (fEmpty);
|
||||
if (f3)
|
||||
|
|
@ -128,10 +154,10 @@ public:
|
|||
|
||||
std::function<int()> 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<std::function<int()>> fStackTmp = new std::function<int()> (fStack);
|
||||
ScopedPointer<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
|
||||
std::function<int()> f1 (static_cast<std::function<int()>&&> (*fStackTmp));
|
||||
|
||||
fStackTmp = nullptr;
|
||||
expectEquals (f1(), 3);
|
||||
|
||||
ScopedPointer<std::function<int()>> fHeapTmp = new std::function<int()> (fHeap);
|
||||
ScopedPointer<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
|
||||
std::function<int()> f2 (static_cast<std::function<int()>&&> (*fHeapTmp));
|
||||
if (*fHeapTmp)
|
||||
expect (false);
|
||||
|
||||
fHeapTmp = nullptr;
|
||||
expectEquals (f2(), 8);
|
||||
expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
|
||||
|
||||
ScopedPointer<std::function<int()>> fEmptyTmp = new std::function<int()>();
|
||||
ScopedPointer<std::function<int()>> fEmptyTmp (new std::function<int()>());
|
||||
std::function<int()> f3 (static_cast<std::function<int()>&&> (*fEmptyTmp));
|
||||
fEmptyTmp = nullptr;
|
||||
if (f3)
|
||||
|
|
@ -169,23 +195,23 @@ public:
|
|||
beginTest ("move assignment");
|
||||
|
||||
std::function<int()> f1 (fHeap);
|
||||
ScopedPointer<std::function<int()>> fStackTmp = new std::function<int()> (fStack);
|
||||
ScopedPointer<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
|
||||
f1 = static_cast<std::function<int()>&&> (*fStackTmp);
|
||||
|
||||
fStackTmp = nullptr;
|
||||
expectEquals (f1(), 3);
|
||||
|
||||
std::function<int()> f2 (fStack);
|
||||
ScopedPointer<std::function<int()>> fHeapTmp = new std::function<int()> (fHeap);
|
||||
ScopedPointer<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
|
||||
f2 = static_cast<std::function<int()>&&> (*fHeapTmp);
|
||||
if (*fHeapTmp)
|
||||
expect (false);
|
||||
|
||||
fHeapTmp = nullptr;
|
||||
expectEquals (f2(), 8);
|
||||
expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
|
||||
|
||||
std::function<int()> f3 (fHeap);
|
||||
ScopedPointer<std::function<int()>> fEmptyTmp = new std::function<int()> ();
|
||||
ScopedPointer<std::function<int()>> fEmptyTmp (new std::function<int()>());
|
||||
f3 = static_cast<std::function<int()>&&> (*fEmptyTmp);
|
||||
fEmptyTmp = nullptr;
|
||||
if (f3)
|
||||
|
|
@ -218,7 +244,7 @@ public:
|
|||
std::function<int()> f3 (fHeap);
|
||||
f3.swap (f1);
|
||||
expectEquals (f3(), 3);
|
||||
expectEquals (f1(), 8);
|
||||
expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ namespace std
|
|||
template <typename Functor>
|
||||
function (Functor f)
|
||||
{
|
||||
functorHolderHelper = createFunctorStorage (sizeof (FunctorHolder<Functor, Result, Arguments...>));
|
||||
functorHolderHelper = getFunctorStorage (sizeof (FunctorHolder<Functor, Result, Arguments...>));
|
||||
new (functorHolderHelper) FunctorHolder<Functor, Result, Arguments...> (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 <typename Functor, typename ReturnType, typename... Args>
|
||||
struct FunctorHolder : FunctorHolderBase<Result, Arguments...>
|
||||
struct FunctorHolder : FunctorHolderBase<Result, Arguments...>
|
||||
{
|
||||
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<Result, Arguments...>* createFunctorStorage (size_t size)
|
||||
FunctorHolderBase<Result, Arguments...>* 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<FunctorHolderBase<Result, Arguments...>*> (storagePointer);
|
||||
return reinterpret_cast<FunctorHolderBase<Result, Arguments...>*>
|
||||
(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<Result, Arguments...>();
|
||||
if (functorHolderHelper->getSize() > functorHolderStackSize)
|
||||
delete[] reinterpret_cast<char*> (functorHolderHelper);
|
||||
else
|
||||
functorHolderHelper->~FunctorHolderBase<Result, Arguments...>();
|
||||
|
||||
functorHolderHelper = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static const int functorHolderStackSize = 24;
|
||||
char stackFunctorStorage[functorHolderStackSize];
|
||||
char* heapFunctorStorage = nullptr;
|
||||
|
||||
FunctorHolderBase<Result, Arguments...>* functorHolderHelper = nullptr;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue