Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkStorageThreadDispatcherBase.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkStorageThreadDispatcherBase_h
14 #define mitkStorageThreadDispatcherBase_h
15 
16 #include <MitkCoreExports.h>
17 #include <mitkCommon.h>
18 #include <itkObject.h>
19 #include <functional>
20 
21 namespace mitk
22 {
32  class MITKCORE_EXPORT StorageThreadDispatcherBase : public itk::Object
33  {
34  public:
36 
46  void Execute(std::function<void()> task)
47  {
48  if (this->IsDispatchThread())
49  {
50  task();
51  }
52  else
53  {
54  this->ExecuteDispatched(task);
55  }
56  }
57 
67  virtual bool IsDispatchThread() const = 0;
68 
83  virtual void Post(std::function<void()> task) = 0;
84 
92  template <typename R>
93  R ExecuteWithResult(std::function<R()> task)
94  {
95  R result{};
96  this->Execute([&result, &task]() { result = task(); });
97  return result;
98  }
99 
100  protected:
101 
110  virtual void ExecuteDispatched(std::function<void()> task) = 0;
111 
113  ~StorageThreadDispatcherBase() override = default;
114  };
115 }
116 
117 #endif
#define MITKCORE_EXPORT
Abstract base class for dispatching tasks to the thread that owns DataStorage.
virtual void ExecuteDispatched(std::function< void()> task)=0
Execute a task on the storage-owning thread, blocking until completion.
void Execute(std::function< void()> task)
Execute a task on the storage-owning thread, blocking until completion.
R ExecuteWithResult(std::function< R()> task)
Convenience template for tasks that return a value.
~StorageThreadDispatcherBase() override=default
virtual bool IsDispatchThread() const =0
Check if the current thread is the dispatch target thread.
virtual void Post(std::function< void()> task)=0
Post a task to run on the storage-owning thread, returning immediately.
mitkClassMacroItkParent(StorageThreadDispatcherBase, itk::Object)
Find image slices visible on a given plane.