changeset 199:8cae98f73d53

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Mar 2021 14:52:06 +0100
parents 7335efde0d23
children 30b210616f4f
files Framework/Plugins/IndexUnitTests.h Framework/Plugins/OrthancCppDatabasePlugin.h MySQL/Plugins/MySQLIndex.cpp MySQL/Plugins/MySQLIndex.h PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/PostgreSQLIndex.h SQLite/Plugins/SQLiteIndex.cpp SQLite/Plugins/SQLiteIndex.h
diffstat 8 files changed, 128 insertions(+), 139 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/IndexUnitTests.h	Thu Mar 18 12:24:28 2021 +0100
+++ b/Framework/Plugins/IndexUnitTests.h	Thu Mar 18 14:52:06 2021 +0100
@@ -98,44 +98,60 @@
                                             _OrthancPluginService service,
                                             const void* params)
 {
-  if (service == _OrthancPluginService_DatabaseAnswer)
+  switch (service)
   {
-    const _OrthancPluginDatabaseAnswer& answer = 
-      *reinterpret_cast<const _OrthancPluginDatabaseAnswer*>(params);
-
-    switch (answer.type)
+    case _OrthancPluginService_DatabaseAnswer:
     {
-      case _OrthancPluginDatabaseAnswerType_Attachment:
+      const _OrthancPluginDatabaseAnswer& answer = 
+        *reinterpret_cast<const _OrthancPluginDatabaseAnswer*>(params);
+
+      switch (answer.type)
       {
-        const OrthancPluginAttachment& attachment = 
-          *reinterpret_cast<const OrthancPluginAttachment*>(answer.valueGeneric);
-        CheckAttachment(attachment);
-        break;
+        case _OrthancPluginDatabaseAnswerType_Attachment:
+        {
+          const OrthancPluginAttachment& attachment = 
+            *reinterpret_cast<const OrthancPluginAttachment*>(answer.valueGeneric);
+          CheckAttachment(attachment);
+          break;
+        }
+
+        case _OrthancPluginDatabaseAnswerType_ExportedResource:
+        {
+          const OrthancPluginExportedResource& attachment = 
+            *reinterpret_cast<const OrthancPluginExportedResource*>(answer.valueGeneric);
+          CheckExportedResource(attachment);
+          break;
+        }
+
+        case _OrthancPluginDatabaseAnswerType_DicomTag:
+        {
+          const OrthancPluginDicomTag& tag = 
+            *reinterpret_cast<const OrthancPluginDicomTag*>(answer.valueGeneric);
+          CheckDicomTag(tag);
+          break;
+        }
+
+        default:
+          printf("Unhandled message: %d\n", answer.type);
+          break;
       }
 
-      case _OrthancPluginDatabaseAnswerType_ExportedResource:
-      {
-        const OrthancPluginExportedResource& attachment = 
-          *reinterpret_cast<const OrthancPluginExportedResource*>(answer.valueGeneric);
-        CheckExportedResource(attachment);
-        break;
-      }
+      return OrthancPluginErrorCode_Success;
+    }
 
-      case _OrthancPluginDatabaseAnswerType_DicomTag:
-      {
-        const OrthancPluginDicomTag& tag = 
-          *reinterpret_cast<const OrthancPluginDicomTag*>(answer.valueGeneric);
-        CheckDicomTag(tag);
-        break;
-      }
+    case _OrthancPluginService_GetExpectedDatabaseVersion:
+    {
+      const _OrthancPluginReturnSingleValue& p =
+        *reinterpret_cast<const _OrthancPluginReturnSingleValue*>(params);
+      *(p.resultUint32) = ORTHANC_DATABASE_VERSION;
+      return OrthancPluginErrorCode_Success;
+    }
 
-      default:
-        printf("Unhandled message: %d\n", answer.type);
-        break;
-    }
+    default:
+      assert(0);
+      printf("Service not emulated: %d\n", service);
+      return OrthancPluginErrorCode_NotImplemented;
   }
-
-  return OrthancPluginErrorCode_Success;
 }
 
 
@@ -163,7 +179,7 @@
 #  error Unsupported database backend
 #endif
 
-  db.RegisterOutput(new OrthancPlugins::DatabaseBackendOutput(&context, NULL));
+  db.RegisterOutput(&context, new OrthancPlugins::DatabaseBackendOutput(&context, NULL));
   db.Open();
   
 
