MeVisLab Toolbox Reference
ProcessorConnection.h
Go to the documentation of this file.
1 // Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2 // **InsertLicense** code
3 
4 #pragma once
5 
6 #include <asio/Connection.h>
7 #include <asio/Observable.h>
8 #include <asio/Executable.h>
9 #include <asio/ExecutionPolicy.h>
12 
13 #include <ThirdPartyWarningsDisable.h>
14 #include <memory>
15 #include <ThirdPartyWarningsRestore.h>
16 
17 namespace asio {
18 
19  template<typename... Args>
21 
60  template<typename... Args>
61  class ProcessorConnection<ParameterPack<Args...>> : public Connection
62  {
63  public:
64 
65  using Source = Observable<Args...>;
66  using Target = Executable<Args...>;
67 
68  ProcessorConnection() = default;
69 
70  ProcessorConnection(std::shared_ptr<Source> source, std::shared_ptr<Target> target, std::unique_ptr<ExecutionPolicy> execution_policy, bool mute_until_target_execution_finished = false)
71  : policy(std::move(execution_policy))
72  , isSynchronousExecution(dynamic_cast<SynchronousExecution*>(policy.get()) != nullptr)
73  {
74  auto handle = guard.handle();
75  if (mute_until_target_execution_finished) {
76  connection = source->signal.connect_extended([this, handle, target](const SignalConnection& conn, const Args&... args) {
77  auto lock = handle.lock();
78  if (lock) {
79  SharedSignalConnectionBlock skip_until_finished(conn);
80  if (isSynchronousExecution) {
81  target->execute(args...);
82  }
83  else {
84  policy->execute([target, skip_until_finished, args...]{
85  target->execute(args...);
86  });
87  }
88  }
89  });
90  }
91  else {
92  connection = source->signal.connect([this, handle, target](const Args&... args) {
93  auto lock = handle.lock();
94  if (lock) {
95  if (isSynchronousExecution) {
96  target->execute(args...);
97  }
98  else {
99  policy->execute([target, args...]{
100  target->execute(args...);
101  });
102  }
103  }
104  });
105  }
106  }
107 
108  void suspend() override
109  {
110  block = SharedSignalConnectionBlock(connection);
111  }
112 
113  void resume() override
114  {
115  block = SharedSignalConnectionBlock();
116  }
117 
118  private:
119 
120  std::unique_ptr<ExecutionPolicy> policy;
121  bool isSynchronousExecution;
122  ScopedSignalConnection connection;
124 
126  };
127 
128 }
Abstract base class for a connection between two I/O processors.
Definition: Connection.h:16
ProcessorConnection(std::shared_ptr< Source > source, std::shared_ptr< Target > target, std::unique_ptr< ExecutionPolicy > execution_policy, bool mute_until_target_execution_finished=false)
A guard that protects resources of a given class from being detroyed if another thread still works wi...
An synchronous execution policy.
boost::signals2::scoped_connection ScopedSignalConnection
Definition: Signal.h:25
boost::signals2::connection SignalConnection
Definition: Signal.h:23
boost::signals2::shared_connection_block SharedSignalConnectionBlock
Definition: Signal.h:27
ml_vertex_id_map get(vertex_index_t, ml_graph_ptr g)
get() function for vertex id property map vertex_index_t just necessary for overloading
boost::graph_traits< ml_graph_ptr >::vertex_descriptor source(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for u of the edge (u,v) represented by e.
boost::graph_traits< ml_graph_ptr >::vertex_descriptor target(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for v of the edge (u,v) represented by e.