mirror of
https://github.com/leozide/leocad.git
synced 2026-10-26 22:10:32 +00:00
Own library primitives with unique pointers
Store primitive entries as unique_ptr values so source destruction and replacement release their owned data automatically. This removes the manual deletion loop and prevents leaking an overwritten entry during discovery while preserving borrowed pointers used by loaders. Remove the duplicate IsPrimitive lookup and use FindPrimitive for directory precedence checks and primitive detection during model import. Validated with a warning-free Qt 6.10 Debug build, all 58 offscreen lifecycle groups, and clean diff whitespace checks.
This commit is contained in:
1 parent
b6ab0c4c3b
commit
03d58100cb
3 files changed
+8
-24
No files matched your search
+6
-15
@@ -732,13 +732,13 @@ bool lcPiecesLibrary::OpenArchive(std::unique_ptr<lcFile> File, lcZipFileType Zi
|
||||
PieceData->SetZipFile(ZipFileType, FileIdx);
|
||||
}
|
||||
else
|
||||
Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NormalizedName.c_str()), ZipFileType, FileIdx, false, false, true);
|
||||
Source->Primitives[Name] = std::make_unique<lcLibraryPrimitive>(QString(), FileInfo.file_name + (Name - NormalizedName.c_str()), ZipFileType, FileIdx, false, false, true);
|
||||
}
|
||||
else if (!strncmp(Name, "P/", 2))
|
||||
{
|
||||
Name += 2;
|
||||
|
||||
Source->Primitives[Name] = new lcLibraryPrimitive(QString(), FileInfo.file_name + (Name - NormalizedName.c_str()), ZipFileType, FileIdx, IsStudPrimitive(Name), IsStudStylePrimitive(Name), false);
|
||||
Source->Primitives[Name] = std::make_unique<lcLibraryPrimitive>(QString(), FileInfo.file_name + (Name - NormalizedName.c_str()), ZipFileType, FileIdx, IsStudPrimitive(Name), IsStudStylePrimitive(Name), false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -850,14 +850,14 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress)
|
||||
if (Name.size() <= 4 || Name.compare(Name.size() - 4, 4, ".DAT"))
|
||||
continue;
|
||||
|
||||
if (BaseFolderIdx > 0 && IsPrimitive(Name.c_str()))
|
||||
if (BaseFolderIdx > 0 && FindPrimitive(Name.c_str()))
|
||||
continue;
|
||||
|
||||
if (BaseFolderIdx == static_cast<int>(lcLibraryFolderType::Unofficial))
|
||||
mHasUnofficial = true;
|
||||
|
||||
const bool SubFile = SubFileDirectories[DirectoryIdx];
|
||||
Source->Primitives[Name] = new lcLibraryPrimitive(std::move(FileName), RelativeName, lcZipFileType::Count, 0, !SubFile && IsStudPrimitive(Name.c_str()), IsStudStylePrimitive(Name.c_str()), SubFile);
|
||||
Source->Primitives[Name] = std::make_unique<lcLibraryPrimitive>(std::move(FileName), RelativeName, lcZipFileType::Count, 0, !SubFile && IsStudPrimitive(Name.c_str()), IsStudStylePrimitive(Name.c_str()), SubFile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2186,7 +2186,7 @@ void lcPiecesLibrary::UpdateStudStyleAndColors(lcStudStyle StudStyle, bool StudC
|
||||
{
|
||||
for (const auto& PrimitiveIt : Source->Primitives)
|
||||
{
|
||||
lcLibraryPrimitive* Primitive = PrimitiveIt.second;
|
||||
lcLibraryPrimitive* Primitive = PrimitiveIt.second.get();
|
||||
|
||||
if (Primitive->mStudStyle || Primitive->mMeshData.mHasStyleStud)
|
||||
Primitive->Unload();
|
||||
@@ -2229,15 +2229,6 @@ void lcPiecesLibrary::UpdateStudStyleAndColors(lcStudStyle StudStyle, bool StudC
|
||||
mAssetLoader->ResumeQueuedWork();
|
||||
}
|
||||
|
||||
bool lcPiecesLibrary::IsPrimitive(const char* Name) const
|
||||
{
|
||||
for (const std::unique_ptr<lcLibrarySource>& Source : mSources)
|
||||
if (Source->Primitives.find(Name) != Source->Primitives.end())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
lcLibraryPrimitive* lcPiecesLibrary::FindPrimitive(const char* Name) const
|
||||
{
|
||||
for (const std::unique_ptr<lcLibrarySource>& Source : mSources)
|
||||
@@ -2245,7 +2236,7 @@ lcLibraryPrimitive* lcPiecesLibrary::FindPrimitive(const char* Name) const
|
||||
const auto PrimitiveIt = Source->Primitives.find(Name);
|
||||
|
||||
if (PrimitiveIt != Source->Primitives.end())
|
||||
return PrimitiveIt->second;
|
||||
return PrimitiveIt->second.get();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
||||
+1
-8
@@ -107,19 +107,13 @@ struct lcLibrarySource
|
||||
{
|
||||
lcLibrarySource() = default;
|
||||
|
||||
~lcLibrarySource()
|
||||
{
|
||||
for (const auto& PrimitiveIt : Primitives)
|
||||
delete PrimitiveIt.second;
|
||||
}
|
||||
|
||||
lcLibrarySource(const lcLibrarySource&) = delete;
|
||||
lcLibrarySource(lcLibrarySource&&) = delete;
|
||||
lcLibrarySource& operator=(const lcLibrarySource&) = delete;
|
||||
lcLibrarySource& operator=(lcLibrarySource&&) = delete;
|
||||
|
||||
lcLibrarySourceType Type;
|
||||
std::map<std::string, lcLibraryPrimitive*> Primitives;
|
||||
std::map<std::string, std::unique_ptr<lcLibraryPrimitive>> Primitives;
|
||||
};
|
||||
|
||||
// Coordinates asset sources, loading, settings and shared render buffers.
|
||||
@@ -265,7 +259,6 @@ public:
|
||||
std::string GetPartId(const lcPieceData* PieceData) const;
|
||||
|
||||
// Queries primitive sources; FindPrimitive() returns a borrowed entry.
|
||||
bool IsPrimitive(const char* Name) const;
|
||||
lcLibraryPrimitive* FindPrimitive(const char* Name) const;
|
||||
|
||||
// Loads shared primitive data, waiting for another worker when necessary.
|
||||
|
||||
+1
-1
@@ -999,7 +999,7 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
|
||||
|
||||
QByteArray CleanId = PartId.toLatin1().toUpper().replace('\\', '/');
|
||||
|
||||
if (Library->IsPrimitive(CleanId.constData()))
|
||||
if (Library->FindPrimitive(CleanId.constData()))
|
||||
{
|
||||
mFileLines.append(OriginalLine);
|
||||
mHasDirectGeometry = true;
|
||||
|
||||
Reference in new issue
Block a user