--- a/Framework/Plugins/OrthancCppDatabasePlugin.h	Thu Mar 18 12:24:28 2021 +0100
+++ b/Framework/Plugins/OrthancCppDatabasePlugin.h	Thu Mar 18 14:52:06 2021 +0100
@@ -55,7 +55,7 @@
   }                                                               \
   catch (...)                                                     \
   {                                                               \
-    OrthancPluginLogError(backend->GetOutput().GetContext(), "Native exception"); \
+    OrthancPluginLogError(backend->GetContext(), "Native exception");   \
     return OrthancPluginErrorCode_DatabasePlugin;                 \
   }
 
@@ -71,9 +71,7 @@
    **/
   class DatabaseBackendOutput : public boost::noncopyable
   {
-    friend class DatabaseBackendAdapter;
-
-  private:
+  public:
     enum AllowedAnswers
     {
       AllowedAnswers_All,
@@ -87,15 +85,11 @@
       AllowedAnswers_Metadata
     };
 
+  private:
     OrthancPluginContext*         context_;
     OrthancPluginDatabaseContext* database_;
     AllowedAnswers                allowedAnswers_;
 
-    void SetAllowedAnswers(AllowedAnswers allowed)
-    {
-      allowedAnswers_ = allowed;
-    }
-
   public:
     DatabaseBackendOutput(OrthancPluginContext*         context,
                           OrthancPluginDatabaseContext* database) :
@@ -105,9 +99,14 @@
     {
     }
 
-    OrthancPluginContext* GetContext()
+    void SetAllowedAnswers(AllowedAnswers allowed)
     {
-      return context_;
+      allowedAnswers_ = allowed;
+    }
+
+    OrthancPluginDatabaseContext* GetDatabase() const
+    {
+      return database_;
     }
 
     void SignalDeletedAttachment(const std::string& uuid,
@@ -282,9 +281,8 @@
    **/
   class IDatabaseBackend : public boost::noncopyable
   {
-    friend class DatabaseBackendAdapter;
-
   private:
+    OrthancPluginContext*   context_;
     DatabaseBackendOutput*  output_;
 
     void Finalize()
@@ -296,14 +294,11 @@
       }
     }
 
-  protected:
-    DatabaseBackendOutput& GetOutput()
-    {
-      return *output_;
-    }
-
   public:
-    IDatabaseBackend() : output_(NULL)
+    // "context" can be NULL iff. running the unit tests
+    IDatabaseBackend() :
+      context_(NULL),
+      output_(NULL)
     {
     }
 
@@ -312,10 +307,22 @@
       Finalize();
     }
 
+    DatabaseBackendOutput& GetOutput()
+    {
+      return *output_;
+    }
+
+    OrthancPluginContext* GetContext() const
+    {
+      return context_;
+    }
+
     // This takes the ownership
-    void RegisterOutput(DatabaseBackendOutput* output)
+    void RegisterOutput(OrthancPluginContext* context,
+                        DatabaseBackendOutput* output)
     {
       Finalize();
+      context_ = context;
       output_ = output;
     }
 
@@ -563,7 +570,7 @@
                          const std::runtime_error& e)
     {
       const std::string message = "Exception in database back-end: " + std::string(e.what());
-      OrthancPluginLogError(backend->GetOutput().GetContext(), message.c_str());
+      OrthancPluginLogError(backend->GetContext(), message.c_str());
     }
 
 
@@ -706,8 +713,8 @@
         for (std::list<int64_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, *it);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), *it);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -731,8 +738,8 @@
         for (std::list<std::string>::const_iterator
                it = ids.begin(); it != ids.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             it->c_str());
         }
 
@@ -759,8 +766,8 @@
         for (std::list<std::string>::const_iterator
                it = ids.begin(); it != ids.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             it->c_str());
         }
 
@@ -785,8 +792,8 @@
         
         if (done)
         {
-          OrthancPluginDatabaseAnswerChangesDone(backend->GetOutput().context_,
-                                                 backend->GetOutput().database_);
+          OrthancPluginDatabaseAnswerChangesDone(backend->GetContext(),
+                                                 backend->GetOutput().GetDatabase());
         }
 
         return OrthancPluginErrorCode_Success;
@@ -810,8 +817,8 @@
         for (std::list<int64_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, *it);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), *it);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -835,8 +842,8 @@
         for (std::list<std::string>::const_iterator
                it = ids.begin(); it != ids.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             it->c_str());
         }
 
