]> OCCT Git - occt.git/commitdiff
DRAW - Static plugin factory refactor #233
authordpasukhi <dpasukhi@opencascade.com>
Sat, 4 Jan 2025 11:35:31 +0000 (11:35 +0000)
committerdpasukhi <dpasukhi@opencascade.com>
Sat, 4 Jan 2025 12:07:25 +0000 (12:07 +0000)
Reorganize Static plug-in mapping to be more transparent.

src/DRAWEXE/DRAWEXE.cxx
src/DRAWEXE/EXTERNLIB_STATIC

index 9f3db744c256b8fd39d1aaa831b0d557a351af65..d8f9a4895ab69a4e6b0a5965a92d93381a9d1123 100644 (file)
@@ -13,8 +13,8 @@
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <Draw.hxx>
 #include <DBRep.hxx>
+#include <Draw.hxx>
 #include <DrawTrSurf.hxx>
 #include <Message.hxx>
 #include <Message_PrinterOStream.hxx>
   #include <BOPTest.hxx>
   #include <DPrsStd.hxx>
   #if defined(HAVE_OPENGL) || defined(HAVE_GLES2)
-  #include <OpenGlTest.hxx>
+    #include <OpenGlTest.hxx>
   #endif
   #include <TObjDRAW.hxx>
   #include <ViewerTest.hxx>
-  #include <XSDRAW.hxx>
   #include <XDEDRAW.hxx>
-  #include <XSDRAWSTEP.hxx>
-  #include <XSDRAWIGES.hxx>
+  #include <XSDRAW.hxx>
+  #include <XSDRAWDE.hxx>
   #include <XSDRAWGLTF.hxx>
+  #include <XSDRAWIGES.hxx>
   #include <XSDRAWOBJ.hxx>
   #include <XSDRAWPLY.hxx>
-  #include <XSDRAWVRML.hxx>
+  #include <XSDRAWSTEP.hxx>
   #include <XSDRAWSTL.hxx>
+  #include <XSDRAWVRML.hxx>
 #endif
 
-Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand);
+Standard_IMPORT Standard_Boolean Draw_Interprete(const char* theCommand);
 
 #if defined(__EMSCRIPTEN__)
