Skip to content

Commit 5829270

Browse files
committed
[ADD] new singleton model (with creation/destroy by use at beginning and end)
1 parent dba9ab7 commit 5829270

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

ImGuiFileDialog.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ class IGFD_API FileDialog : public PlacesFeature, public KeyExplorerFeature, pub
882882
ImGuiWindowFlags m_CurrentDisplayedFlags;
883883

884884
public:
885+
#ifndef NEW_SINGLETON
885886
// Singleton for easier accces form anywhere but only one dialog at a time
886887
// vCopy or vForce can be used for share a memory pointer in a new memory space like a dll module
887888
static FileDialog* Instance(FileDialog* vCopy = nullptr, bool vForce = false) {
@@ -895,6 +896,28 @@ class IGFD_API FileDialog : public PlacesFeature, public KeyExplorerFeature, pub
895896
}
896897
return &_instance;
897898
}
899+
#else // NEW_SINGLETON
900+
static std::unique_ptr<FileDialog>& initSingleton(FileDialog* vCopy = nullptr, bool vForce = false) {
901+
static auto mp_instance = std::unique_ptr<FileDialog>(new FileDialog());
902+
static std::unique_ptr<FileDialog> mp_instance_copy;
903+
if (vCopy != nullptr || vForce) {
904+
if (vCopy != nullptr) {
905+
mp_instance_copy = std::unique_ptr<FileDialog>(vCopy);
906+
} else {
907+
mp_instance_copy.release(); // frees the internal borrowed pointer without deletion
908+
}
909+
}
910+
if (mp_instance_copy != nullptr) {
911+
return mp_instance_copy;
912+
}
913+
return mp_instance;
914+
}
915+
static FileDialog& ref() { return *initSingleton().get(); }
916+
static void unitSingleton() {
917+
initSingleton(nullptr, true); // frees the borrowed pointer in case
918+
initSingleton().reset(); // else the reset with destroy the borrowed pointer
919+
}
920+
#endif // NEW_SINGLETON
898921

899922
public:
900923
FileDialog(); // ImGuiFileDialog Constructor. can be used for have many dialog at same time (not possible with singleton)

0 commit comments

Comments
 (0)