@@ -861,8 +868,8 @@
 
         if (done)
         {
-          OrthancPluginDatabaseAnswerExportedResourcesDone(backend->GetOutput().context_,
-                                                           backend->GetOutput().database_);
+          OrthancPluginDatabaseAnswerExportedResourcesDone(backend->GetContext(),
+                                                           backend->GetOutput().GetDatabase());
         }
         return OrthancPluginErrorCode_Success;
       }
@@ -926,8 +933,8 @@
       try
       {
         std::string s = backend->GetPublicId(id);
-        OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                          backend->GetOutput().database_,
+        OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                          backend->GetOutput().GetDatabase(),
                                           s.c_str());
 
         return OrthancPluginErrorCode_Success;
@@ -1045,8 +1052,8 @@
         for (std::list<int32_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt32(backend->GetOutput().context_,
-                                           backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerInt32(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(),
                                            *it);
         }
 
@@ -1071,8 +1078,8 @@
         for (std::list<int32_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt32(backend->GetOutput().context_,
-                                           backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerInt32(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(),
                                            *it);
         }
 
@@ -1141,8 +1148,8 @@
         std::string s;
         if (backend->LookupGlobalProperty(s, property))
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             s.c_str());
         }
 
@@ -1169,8 +1176,8 @@
         for (std::list<int64_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, *it);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), *it);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1198,8 +1205,8 @@
         for (std::list<int64_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, *it);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), *it);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1221,8 +1228,8 @@
         std::string s;
         if (backend->LookupMetadata(s, id, metadata))
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_, s.c_str());
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(), s.c_str());
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1243,8 +1250,8 @@
         int64_t parent;
         if (backend->LookupParent(parent, id))
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, parent);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), parent);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1266,8 +1273,8 @@
         OrthancPluginResourceType type;
         if (backend->LookupResource(id, type, publicId))
         {
-          OrthancPluginDatabaseAnswerResource(backend->GetOutput().context_,
-                                              backend->GetOutput().database_, 
+          OrthancPluginDatabaseAnswerResource(backend->GetContext(),
+                                              backend->GetOutput().GetDatabase(), 
                                               id, type);
         }
 
@@ -1288,8 +1295,8 @@
         int64_t id;
         if (backend->SelectPatientToRecycle(id))
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, id);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), id);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1310,8 +1317,8 @@
         int64_t id;
         if (backend->SelectPatientToRecycle(id, patientIdToAvoid))
         {
-          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
-                                           backend->GetOutput().database_, id);
+          OrthancPluginDatabaseAnswerInt64(backend->GetContext(),
+                                           backend->GetOutput().GetDatabase(), id);
         }
 
         return OrthancPluginErrorCode_Success;
@@ -1612,8 +1619,8 @@
         for (std::list<std::string>::const_iterator
                it = values.begin(); it != values.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             it->c_str());
         }
 
@@ -1673,8 +1680,8 @@
         for (std::map<int32_t, std::string>::const_iterator
                it = result.begin(); it != result.end(); ++it)
         {
-          OrthancPluginDatabaseAnswerMetadata(backend->GetOutput().context_,
-                                            backend->GetOutput().database_,
+          OrthancPluginDatabaseAnswerMetadata(backend->GetContext(),
+                                            backend->GetOutput().GetDatabase(),
                                             resourceId, it->first, it->second.c_str());
         }
         
@@ -1708,8 +1715,8 @@
 
           if (!parent.empty())
           {
-            OrthancPluginDatabaseAnswerString(backend->GetOutput().context_,
-                                              backend->GetOutput().database_,
+            OrthancPluginDatabaseAnswerString(backend->GetContext(),
+                                              backend->GetOutput().GetDatabase(),
                                               parent.c_str());
           }
         }
@@ -1852,7 +1859,7 @@
         throw std::runtime_error("Unable to register the database backend");
       }
 
-      backend.RegisterOutput(new DatabaseBackendOutput(context, database));
+      backend.RegisterOutput(context, new DatabaseBackendOutput(context, database));
     }
   };
 }
--- a/MySQL/Plugins/MySQLIndex.cpp	Thu Mar 18 12:24:28 2021 +0100
+++ b/MySQL/Plugins/MySQLIndex.cpp	Thu Mar 18 14:52:06 2021 +0100
@@ -49,14 +49,10 @@
   IDatabase* MySQLIndex::OpenInternal()
   {
     uint32_t expectedVersion = 6;
-    if (context_)
+
+    if (GetContext())   // "GetContext()" can possibly be NULL in the unit tests
     {
-      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_);
-    }
-    else
-    {
-      // This case only occurs during unit testing
-      expectedVersion = 6;
+      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext());
     }
 
     // Check the expected version of the database