-#include <emscripten/bind.h>
-#include <emscripten/emscripten.h>
-#include <emscripten/threading.h>
+  #include <emscripten/bind.h>
+  #include <emscripten/emscripten.h>
+  #include <emscripten/threading.h>
 
 //! Signal async command completion to Module.evalAsyncCompleted callback.
 EM_JS(void, occJSEvalAsyncCompleted, (int theResult), {
-  if (Module.evalAsyncCompleted != undefined) {
-    Module.evalAsyncCompleted (theResult);
-  } else {
-    console.error ("Module.evalAsyncCompleted() is undefined");
+  if (Module.evalAsyncCompleted != undefined)
+  {
+    Module.evalAsyncCompleted(theResult);
+  }
+  else
+  {
+    console.error("Module.evalAsyncCompleted() is undefined");
   }
 });
 
@@ -64,14 +68,14 @@ class DRAWEXE
 {
 public:
   //! Evaluate Tcl command.
-  static int eval (const std::string& theCommand)
+  static int eval(const std::string& theCommand)
   {
     int aRes = 0;
     try
     {
       OCC_CATCH_SIGNALS
       //aRes = Draw::GetInterpretor().Eval (theCommand.c_str());
-      aRes = Draw_Interprete (theCommand.c_str()) ? 1 : 0;
+      aRes = Draw_Interprete(theCommand.c_str()) ? 1 : 0;
     }
     catch (Standard_Failure& anExcept)
     {
@@ -81,54 +85,56 @@ public:
   }
 
   //! Check if Tcl command is complete.
-  static bool isComplete (const std::string& theCommand)
+  static bool isComplete(const std::string& theCommand)
   {
-    return Draw::GetInterpretor().Complete (theCommand.c_str());
+    return Draw::GetInterpretor().Complete(theCommand.c_str());
   }
 
   //! Evaluate Tcl command asynchronously.
-  static void evalAsync (const std::string& theCommand)
+  static void evalAsync(const std::string& theCommand)
   {
   #if defined(__EMSCRIPTEN_PTHREADS__)
-    std::string* aCmdPtr = new std::string (theCommand);
-    OSD_Thread aThread (&evalAsyncEntry);
-    aThread.Run (aCmdPtr);
+    std::string* aCmdPtr = new std::string(theCommand);
+    OSD_Thread   aThread(&evalAsyncEntry);
+    aThread.Run(aCmdPtr);
   #else
     // fallback synchronous implementation
-    int aRes = eval (theCommand);
-    occJSEvalAsyncCompleted (aRes);
+    int aRes = eval(theCommand);
+    occJSEvalAsyncCompleted(aRes);
   #endif
   }
 
-#if defined(__EMSCRIPTEN_PTHREADS__)
+  #if defined(__EMSCRIPTEN_PTHREADS__)
 private:
   //! Thread entry for async command execution.
-  static Standard_Address evalAsyncEntry (Standard_Address theData)
+  static Standard_Address evalAsyncEntry(Standard_Address theData)
   {
-    OSD::SetSignal (false);
-    std::string* aCmdPtr = (std::string* )theData;
-    const std::string aCmd = *aCmdPtr;
+    OSD::SetSignal(false);
+    std::string*      aCmdPtr = (std::string*)theData;
+    const std::string aCmd    = *aCmdPtr;
     delete aCmdPtr;
-    int aRes = eval (aCmd);
-    emscripten_async_run_in_main_runtime_thread (EM_FUNC_SIG_VI, evalAsyncCompletedEntry, aRes);
+    int aRes = eval(aCmd);
+    emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_VI, evalAsyncCompletedEntry, aRes);
     return 0;
   }
 
   //! Notify Module.evalAsyncCompleted about async cmd completion.
-  static void evalAsyncCompletedEntry (int theResult)
-  {
-    occJSEvalAsyncCompleted (theResult);
-  }
-#endif
+  static void evalAsyncCompletedEntry(int theResult) { occJSEvalAsyncCompleted(theResult); }
+  #endif
 };
 
 //! Print message to Module.printMessage callback.
 EM_JS(void, occJSPrintMessage, (const char* theStr, int theGravity), {
-  if (Module.printMessage != undefined && Module.printMessage != null) {
-    Module.printMessage (UTF8ToString(theStr), theGravity);
-  } else if (Module.print != undefined && Module.print != null) {
-    Module.print (UTF8ToString(theStr));
-  } else {
+  if (Module.printMessage != undefined && Module.printMessage != null)
+  {
+    Module.printMessage(UTF8ToString(theStr), theGravity);
+  }
+  else if (Module.print != undefined && Module.print != null)
+  {
+    Module.print(UTF8ToString(theStr));
+  }
+  else
+  {
     //console.info (UTF8ToString(theStr));
   }
 });
@@ -138,176 +144,116 @@ class DRAWEXE_WasmModulePrinter : public Message_Printer
 {
   DEFINE_STANDARD_RTTI_INLINE(DRAWEXE_WasmModulePrinter, Message_Printer)
 public:
-
   //! Main constructor.
-  DRAWEXE_WasmModulePrinter (const Message_Gravity theTraceLevel = Message_Info)
+  DRAWEXE_WasmModulePrinter(const Message_Gravity theTraceLevel = Message_Info)
   {
-    SetTraceLevel (theTraceLevel);
+    SetTraceLevel(theTraceLevel);
   }
 
   //! Destructor.
   virtual ~DRAWEXE_WasmModulePrinter() {}
 
 protected:
-
   //! Puts a message.
-  virtual void send (const TCollection_AsciiString& theString,
-                     const Message_Gravity theGravity) const Standard_OVERRIDE
+  virtual void send(const TCollection_AsciiString& theString,
+                    const Message_Gravity          theGravity) const Standard_OVERRIDE
   {
     if (theGravity >= myTraceLevel)
     {
-      occJSPrintMessage (theString.ToCString(), (int )theGravity);
+      occJSPrintMessage(theString.ToCString(), (int)theGravity);
     }
   }
 };
 
-EMSCRIPTEN_BINDINGS(DRAWEXE) {
-  emscripten::function("eval",       &DRAWEXE::eval);
-  emscripten::function("evalAsync",  &DRAWEXE::evalAsync);
+EMSCRIPTEN_BINDINGS(DRAWEXE)
+{
+  emscripten::function("eval", &DRAWEXE::eval);
+  emscripten::function("evalAsync", &DRAWEXE::evalAsync);
   emscripten::function("isComplete", &DRAWEXE::isComplete);
 }
 #endif
 
 #ifdef OCCT_NO_PLUGINS
+  #include <functional>
+  #include <string>
+  #include <unordered_map>
+
 //! Mimic pload command by loading pre-defined set of statically linked plugins.
-static Standard_Integer Pload (Draw_Interpretor& theDI,
-                               Standard_Integer  theNbArgs,
-                               const char**      theArgVec)
+static Standard_Integer Pload(Draw_Interpretor& theDI,
+                              Standard_Integer  theNbArgs,
+                              const char**      theArgVec)
 {
+  // Define a map of aPlugin keys to their corresponding factory methods
+  std::unordered_map<std::string, std::function<void(Draw_Interpretor&)>> aPluginMap = {
+    {"TOPTEST", BOPTest::Factory},
+    {"DCAF", DPrsStd::Factory},
+    {"AISV", ViewerTest::Factory},
+  #if defined(HAVE_OPENGL)
+    {"GL", OpenGlTest::Factory},
+    {"OPENGL", OpenGlTest::Factory},
+  #endif
+  #if defined(HAVE_GLES2)
+    {"GLES", OpenGlTest::Factory},
+    {"OPENGLES", OpenGlTest::Factory},
+  #endif
+    {"XSDRAW", XSDRAW::Factory},
+    {"XDEDRAW", XDEDRAW::Factory},
+    {"STEP", XSDRAWSTEP::Factory},
+    {"IGES", XSDRAWIGES::Factory},
+    {"PLY", XSDRAWPLY::Factory},
+    {"GLTF", XSDRAWGLTF::Factory},
+    {"VRML", XSDRAWVRML::Factory},
+    {"STL", XSDRAWSTL::Factory},
+    {"OBJ", XSDRAWOBJ::Factory},
+    {"DE", XSDRAWDE::Factory}};
+
+  // Define a map of aliases to their corresponding aPlugin keys
+  std::unordered_map<std::string, std::vector<std::string>> anAliasMap = {
+    {"DEFAULT", {"MODELING"}},
+    {"MODELING", {"TOPTEST"}},
+    {"VISUALIZATION", {"AISV"}},
+    {"OCAFKERNEL", {"DCAF"}},
+    {"DATAEXCHANGEKERNEL", {"XSDRAW", "DE"}},
+    {"OCAF", {"VISUALIZATION", "OCAFKERNEL"}},
+    {"DATAEXCHANGE", {"XDE", "VISUALIZATION"}},
+    {"XDE", {"DATAEXCHANGEKERNEL", "XDEDRAW", "STEP", "IGES", "GLTF", "OBJ", "PLY", "STL", "VRML"}},
+    {"ALL", {"MODELING", "OCAFKERNEL", "DATAEXCHANGE"}}};
+
   NCollection_IndexedMap<TCollection_AsciiString> aPlugins;
-  for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
+
+  std::function<void(const TCollection_AsciiString&)> processAlias;
+  processAlias = [&](const TCollection_AsciiString& theAlias) -> void
   {
-    TCollection_AsciiString anArg (theArgVec[anArgIter]);
-    anArg.UpperCase();
-    if (anArg == "DEFAULT")
-    {
-      aPlugins.Add ("TOPTEST");
-    }
-    else if (anArg == "MODELING")
-    {
-      aPlugins.Add ("TOPTEST");
-    }
-    else if (anArg == "VISUALIZATION")
-    {
-      aPlugins.Add ("AISV");
-    }
-    else if (anArg == "OCAFKERNEL")
-    {
-      aPlugins.Add ("DCAF");
-    }
-    else if (anArg == "DATAEXCHANGEKERNEL")
-    {
-      aPlugins.Add ("XSDRAW");
-    }
-    else if (anArg == "OCAF")
+    auto anAliasIt = anAliasMap.find(theAlias.ToCString());
+    if (anAliasIt != anAliasMap.end())
     {
-      aPlugins.Add ("AISV");
-      aPlugins.Add ("DCAF");
-    }
-    else if (anArg == "DATAEXCHANGE")
-    {
-      aPlugins.Add ("XSDRAW");
-      aPlugins.Add ("XSDRAWSTEP");
-      aPlugins.Add ("XSDRAWIGES");
-      aPlugins.Add ("XSDRAWGLTF");
-      aPlugins.Add ("XSDRAWOBJ");
-      aPlugins.Add ("XSDRAWPLY");
-      aPlugins.Add ("XSDRAWVRML");
-      aPlugins.Add ("XSDRAWSTL");
-      aPlugins.Add ("XDEDRAW");
-      aPlugins.Add ("AISV");
-    }
-    else if (anArg == "XDE")
-    {
-      aPlugins.Add ("XSDRAW");
-      aPlugins.Add ("XDEDRAW");
-    }
-    else if (anArg == "ALL")
-    {
-      aPlugins.Add ("TOPTEST");
-      aPlugins.Add ("DCAF");
-      aPlugins.Add ("XSDRAW");
-      aPlugins.Add ("XDEDRAW");
-      aPlugins.Add ("AISV");
-      aPlugins.Add ("XSDRAWSTEP");
-      aPlugins.Add ("XSDRAWIGES");
-      aPlugins.Add ("XSDRAWGLTF");
-      aPlugins.Add ("XSDRAWOBJ");
-      aPlugins.Add ("XSDRAWPLY");
-      aPlugins.Add ("XSDRAWVRML");
-      aPlugins.Add ("XSDRAWSTL");
+      for (const auto& aPlugin : anAliasIt->second)
+      {
+        processAlias(TCollection_AsciiString(aPlugin.c_str()));
+      }
     }
     else
     {
-      aPlugins.Add (anArg);
+      aPlugins.Add(theAlias);
     }
+  };
+
+  for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
+  {
+    TCollection_AsciiString anArg(theArgVec[anArgIter]);
+    anArg.UpperCase();
+    processAlias(anArg);
   }
 
-  for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aPluginIter (aPlugins);
-       aPluginIter.More(); aPluginIter.Next())
+  for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aPluginIter(aPlugins);
+       aPluginIter.More();
+       aPluginIter.Next())
   {
     const TCollection_AsciiString& aPlugin = aPluginIter.Value();
-    if (aPlugin == "TOPTEST")
-    {
-      BOPTest::Factory (theDI);
-    }
-    else if (aPlugin == "DCAF")
-    {
-      DPrsStd::Factory (theDI);
-    }
-    else if (aPlugin == "AISV")
-    {
-      ViewerTest::Factory (theDI);
-    }
-  #if defined(HAVE_OPENGL)
-    else if (aPlugin == "GL"
-          || aPlugin == "OPENGL")
-    {
-      OpenGlTest::Factory (theDI);
-    }
-  #endif
-  #if defined(HAVE_GLES2)
-    else if (aPlugin == "GLES"
-          || aPlugin == "OPENGLES")
-    {
-      OpenGlTest::Factory (theDI);
-    }
-  #endif
-    else if (aPlugin == "XSDRAW")
-    {
-      XSDRAW::Factory (theDI);
-    }
-    else if (aPlugin == "XDEDRAW")
-    {
-      XDEDRAW::Factory (theDI);
-    }
-    else if (aPlugin == "STEP")
-    {
-      XSDRAWSTEP::Factory (theDI);
-    }
-    else if (aPlugin == "IGES")
-    {
-      XSDRAWIGES::Factory (theDI);
-    }
-    else if (aPlugin == "PLY")
-    {
-      XSDRAWPLY::Factory (theDI);
-    }
-    else if (aPlugin == "GLTF")
-    {
-      XSDRAWGLTF::Factory (theDI);
-    }
-    else if (aPlugin == "VRML")
-    {
-      XSDRAWVRML::Factory (theDI);
-    }
-    else if (aPlugin == "STL")
-    {
-      XSDRAWSTL::Factory (theDI);
-    }
-    else if (aPlugin == "OBJ")
+    auto                           anIter  = aPluginMap.find(aPlugin.ToCString());
+    if (anIter != aPluginMap.end())
     {
-      XSDRAWOBJ::Factory (theDI);
+      anIter->second(theDI);
     }
     else
     {
@@ -322,29 +268,33 @@ static Standard_Integer Pload (Draw_Interpretor& theDI,
 
 //=======================================================================
 //function : Draw_InitAppli
-//purpose  : 
+//purpose  :
 //=======================================================================
 
-void Draw_InitAppli (Draw_Interpretor& theDI)
+void Draw_InitAppli(Draw_Interpretor& theDI)
 {
 #if defined(__EMSCRIPTEN__)
   // open JavaScript console within the Browser to see this output
-  Message_Gravity aGravity = Message_Info;
-  Handle(Message_PrinterSystemLog) aJSConsolePrinter = new Message_PrinterSystemLog ("DRAWEXE", aGravity);
-  Message::DefaultMessenger()->AddPrinter (aJSConsolePrinter);
+  Message_Gravity                  aGravity          = Message_Info;
+  Handle(Message_PrinterSystemLog) aJSConsolePrinter = new Message_PrinterSystemLog("DRAWEXE",
+                                                                                    aGravity);
+  Message::DefaultMessenger()->AddPrinter(aJSConsolePrinter);
   // replace printer into std::cout by a printer into a custom callback Module.printMessage accepting message gravity
-  Message::DefaultMessenger()->RemovePrinters (STANDARD_TYPE(Message_PrinterOStream));
-  Handle(DRAWEXE_WasmModulePrinter) aJSModulePrinter = new DRAWEXE_WasmModulePrinter (aGravity);
-  Message::DefaultMessenger()->AddPrinter (aJSModulePrinter);
+  Message::DefaultMessenger()->RemovePrinters(STANDARD_TYPE(Message_PrinterOStream));
+  Handle(DRAWEXE_WasmModulePrinter) aJSModulePrinter = new DRAWEXE_WasmModulePrinter(aGravity);
+  Message::DefaultMessenger()->AddPrinter(aJSModulePrinter);
 #endif
 
-  Draw::Commands (theDI);
-  DBRep::BasicCommands (theDI);
-  DrawTrSurf::BasicCommands (theDI);
+  Draw::Commands(theDI);
+  DBRep::BasicCommands(theDI);
+  DrawTrSurf::BasicCommands(theDI);
 
 #ifdef OCCT_NO_PLUGINS
-  theDI.Add ("pload" , "pload [[Key1] [Key2] ...]: Loads Draw plugins",
-             __FILE__, Pload, "Draw Plugin");
+  theDI.Add("pload",
+            "pload [[Key1] [Key2] ...]: Loads Draw plugins",
+            __FILE__,
+            Pload,
+            "Draw Plugin");
 #endif
 }
 
index 19aaa0a63891938a881e37a6cf9d5661e1191bef..37d49504a5fe213d562ffa7251fdf0bf1def97cf 100644 (file)
@@ -9,6 +9,7 @@ TKXSDRAWOBJ
 TKXSDRAWPLY
 TKXSDRAWVRML
 TKXSDRAWSTL
+TKXSDRAWDE
 TKOpenGlTest
 TKOpenGlesTest
 TKViewerTest