From 05ea1cf66cdb0681b711bacc138e883e3b98e3f5 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 30 Jul 2021 15:13:49 +0200
Subject: [PATCH] LIB: A default `loadDocument(const QString &)` is implemented
 inside the CRTPStore class

---
 src/Lib/CRTPStore.hh                  | 11 +++++++++++
 src/Lib/Document/VivyDocumentStore.cc | 18 ------------------
 src/Lib/Document/VivyDocumentStore.hh |  7 +------
 src/Lib/Script/ScriptStore.cc         | 14 --------------
 src/Lib/Script/ScriptStore.hh         |  2 --
 5 files changed, 12 insertions(+), 40 deletions(-)

diff --git a/src/Lib/CRTPStore.hh b/src/Lib/CRTPStore.hh
index 57c27661..d75be680 100644
--- a/src/Lib/CRTPStore.hh
+++ b/src/Lib/CRTPStore.hh
@@ -7,6 +7,8 @@
 #include "Utils.hh"
 #include "Uuid.hh"
 #include <QString>
+#include <QMap>
+#include <memory>
 
 #define VIVY_STORAGE_CLASS(theClassName, theDocumentName)                                          \
     VIVY_UNMOVABLE_OBJECT(theClassName)                                                            \
@@ -30,6 +32,15 @@ protected:
     explicit CRTPStore() noexcept = default;
 
 public:
+    // Load a document by its name
+    std::shared_ptr<Document> loadDocument(const QString &file)
+    {
+        auto ret      = std::make_shared<Document>(file);
+        const Uuid id = ret->getUuid();
+        documents[id] = ret;
+        return ret;
+    }
+
     // Close a document, please be sure to not used any of the dangling
     // references to the closed document...
     void closeDocument(const Uuid &uuid) noexcept
diff --git a/src/Lib/Document/VivyDocumentStore.cc b/src/Lib/Document/VivyDocumentStore.cc
index da9ff0ac..5e2ca6fb 100644
--- a/src/Lib/Document/VivyDocumentStore.cc
+++ b/src/Lib/Document/VivyDocumentStore.cc
@@ -5,24 +5,6 @@
 
 using namespace Vivy;
 
-std::shared_ptr<VivyDocument>
-VivyDocumentStore::loadDocument(const QString &file)
-{
-    const QFileInfo fileinfo(file);
-    const QString baseName = fileinfo.baseName();
-    auto ret               = std::make_shared<VivyDocument>(file);
-
-    if (ret) {
-        qDebug() << "Register document " << baseName << ret->getDocumentCapabilitiesString();
-        const Uuid uuid = ret->getUuid();
-        documents[uuid] = ret;
-        return documents[uuid];
-    } else {
-        qDebug() << "Failed to create document from " << file;
-        throw std::runtime_error("Failed to create the document");
-    }
-}
-
 std::shared_ptr<VivyDocument>
 VivyDocumentStore::newDocument(VivyDocument::Options opt)
 {
diff --git a/src/Lib/Document/VivyDocumentStore.hh b/src/Lib/Document/VivyDocumentStore.hh
index 79929f2c..c2ac6b8e 100644
--- a/src/Lib/Document/VivyDocumentStore.hh
+++ b/src/Lib/Document/VivyDocumentStore.hh
@@ -8,10 +8,6 @@
 #include "../Utils.hh"
 #include "../CRTPStore.hh"
 
-#include <QMap>
-#include <QString>
-#include <memory>
-
 namespace Vivy
 {
 class VivyDocumentStore final : public CRTPStore<VivyDocumentStore, VivyDocument> {
@@ -20,8 +16,7 @@ class VivyDocumentStore final : public CRTPStore<VivyDocumentStore, VivyDocument
 public:
     explicit VivyDocumentStore() noexcept = default;
 
-    // Create/load documents
-    std::shared_ptr<VivyDocument> loadDocument(const QString &file);
+    // Create a new empty document with no backing file for the moment
     std::shared_ptr<VivyDocument> newDocument(VivyDocument::Options opt = VivyDocument::NoOption);
 };
 
diff --git a/src/Lib/Script/ScriptStore.cc b/src/Lib/Script/ScriptStore.cc
index 959935a5..deb8b89e 100644
--- a/src/Lib/Script/ScriptStore.cc
+++ b/src/Lib/Script/ScriptStore.cc
@@ -2,17 +2,3 @@
 #include "../Uuid.hh"
 
 using namespace Vivy::Script;
-
-std::shared_ptr<ScriptDocument>
-ScriptStore::loadDocument(const QString &file)
-{
-    try {
-        auto ret        = std::make_shared<ScriptDocument>(file);
-        const Uuid uuid = ret->getUuid();
-        documents[uuid] = ret;
-        return ret;
-    } catch (const std::runtime_error &e) {
-        qCritical() << "Failed to create the Script document with:" << e.what();
-        return std::shared_ptr<ScriptDocument>(nullptr);
-    }
-}
diff --git a/src/Lib/Script/ScriptStore.hh b/src/Lib/Script/ScriptStore.hh
index f42d84a0..8cddd1eb 100644
--- a/src/Lib/Script/ScriptStore.hh
+++ b/src/Lib/Script/ScriptStore.hh
@@ -10,7 +10,5 @@ class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> {
 
 public:
     explicit ScriptStore() noexcept = default;
-
-    std::shared_ptr<ScriptDocument> loadDocument(const QString &file);
 };
 }
-- 
GitLab