@@ -273,7 +269,6 @@
 
   MySQLIndex::MySQLIndex(const MySQLParameters& parameters) :
     IndexBackend(new Factory(*this)),
-    context_(NULL),
     parameters_(parameters),
     clearAll_(false)
   {
--- a/MySQL/Plugins/MySQLIndex.h	Thu Mar 18 12:24:28 2021 +0100
+++ b/MySQL/Plugins/MySQLIndex.h	Thu Mar 18 14:52:06 2021 +0100
@@ -57,7 +57,6 @@
       }
     };
 
-    OrthancPluginContext*  context_;
     MySQLParameters        parameters_;
     bool                   clearAll_;
 
@@ -66,11 +65,6 @@
   public:
     MySQLIndex(const MySQLParameters& parameters);
 
-    void SetOrthancPluginContext(OrthancPluginContext* context)
-    {
-      context_ = context;
-    }
-
     void SetClearAll(bool clear)
     {
       clearAll_ = clear;
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Thu Mar 18 12:24:28 2021 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Thu Mar 18 14:52:06 2021 +0100
@@ -48,14 +48,10 @@
   IDatabase* PostgreSQLIndex::OpenInternal()
   {
     uint32_t expectedVersion = 6;
-    if (context_)
+
+    if (GetContext())   // "GetContext()" can possibly be NULL in the unit tests
     {
-      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_);
-    }
-    else
-    {
-      // This case only occurs during unit testing
-      expectedVersion = 6;
+      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext());
     }
 
     // Check the expected version of the database
@@ -263,7 +259,6 @@
 
   PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) :
     IndexBackend(new Factory(*this)),
-    context_(NULL),
     parameters_(parameters),
     clearAll_(false)
   {
--- a/PostgreSQL/Plugins/PostgreSQLIndex.h	Thu Mar 18 12:24:28 2021 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.h	Thu Mar 18 14:52:06 2021 +0100
@@ -57,7 +57,6 @@
       }
     };
 
-    OrthancPluginContext*  context_;
     PostgreSQLParameters   parameters_;
     bool                   clearAll_;
 
@@ -66,11 +65,6 @@
   public:
     PostgreSQLIndex(const PostgreSQLParameters& parameters);
 
-    void SetOrthancPluginContext(OrthancPluginContext* context)
-    {
-      context_ = context;
-    }
-
     void SetClearAll(bool clear)
     {
       clearAll_ = clear;
--- a/SQLite/Plugins/SQLiteIndex.cpp	Thu Mar 18 12:24:28 2021 +0100
+++ b/SQLite/Plugins/SQLiteIndex.cpp	Thu Mar 18 14:52:06 2021 +0100
@@ -37,14 +37,10 @@
   IDatabase* SQLiteIndex::OpenInternal()
   {
     uint32_t expectedVersion = 6;
-    if (context_)
+
+    if (GetContext())   // "GetContext()" can possibly be NULL in the unit tests
     {
-      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_);
-    }
-    else
-    {
-      // This case only occurs during unit testing
-      expectedVersion = 6;
+      expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext());
     }
 
     // Check the expected version of the database
@@ -138,7 +134,6 @@
 
   SQLiteIndex::SQLiteIndex(const std::string& path) :
     IndexBackend(new Factory(*this)),
-    context_(NULL),
     path_(path),
     fast_(true)
   {
@@ -151,7 +146,6 @@
 
   SQLiteIndex::SQLiteIndex() :
     IndexBackend(new Factory(*this)),
-    context_(NULL),
     fast_(true)
   {
   }
--- a/SQLite/Plugins/SQLiteIndex.h	Thu Mar 18 12:24:28 2021 +0100
+++ b/SQLite/Plugins/SQLiteIndex.h	Thu Mar 18 14:52:06 2021 +0100
@@ -58,7 +58,6 @@
       }
     };
 
-    OrthancPluginContext*  context_;
     std::string            path_;
     bool                   fast_;
 
@@ -69,11 +68,6 @@
 
     SQLiteIndex(const std::string& path);
 
-    void SetOrthancPluginContext(OrthancPluginContext* context)
-    {
-      context_ = context;
-    }
-
     void SetFast(bool fast)
     {
       fast_ = fast;