opensurgsim
Runtime.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2015, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_RUNTIME_H
17 #define SURGSIM_FRAMEWORK_RUNTIME_H
18 
19 #include <boost/thread/mutex.hpp>
20 #include <boost/thread/thread.hpp>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 #include <atomic>
26 
27 #include "SurgSim/Framework/Messenger.h"
28 
29 
30 namespace YAML
31 {
32 class Node;
33 }
34 
35 namespace SurgSim
36 {
37 namespace Framework
38 {
39 
40 class ApplicationData;
41 class Barrier;
42 class ComponentManager;
43 class Component;
44 class Logger;
45 class Scene;
46 class SceneElement;
47 class ThreadPool;
48 
53 class Runtime : public std::enable_shared_from_this<Runtime>
54 {
55 public:
56 
58  Runtime();
59 
64  explicit Runtime(const std::string& configFilePath);
65 
67  ~Runtime();
68 
70  void addManager(std::shared_ptr<ComponentManager> thread);
71 
73  std::vector<std::weak_ptr<ComponentManager>> getManagers() const;
74 
76  template <class T>
77  std::shared_ptr<T> getManager() const;
78 
79  Messenger& getMessenger();
80 
81 
83  std::shared_ptr<Scene> getScene();
84 
86  std::shared_ptr<Scene> getScene() const;
87 
91  bool addSceneElement(std::shared_ptr<SceneElement> sceneElement);
92 
94  bool execute();
95 
98  bool start(bool paused = false);
99 
105  void pause();
106 
110  void resume();
111 
113  void step();
114 
120  bool stop();
121 
124  bool isRunning() const;
125 
128  bool isPaused() const;
129 
132  static std::shared_ptr<const ApplicationData> getApplicationData();
133 
136  static std::shared_ptr<ThreadPool> getThreadPool();
137 
138 
139 
142  void addComponent(const std::shared_ptr<Component>& component);
143 
146  void removeComponent(const std::shared_ptr<Component>& component);
147 
152  void loadScene(const std::string& fileName);
153 
174  void addSceneElements(const std::string& fileName);
175 
182  std::vector<std::shared_ptr<SceneElement>> duplicateSceneElements(const std::string& fileName);
183 
186  void saveScene(const std::string& fileName) const;
187 
188 private:
189 
192  void preprocessSceneElements();
193 
197  void addComponents(const std::vector<std::shared_ptr<SurgSim::Framework::Component>>& components);
198 
202  void initSearchPaths(const std::string& configFilePath);
203 
209  bool tryConvertElements(const std::string& fileName, const YAML::Node& node,
210  std::vector<std::shared_ptr<SceneElement>>* elements);
211 
214  std::shared_ptr<Runtime> getSharedPtr();
215  std::atomic<bool> m_isRunning;
216  std::vector<std::shared_ptr<ComponentManager>> m_managers;
217  std::shared_ptr<Scene> m_scene;
218  static std::shared_ptr<ApplicationData> m_applicationData;
219 
220  Messenger m_messenger;
221  boost::thread m_messengerThread;
222 
223  boost::mutex m_sceneHandling;
224 
225  std::shared_ptr<Barrier> m_barrier;
226 
227  bool m_isPaused;
228 
229  bool m_isStopped;
230 };
231 
232 template <class T>
233 std::shared_ptr<T>
235 {
236  std::shared_ptr<T> result;
237  for (auto& manager : m_managers)
238  {
239  result = std::dynamic_pointer_cast<T>(manager);
240  if (result != nullptr)
241  {
242  return result;
243  }
244  }
245  return result;
246 }
247 
252 bool tryLoadNode(const std::string& fileName, YAML::Node* node);
253 
254 
255 }; // namespace Framework
256 }; // namespace SurgSim
257 
258 #endif // SURGSIM_FRAMEWORK_RUNTIME_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
std::shared_ptr< T > getManager() const
Definition: Runtime.h:234
Definition: DataStructuresConvert.h:28
Messenger implements asynchronous communication to OSS, components can add themselves as subscribers ...
Definition: Messenger.h:40
This class contains all the information about the runtime environment of the simulation, all the running threads, the state, while it is de facto a singleton it should be passed around if needed.
Definition: Runtime.h:53