1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added some overloads to OwnedArray to let items be added from std::unique_ptrs. Also removed OwnedArray::addIfNotAlreadyThere because it's ambiguous about whether the object should be deleted if it fails to be added!

This commit is contained in:
jules 2019-05-14 15:16:15 +01:00
parent 332a9edb57
commit 62ead7dc7d
12 changed files with 94 additions and 56 deletions

View file

@ -341,7 +341,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::Config
if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:")) if (line.startsWith ("/**") && line.containsIgnoreCase ("Config:"))
{ {
std::unique_ptr<Project::ConfigFlag> config (new Project::ConfigFlag()); auto config = std::make_unique<Project::ConfigFlag>();
config->sourceModuleID = getID(); config->sourceModuleID = getID();
config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim(); config->symbol = line.fromFirstOccurrenceOf (":", false, false).trim();
@ -361,6 +361,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::Config
config->value = project.getConfigFlag (config->symbol); config->value = project.getConfigFlag (config->symbol);
i += 2; i += 2;
if (lines[i].contains ("#define " + config->symbol)) if (lines[i].contains ("#define " + config->symbol))
{ {
auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim(); auto value = lines[i].fromFirstOccurrenceOf ("#define " + config->symbol, false, true).trim();
@ -372,7 +373,7 @@ void LibraryModule::getConfigFlags (Project& project, OwnedArray<Project::Config
if (currentValue == "enabled") config->value = true; if (currentValue == "enabled") config->value = true;
else if (currentValue == "disabled") config->value = false; else if (currentValue == "disabled") config->value = false;
flags.add (config.release()); flags.add (std::move (config));
} }
} }
} }

View file

@ -1231,15 +1231,17 @@ Project::Item Project::Item::createCopy() { Item i (*this); i.state = i.
String Project::Item::getID() const { return state [Ids::ID]; } String Project::Item::getID() const { return state [Ids::ID]; }
void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); } void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); }
Drawable* Project::Item::loadAsImageFile() const std::unique_ptr<Drawable> Project::Item::loadAsImageFile() const
{ {
const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob()); const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob());
if (! mml.lockWasGained()) if (! mml.lockWasGained())
return nullptr; return nullptr;
return isValid() ? Drawable::createFromImageFile (getFile()) if (isValid())
: nullptr; return std::unique_ptr<Drawable> (Drawable::createFromImageFile (getFile()));
return {};
} }
Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid, bool isModuleCode) Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid, bool isModuleCode)

View file

@ -261,7 +261,7 @@ public:
Item findItemWithID (const String& targetId) const; // (recursive search) Item findItemWithID (const String& targetId) const; // (recursive search)
String getImageFileID() const; String getImageFileID() const;
Drawable* loadAsImageFile() const; std::unique_ptr<Drawable> loadAsImageFile() const;
//============================================================================== //==============================================================================
Value getNameValue(); Value getNameValue();

View file

@ -2385,15 +2385,11 @@ private:
void getIconImages (OwnedArray<Drawable>& images) const void getIconImages (OwnedArray<Drawable>& images) const
{ {
std::unique_ptr<Drawable> bigIcon (getBigIcon()); if (auto icon = getBigIcon())
images.add (std::move (icon));
if (bigIcon != nullptr) if (auto icon = getSmallIcon())
images.add (bigIcon.release()); images.add (std::move (icon));
std::unique_ptr<Drawable> smallIcon (getSmallIcon());
if (smallIcon != nullptr)
images.add (smallIcon.release());
} }
void createiOSIconFiles (File appIconSet) const void createiOSIconFiles (File appIconSet) const

View file

@ -819,12 +819,12 @@ void ProjectExporter::createDefaultConfigs()
} }
} }
Drawable* ProjectExporter::getBigIcon() const std::unique_ptr<Drawable> ProjectExporter::getBigIcon() const
{ {
return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile(); return project.getMainGroup().findItemWithID (settings [Ids::bigIcon]).loadAsImageFile();
} }
Drawable* ProjectExporter::getSmallIcon() const std::unique_ptr<Drawable> ProjectExporter::getSmallIcon() const
{ {
return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile(); return project.getMainGroup().findItemWithID (settings [Ids::smallIcon]).loadAsImageFile();
} }
@ -833,8 +833,8 @@ Image ProjectExporter::getBestIconForSize (int size, bool returnNullIfNothingBig
{ {
Drawable* im = nullptr; Drawable* im = nullptr;
std::unique_ptr<Drawable> im1 (getSmallIcon()); auto im1 = getSmallIcon();
std::unique_ptr<Drawable> im2 (getBigIcon()); auto im2 = getBigIcon();
if (im1 != nullptr && im2 != nullptr) if (im1 != nullptr && im2 != nullptr)
{ {

View file

@ -179,8 +179,8 @@ public:
void addProjectPathToBuildPathList (StringArray&, const RelativePath&, int index = -1) const; void addProjectPathToBuildPathList (StringArray&, const RelativePath&, int index = -1) const;
Drawable* getBigIcon() const; std::unique_ptr<Drawable> getBigIcon() const;
Drawable* getSmallIcon() const; std::unique_ptr<Drawable> getSmallIcon() const;
Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const; Image getBestIconForSize (int size, bool returnNullIfNothingBigEnough) const;
String getExporterIdentifierMacro() const String getExporterIdentifierMacro() const

View file

@ -172,7 +172,7 @@ void ProjectSaver::writeProjects (const OwnedArray<LibraryModule>& modules, cons
if (specifiedExporterToSave.isNotEmpty() && exp->getName() != specifiedExporterToSave) if (specifiedExporterToSave.isNotEmpty() && exp->getName() != specifiedExporterToSave)
continue; continue;
auto* exporter = exporters.add (exp.exporter.release()); auto exporter = exporters.add (std::move (exp.exporter));
exporter->initialiseDependencyPathValues(); exporter->initialiseDependencyPathValues();

View file

@ -419,7 +419,7 @@ struct PluginTreeUtils
if (current->plugins.size() + current->subFolders.size() > 0) if (current->plugins.size() + current->subFolders.size() > 0)
{ {
current->folder = lastType; current->folder = lastType;
tree.subFolders.add (current.release()); tree.subFolders.add (std::move (current));
current.reset (new KnownPluginList::PluginTree()); current.reset (new KnownPluginList::PluginTree());
} }
@ -432,7 +432,7 @@ struct PluginTreeUtils
if (current->plugins.size() + current->subFolders.size() > 0) if (current->plugins.size() + current->subFolders.size() > 0)
{ {
current->folder = lastType; current->folder = lastType;
tree.subFolders.add (current.release()); tree.subFolders.add (std::move (current));
} }
} }

View file

@ -146,7 +146,7 @@ public:
@see getUnchecked @see getUnchecked
*/ */
inline ObjectClass* operator[] (const int index) const noexcept inline ObjectClass* operator[] (int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
return values.getValueWithDefault (index); return values.getValueWithDefault (index);
@ -157,7 +157,7 @@ public:
This is a faster and less safe version of operator[] which doesn't check the index passed in, so This is a faster and less safe version of operator[] which doesn't check the index passed in, so
it can be used when you're sure the index is always going to be legal. it can be used when you're sure the index is always going to be legal.
*/ */
inline ObjectClass* getUnchecked (const int index) const noexcept inline ObjectClass* getUnchecked (int index) const noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
return values[index]; return values[index];
@ -267,13 +267,55 @@ public:
@returns the new object that was added @returns the new object that was added
@see set, insert, addIfNotAlreadyThere, addSorted @see set, insert, addIfNotAlreadyThere, addSorted
*/ */
ObjectClass* add (ObjectClass* newObject) noexcept ObjectClass* add (ObjectClass* newObject)
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
values.add (newObject); values.add (newObject);
return newObject; return newObject;
} }
/** Appends a new object to the end of the array.
Note that the this object will be deleted by the OwnedArray when it
is removed, so be careful not to delete it somewhere else.
Also be careful not to add the same object to the array more than once,
as this will obviously cause deletion of dangling pointers.
@param newObject the new object to add to the array
@returns the new object that was added
@see set, insert, addIfNotAlreadyThere, addSorted
*/
ObjectClass* add (std::unique_ptr<ObjectClass> newObject)
{
return add (newObject.release());
}
/** Inserts a new object into the array at the given index.
Note that the this object will be deleted by the OwnedArray when it
is removed, so be careful not to delete it somewhere else.
If the index is less than 0 or greater than the size of the array, the
element will be added to the end of the array.
Otherwise, it will be inserted into the array, moving all the later elements
along to make room.
Be careful not to add the same object to the array more than once,
as this will obviously cause deletion of dangling pointers.
@param indexToInsertAt the index at which the new element should be inserted
@param newObject the new object to add to the array
@returns the new object that was added
@see add, addSorted, addIfNotAlreadyThere, set
*/
ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject)
{
const ScopedLockType lock (getLock());
values.insert (indexToInsertAt, newObject, 1);
return newObject;
}
/** Inserts a new object into the array at the given index. /** Inserts a new object into the array at the given index.
Note that the this object will be deleted by the OwnedArray when it Note that the this object will be deleted by the OwnedArray when it
@ -292,11 +334,9 @@ public:
@returns the new object that was added @returns the new object that was added
@see add, addSorted, addIfNotAlreadyThere, set @see add, addSorted, addIfNotAlreadyThere, set
*/ */
ObjectClass* insert (int indexToInsertAt, ObjectClass* newObject) noexcept ObjectClass* insert (int indexToInsertAt, std::unique_ptr<ObjectClass> newObject)
{ {
const ScopedLockType lock (getLock()); return insert (indexToInsertAt, newObject.release());
values.insert (indexToInsertAt, newObject, 1);
return newObject;
} }
/** Inserts an array of values into this array at a given position. /** Inserts an array of values into this array at a given position.
@ -322,25 +362,6 @@ public:
} }
} }
/** Appends a new object at the end of the array as long as the array doesn't
already contain it.
If the array already contains a matching object, nothing will be done.
@param newObject the new object to add to the array
@returns true if the new object was added, false otherwise
*/
bool addIfNotAlreadyThere (ObjectClass* newObject) noexcept
{
const ScopedLockType lock (getLock());
if (contains (newObject))
return false;
add (newObject);
return true;
}
/** Replaces an object in the array with a different one. /** Replaces an object in the array with a different one.
If the index is less than zero, this method does nothing. If the index is less than zero, this method does nothing.
@ -390,6 +411,24 @@ public:
return newObject; return newObject;
} }
/** Replaces an object in the array with a different one.
If the index is less than zero, this method does nothing.
If the index is beyond the end of the array, the new object is added to the end of the array.
Be careful not to add the same object to the array more than once,
as this will obviously cause deletion of dangling pointers.
@param indexToChange the index whose value you want to change
@param newObject the new value to set for this index.
@param deleteOldElement whether to delete the object that's being replaced with the new one
@see add, insert, remove
*/
ObjectClass* set (int indexToChange, std::unique_ptr<ObjectClass> newObject, bool deleteOldElement = true)
{
return set (indexToChange, newObject.release(), deleteOldElement);
}
/** Adds elements from another array to the end of this array. /** Adds elements from another array to the end of this array.
@param arrayToAddFrom the array from which to copy the elements @param arrayToAddFrom the array from which to copy the elements
@ -468,14 +507,14 @@ public:
@see add, sort, indexOfSorted @see add, sort, indexOfSorted
*/ */
template <class ElementComparator> template <class ElementComparator>
int addSorted (ElementComparator& comparator, ObjectClass* const newObject) noexcept int addSorted (ElementComparator& comparator, ObjectClass* newObject) noexcept
{ {
// If you pass in an object with a static compareElements() method, this // If you pass in an object with a static compareElements() method, this
// avoids getting warning messages about the parameter being unused // avoids getting warning messages about the parameter being unused
ignoreUnused (comparator); ignoreUnused (comparator);
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
const int index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size()); auto index = findInsertIndexInSortedArray (comparator, values.begin(), newObject, 0, values.size());
insert (index, newObject); insert (index, newObject);
return index; return index;
} }
@ -493,7 +532,7 @@ public:
@see addSorted, sort @see addSorted, sort
*/ */
template <typename ElementComparator> template <typename ElementComparator>
int indexOfSorted (ElementComparator& comparator, const ObjectClass* const objectToLookFor) const noexcept int indexOfSorted (ElementComparator& comparator, const ObjectClass* objectToLookFor) const noexcept
{ {
// If you pass in an object with a static compareElements() method, this // If you pass in an object with a static compareElements() method, this
// avoids getting warning messages about the parameter being unused // avoids getting warning messages about the parameter being unused
@ -722,7 +761,7 @@ public:
the array won't have to keep dynamically resizing itself as the elements the array won't have to keep dynamically resizing itself as the elements
are added, and it'll therefore be more efficient. are added, and it'll therefore be more efficient.
*/ */
void ensureStorageAllocated (const int minNumElements) noexcept void ensureStorageAllocated (int minNumElements) noexcept
{ {
const ScopedLockType lock (getLock()); const ScopedLockType lock (getLock());
values.ensureAllocatedSize (minNumElements); values.ensureAllocatedSize (minNumElements);

View file

@ -1183,7 +1183,7 @@ struct JavascriptEngine::RootObject : public DynamicObject
if (matchIf (TokenTypes::comma)) if (matchIf (TokenTypes::comma))
{ {
std::unique_ptr<BlockStatement> block (new BlockStatement (location)); std::unique_ptr<BlockStatement> block (new BlockStatement (location));
block->statements.add (s.release()); block->statements.add (std::move (s));
block->statements.add (parseVar()); block->statements.add (parseVar());
return block.release(); return block.release();
} }

View file

@ -146,7 +146,7 @@ bool UndoManager::perform (UndoableAction* newAction)
} }
totalUnitsStored += action->getSizeInUnits(); totalUnitsStored += action->getSizeInUnits();
actionSet->actions.add (action.release()); actionSet->actions.add (std::move (action));
newTransaction = false; newTransaction = false;
moveFutureTransactionsToStash(); moveFutureTransactionsToStash();

View file

@ -253,7 +253,7 @@ bool DirectoryContentsList::addFile (const File& file, const bool isDir,
if (files.getUnchecked(i)->filename == info->filename) if (files.getUnchecked(i)->filename == info->filename)
return false; return false;
files.add (info.release()); files.add (std::move (info));
std::sort (files.begin(), files.end(), [] (const FileInfo* a, const FileInfo* b) std::sort (files.begin(), files.end(), [] (const FileInfo* a, const FileInfo* b)
{ {