Runtime.h
Go to the documentation of this file.
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 <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 
26 namespace YAML
27 {
28 class Node;
29 }
30 
31 namespace SurgSim
32 {
33 namespace Framework
34 {
35 
36 class ApplicationData;
37 class Barrier;
38 class ComponentManager;
39 class Component;
40 class Logger;
41 class Scene;
42 class SceneElement;
43 class ThreadPool;
44 
49 class Runtime : public std::enable_shared_from_this<Runtime>
50 {
51 public:
52 
54  Runtime();
55 
60  explicit Runtime(const std::string& configFilePath);
61 
63  ~Runtime();
64 
66  void addManager(std::shared_ptr<ComponentManager> thread);
67 
69  std::vector<std::weak_ptr<ComponentManager>> getManagers() const;
70 
72  template <class T>
73  std::shared_ptr<T> getManager() const;
74 
76  std::shared_ptr<Scene> getScene();
77 
79  std::shared_ptr<Scene> getScene() const;
80 
84  bool addSceneElement(std::shared_ptr<SceneElement> sceneElement);
85 
87  bool execute();
88 
91  bool start(bool paused = false);
92 
98  void pause();
99 
103  void resume();
104 
106  void step();
107 
113  bool stop();
114 
117  bool isRunning() const;
118 
121  bool isPaused() const;
122 
125  static std::shared_ptr<const ApplicationData> getApplicationData();
126 
129  static std::shared_ptr<ThreadPool> getThreadPool();
130 
133  void addComponent(const std::shared_ptr<Component>& component);
134 
137  void removeComponent(const std::shared_ptr<Component>& component);
138 
143  void loadScene(const std::string& fileName);
144 
165  void addSceneElements(const std::string& fileName);
166 
173  std::vector<std::shared_ptr<SceneElement>> duplicateSceneElements(const std::string& fileName);
174 
177  void saveScene(const std::string& fileName) const;
178 
179 private:
180 
183  void preprocessSceneElements();
184 
188  void addComponents(const std::vector<std::shared_ptr<SurgSim::Framework::Component>>& components);
189 
193  void initSearchPaths(const std::string& configFilePath);
194 
200  bool tryConvertElements(const std::string& fileName, const YAML::Node& node,
201  std::vector<std::shared_ptr<SceneElement>>* elements);
202 
205  std::shared_ptr<Runtime> getSharedPtr();
207  std::vector<std::shared_ptr<ComponentManager>> m_managers;
208  std::shared_ptr<Scene> m_scene;
209  static std::shared_ptr<ApplicationData> m_applicationData;
210 
211  boost::mutex m_sceneHandling;
212 
213  std::shared_ptr<Barrier> m_barrier;
215 
217 };
218 
219 template <class T>
220 std::shared_ptr<T>
222 {
223  std::shared_ptr<T> result;
224  for (auto& manager : m_managers)
225  {
226  result = std::dynamic_pointer_cast<T>(manager);
227  if (result != nullptr)
228  {
229  return result;
230  }
231  }
232  return result;
233 }
234 
239 bool tryLoadNode(const std::string& fileName, YAML::Node* node);
240 
241 
242 }; // namespace Framework
243 }; // namespace SurgSim
244 
245 #endif // SURGSIM_FRAMEWORK_RUNTIME_H
Definition: CompoundShapeToGraphics.cpp:29
std::vector< std::shared_ptr< ComponentManager > > m_managers
Definition: Runtime.h:207
bool tryLoadNode(const std::string &fileName, YAML::Node *node)
Perform a YAML load operation.
Definition: Runtime.cpp:424
bool m_isStopped
Definition: Runtime.h:216
bool m_isRunning
Definition: Runtime.h:206
std::shared_ptr< T > getManager() const
Definition: Runtime.h:221
boost::mutex m_sceneHandling
Definition: Runtime.h:211
bool m_isPaused
Definition: Runtime.h:214
Definition: DataStructuresConvert.h:28
std::shared_ptr< Barrier > m_barrier
Definition: Runtime.h:213
std::shared_ptr< Scene > m_scene
Definition: Runtime.h:208
static std::shared_ptr< ApplicationData > m_applicationData
Definition: Runtime.h:209
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:49