Mercurial > hg > orthanc-stone
changeset 2282:e45902983245 refactoring
added StoneApplication, cleaning initialization
line wrap: on
line diff
--- a/Applications/Samples/RtViewerPlugin/Plugin.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/Samples/RtViewerPlugin/Plugin.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -109,11 +109,7 @@ { OrthancPlugins::SetGlobalContext(context); -#if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2) - Orthanc::Logging::InitializePluginContext(context); -#else - Orthanc::Logging::Initialize(context); -#endif + OrthancStone::InitializeStone(context); /* Check the version of the Orthanc core */ if (OrthancPluginCheckVersion(context) == 0)
--- a/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -460,25 +460,25 @@ **/ int main(int argc, char* argv[]) { - using namespace OrthancStone; + OrthancStone::StoneInitialize(); + OrthancStone::SdlWindow::GlobalInitialize(); - StoneInitialize(); - OrthancStone::SdlWindow::GlobalInitialize(); + int status = 0; try { - boost::shared_ptr<RtViewerApp> app = RtViewerApp::Create(); + boost::shared_ptr<OrthancStone::RtViewerApp> app = OrthancStone::RtViewerApp::Create(); g_app = app; app->RunSdl(argc,argv); } catch (Orthanc::OrthancException& e) { LOG(ERROR) << "EXCEPTION: " << e.What(); + status = 1; } OrthancStone::SdlWindow::GlobalFinalize(); - StoneFinalize(); + OrthancStone::StoneFinalize(); - return 0; + return status; } -
--- a/Applications/Samples/Sdl/SingleFrameViewer/SdlSimpleViewer.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/Samples/Sdl/SingleFrameViewer/SdlSimpleViewer.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -56,13 +56,8 @@ // TODO Refactoring -#include "../../../../OrthancStone/Sources/Platforms/Native/NativeEnvironment.h" #include "../../../../OrthancStone/Sources/Oracle/SleepOracleCommand.h" -#include "../../../../OrthancStone/Sources/Oracle/ThreadedOracle.h" - -static OrthancStone::NativeEnvironment environment_; -static OrthancStone::New::ThreadedOracle oracle_(4 /* threads */); - +#include "../../../../OrthancStone/Sources/StoneApplication.h" class Toto : public OrthancStone::IOracleClient { @@ -183,11 +178,13 @@ **/ int main(int argc, char* argv[]) { + int status = 0; + + OrthancStone::StoneInitialize(); + OrthancStone::SdlWindow::GlobalInitialize(); + try { - OrthancStone::StoneInitialize(); - OrthancStone::SdlWindow::GlobalInitialize(); - ProcessOptions(argc, argv); //Orthanc::Logging::EnableInfoLevel(true); @@ -214,9 +211,6 @@ context.StartOracle(); - environment_.Start(); - oracle_.Start(); - { { std::string font; @@ -299,7 +293,7 @@ { case SDLK_b: // TODO Refactoring - oracle_.Submit(environment_, toto_, new OrthancStone::SleepOracleCommand(1000)); + OrthancStone::StoneApplication::GetInstance().Submit(toto_, new OrthancStone::SleepOracleCommand(1000)); break; case SDLK_f: @@ -560,34 +554,32 @@ } } context.StopOracle(); - - oracle_.Stop(); - environment_.Stop(); } } - - OrthancStone::SdlWindow::GlobalFinalize(); - OrthancStone::StoneFinalize(); - return 0; } catch (Orthanc::OrthancException& e) { LOG(ERROR) << "OrthancException: " << e.What(); - return -1; + status = -1; } catch (OrthancStone::StoneException& e) { LOG(ERROR) << "StoneException: " << e.What(); - return -1; + status = -1; } catch (std::runtime_error& e) { LOG(ERROR) << "Runtime error: " << e.what(); - return -1; + status = -1; } catch (...) { LOG(ERROR) << "Native exception"; - return -1; + status = -1; } + + OrthancStone::SdlWindow::GlobalFinalize(); + OrthancStone::StoneFinalize(); + + return status; }
--- a/Applications/Samples/WebAssembly/RtViewer/RtViewerWasm.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/Samples/WebAssembly/RtViewer/RtViewerWasm.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -178,7 +178,7 @@ try { OrthancStone::StoneInitialize(); - Orthanc::Logging::Initialize(); + //Orthanc::Logging::EnableTraceLevel(true); Orthanc::Logging::EnableInfoLevel(true);
--- a/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -79,7 +79,8 @@ { try { - Orthanc::Logging::Initialize(); + OrthancStone::StoneInitialize(); + Orthanc::Logging::EnableInfoLevel(true); //Orthanc::Logging::EnableTraceLevel(true); LOG(WARNING) << "Initializing native Stone";
--- a/Applications/StoneWebViewer/Plugin/Plugin.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/StoneWebViewer/Plugin/Plugin.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -261,11 +261,7 @@ { OrthancPlugins::SetGlobalContext(context); -#if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2) - Orthanc::Logging::InitializePluginContext(context); -#else - Orthanc::Logging::Initialize(context); -#endif + OrthancStone::StoneInitialize(context); /* Check the version of the Orthanc core */ if (OrthancPluginCheckVersion(context) == 0)
--- a/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -52,6 +52,7 @@ #include "../../../OrthancStone/Sources/Scene2D/TextSceneLayer.h" #include "../../../OrthancStone/Sources/Scene2DViewport/ViewportController.h" #include "../../../OrthancStone/Sources/StoneException.h" +#include "../../../OrthancStone/Sources/StoneInitialization.h" #include "../../../OrthancStone/Sources/Toolbox/DicomInstanceParameters.h" #include "../../../OrthancStone/Sources/Toolbox/DicomStructuredReport.h" #include "../../../OrthancStone/Sources/Toolbox/GeometryToolbox.h" @@ -4874,11 +4875,8 @@ // TODO Refactoring -#include "../../../OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyEnvironment.h" #include "../../../OrthancStone/Sources/Oracle/SleepOracleCommand.h" - -static OrthancStone::WebAssemblyEnvironment environment_; -static OrthancStone::New::WebAssemblyOracle oracle_; +#include "../../../OrthancStone/Sources/StoneApplication.h" class Toto : public OrthancStone::IOracleClient { @@ -4906,7 +4904,8 @@ int main(int argc, char const *argv[]) { printf("Initializing Stone\n"); - Orthanc::InitializeFramework("", true); + OrthancStone::StoneInitialize(); + Orthanc::Logging::EnableInfoLevel(true); //Orthanc::Logging::EnableTraceLevel(true); @@ -4937,7 +4936,7 @@ // TODO Refactoring - oracle_.Submit(environment_, toto_, new OrthancStone::SleepOracleCommand(2000)); + OrthancStone::StoneApplication::GetInstance().Submit(toto_, new OrthancStone::SleepOracleCommand(2000)); }
--- a/OrthancStone/Resources/CMake/OrthancStoneConfiguration.cmake Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Resources/CMake/OrthancStoneConfiguration.cmake Tue Sep 22 17:52:22 2026 +0200 @@ -79,6 +79,11 @@ if (ENABLE_SSL) message(FATAL_ERROR "Cannot enable SSL in sandboxed environments") endif() + +else() + if (TARGET_PLATFORM_WASM) + message(FATAL_ERROR "WebAssembly is a sandboxed environments") + endif() endif() @@ -172,6 +177,14 @@ add_definitions(-DCHECK_OBSERVERS_MESSAGES) endif() +if (TARGET_PLATFORM_NATIVE) + add_definitions(-DORTHANC_STONE_TARGET_PLATFORM_NATIVE=1) +endif() + +if (TARGET_PLATFORM_WASM) + add_definitions(-DORTHANC_STONE_TARGET_PLATFORM_WASM=1) +endif() + ##################################################################### @@ -327,6 +340,8 @@ ${ORTHANC_STONE_ROOT}/Scene2DViewport/OneGesturePointerTracker.cpp ${ORTHANC_STONE_ROOT}/Scene2DViewport/UndoStack.cpp ${ORTHANC_STONE_ROOT}/Scene2DViewport/ViewportController.cpp + + ${ORTHANC_STONE_ROOT}/StoneApplication.cpp ${ORTHANC_STONE_ROOT}/StoneEnumerations.cpp ${ORTHANC_STONE_ROOT}/StoneInitialization.cpp @@ -377,7 +392,6 @@ ${ORTHANC_STONE_ROOT}/Wrappers/CairoSurface.cpp ${PLATFORM_SOURCES} - ${APPLICATIONS_SOURCES} ${ORTHANC_CORE_SOURCES} ${ORTHANC_DICOM_SOURCES}
--- a/OrthancStone/Resources/CMake/OrthancStoneParameters.cmake Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Resources/CMake/OrthancStoneParameters.cmake Tue Sep 22 17:52:22 2026 +0200 @@ -66,6 +66,9 @@ ## CMake parameters tunable by the user ##################################################################### +set(TARGET_PLATFORM_WASM OFF CACHE INTERNAL "Target WebAssembly") +set(TARGET_PLATFORM_NATIVE OFF CACHE INTERNAL "Target native environment") + set(ENABLE_OPENGL ON CACHE BOOL "Enable support of OpenGL") # Advanced parameters to fine-tune linking against system libraries
--- a/OrthancStone/Resources/CMake/OrthancStoneSdlParameters.cmake Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Resources/CMake/OrthancStoneSdlParameters.cmake Tue Sep 22 17:52:22 2026 +0200 @@ -27,6 +27,8 @@ include(${CMAKE_CURRENT_LIST_DIR}/OrthancStoneParameters.cmake) +set(TARGET_PLATFORM_NATIVE ON CACHE INTERNAL "") + ##################################################################### ## CMake parameters tunable by the user
--- a/OrthancStone/Resources/CMake/OrthancStoneWebAssemblyParameters.cmake Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Resources/CMake/OrthancStoneWebAssemblyParameters.cmake Tue Sep 22 17:52:22 2026 +0200 @@ -27,6 +27,8 @@ include(${CMAKE_CURRENT_LIST_DIR}/OrthancStoneParameters.cmake) +set(TARGET_PLATFORM_WASM ON CACHE INTERNAL "") + ##################################################################### ## CMake parameters tunable by the user
--- a/OrthancStone/Resources/Documentation/stone-object-model-reference.md Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Resources/Documentation/stone-object-model-reference.md Tue Sep 22 17:52:22 2026 +0200 @@ -413,7 +413,7 @@ Initialize: ``` -Orthanc::Logging::Initialize(); +OrthancStone::StoneInitialize(); Orthanc::Logging::EnableInfoLevel(true); ``` Call, in WASM:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancStone/Sources/StoneApplication.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -0,0 +1,201 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#include "StoneApplication.h" + +#include <Compatibility.h> +#include <Logging.h> +#include <MultiThreading/Mutex.h> + + +#if ORTHANC_STONE_TARGET_PLATFORM_WASM == 1 +# include "Platforms/WebAssembly/WebAssemblyEnvironment.h" +# include "Platforms/WebAssembly/WebAssemblyOracle.h" +#elif ORTHANC_STONE_TARGET_PLATFORM_NATIVE == 1 +# include "Platforms/Native/NativeEnvironment.h" +# include "Oracle/ThreadedOracle.h" +#else +# error Support your platform here +#endif + +namespace OrthancStone +{ +#if ORTHANC_STONE_TARGET_PLATFORM_WASM == 1 + class StoneApplication::PImpl + { + private: + WebAssemblyEnvironment environment_; + New::WebAssemblyOracle oracle_; + + public: + IEnvironment& GetEnvironment() + { + return environment_; + } + + New::IOracle& GetOracle() + { + return oracle_; + } + + void Start() + { + } + + void Stop() + { + } + }; +#endif + + +#if ORTHANC_STONE_TARGET_PLATFORM_NATIVE == 1 + class StoneApplication::PImpl + { + private: + NativeEnvironment environment_; + New::ThreadedOracle oracle_; + + public: + PImpl(unsigned int oracleThreads) : + oracle_(oracleThreads) + { + } + + IEnvironment& GetEnvironment() + { + return environment_; + } + + New::IOracle& GetOracle() + { + return oracle_; + } + + void Start() + { + environment_.Start(); + oracle_.Start(); + } + + void Stop() + { + oracle_.Stop(); + environment_.Stop(); + } + }; +#endif + + + static Orthanc::Mutex applicationMutex_; + static std::unique_ptr<StoneApplication> application_; + static unsigned int threadsCount_ = 4; + + StoneApplication::StoneApplication() + { +#if ORTHANC_STONE_TARGET_PLATFORM_WASM == 1 + pimpl_ = new PImpl; +#elif ORTHANC_STONE_TARGET_PLATFORM_NATIVE == 1 + pimpl_ = new PImpl(threadsCount_); +#else +# error Support your platform here +#endif + } + + + StoneApplication::~StoneApplication() + { + assert(pimpl_ != NULL); + delete pimpl_; + } + + + StoneApplication& StoneApplication::GetInstance() + { + Orthanc::Mutex::ScopedLock lock(applicationMutex_); + + if (application_.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + return *application_; + } + + + void StoneApplication::Initialize() + { + Orthanc::Mutex::ScopedLock lock(applicationMutex_); + + if (application_.get() == NULL) + { + application_.reset(new StoneApplication); + application_->pimpl_->Start(); + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + } + + + void StoneApplication::Finalize() + { + Orthanc::Mutex::ScopedLock lock(applicationMutex_); + + if (application_.get() != NULL) + { + application_->pimpl_->Stop(); + application_.reset(NULL); + } + } + + + IEnvironment& StoneApplication::GetEnvironment() + { + assert(pimpl_ != NULL); + return pimpl_->GetEnvironment(); + } + + + void StoneApplication::Submit(const boost::shared_ptr<IOracleClient>& client, + IOracleCommand* command /* takes ownership */) + { + assert(pimpl_ != NULL); + pimpl_->GetOracle().Submit(pimpl_->GetEnvironment(), client, command); + } + + + void StoneApplication::SetThreadsCount(unsigned int count) + { + if (count == 0) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + else + { + Orthanc::Mutex::ScopedLock lock(applicationMutex_); + threadsCount_ = count; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancStone/Sources/StoneApplication.h Tue Sep 22 17:52:22 2026 +0200 @@ -0,0 +1,57 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "Oracle/IEnvironment.h" + +#include <string> + + +namespace OrthancStone +{ + class StoneApplication : public boost::noncopyable + { + private: + class PImpl; + PImpl* pimpl_; + + StoneApplication(); + + public: + static StoneApplication& GetInstance(); + + static void Initialize(); + + static void Finalize(); + + ~StoneApplication(); + + IEnvironment& GetEnvironment(); + + void Submit(const boost::shared_ptr<IOracleClient>& client, + IOracleCommand* command /* takes ownership */); + + static void SetThreadsCount(unsigned int count); + }; +}
--- a/OrthancStone/Sources/StoneEnumerations.h Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Sources/StoneEnumerations.h Tue Sep 22 17:52:22 2026 +0200 @@ -23,7 +23,7 @@ #pragma once -#include "OrthancFramework.h" +#include <OrthancFramework.h> #include <string>
--- a/OrthancStone/Sources/StoneInitialization.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/OrthancStone/Sources/StoneInitialization.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -46,6 +46,7 @@ # include <DicomParsing/FromDcmtkBridge.h> #endif +#include "StoneApplication.h" #include "Toolbox/LinearAlgebra.h" #include <Logging.h> @@ -61,28 +62,20 @@ { if (pluginContext != NULL) { +#if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2) Orthanc::Logging::InitializePluginContext(pluginContext); +#else + Orthanc::Logging::Initialize(context); +#endif } else { Orthanc::Logging::Initialize(); } -#if ORTHANC_ENABLE_SSL == 1 - // Must be before curl - Orthanc::Toolbox::InitializeOpenSsl(); -#endif - -#if ORTHANC_ENABLE_CURL == 1 - Orthanc::HttpClient::GlobalInitialize(); -# if ORTHANC_ENABLE_SSL == 1 - Orthanc::HttpClient::ConfigureSsl(false, ""); -# endif -#endif + Orthanc::InitializeFramework("", true); #if ORTHANC_ENABLE_DCMTK == 1 - Orthanc::FromDcmtkBridge::InitializeDictionary(true); - Orthanc::FromDcmtkBridge::InitializeCodecs(); # if DCMTK_VERSION_NUMBER <= 360 OFLog::configure(OFLogger::FATAL_LOG_LEVEL); # else @@ -139,23 +132,15 @@ } } } + + StoneApplication::Initialize(); } void StoneFinalize() { -#if ORTHANC_ENABLE_DCMTK == 1 - Orthanc::FromDcmtkBridge::FinalizeCodecs(); -#endif + StoneApplication::Finalize(); -#if ORTHANC_ENABLE_CURL == 1 - Orthanc::HttpClient::GlobalFinalize(); -#endif - -#if ORTHANC_ENABLE_SSL == 1 - Orthanc::Toolbox::FinalizeOpenSsl(); -#endif - - Orthanc::Logging::Finalize(); + Orthanc::FinalizeFramework(); } }
--- a/RenderingPlugin/Sources/Plugin.cpp Sat Sep 19 01:24:20 2026 +0200 +++ b/RenderingPlugin/Sources/Plugin.cpp Tue Sep 22 17:52:22 2026 +0200 @@ -959,11 +959,7 @@ { OrthancPlugins::SetGlobalContext(context); -#if ORTHANC_FRAMEWORK_VERSION_IS_ABOVE(1, 7, 2) - Orthanc::Logging::InitializePluginContext(context); -#else - Orthanc::Logging::Initialize(context); -#endif + OrthancStone::InitializeStone(context); /* Check the version of the Orthanc core */ if (OrthancPluginCheckVersion(context) == 0)
