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 
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
SurgSim::Framework::Runtime::m_isPaused
bool m_isPaused
Definition: Runtime.h:214
SurgSim::Framework::Runtime::resume
void resume()
Resume from pause, causes all managers to resume normal processing.
Definition: Runtime.cpp:240
SurgSim::Framework::Runtime::saveScene
void saveScene(const std::string &fileName) const
Write out the whole scene as a file.
Definition: Runtime.cpp:480
SurgSim::Framework::Runtime::getManager
std::shared_ptr< T > getManager() const
Definition: Runtime.h:221
SurgSim::Framework::Runtime::loadScene
void loadScene(const std::string &fileName)
Loads the scene from the given file, clears all the elements in the scene, the old scene will be over...
Definition: Runtime.cpp:358
SurgSim::Framework::Runtime::addComponents
void addComponents(const std::vector< std::shared_ptr< SurgSim::Framework::Component >> &components)
Adds the components.
Definition: Runtime.cpp:104
SurgSim::Framework::Runtime::m_isStopped
bool m_isStopped
Definition: Runtime.h:216
SurgSim::Framework::Runtime::getApplicationData
static std::shared_ptr< const ApplicationData > getApplicationData()
Gets application data for the runtime.
Definition: Runtime.cpp:321
SurgSim::Framework::Runtime::Runtime
Runtime()
Default constructor.
Definition: Runtime.cpp:38
SurgSim::Framework::Runtime::execute
bool execute()
Start all the threads and block until one of them quits.
Definition: Runtime.cpp:125
SurgSim::Framework::Runtime::getThreadPool
static std::shared_ptr< ThreadPool > getThreadPool()
Gets the thread pool for the runtime.
Definition: Runtime.cpp:328
YAML
Definition: DataStructuresConvert.h:28
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::Runtime::initSearchPaths
void initSearchPaths(const std::string &configFilePath)
Initializes the search paths.
Definition: Runtime.cpp:307
SurgSim::Framework::Runtime::getManagers
std::vector< std::weak_ptr< ComponentManager > > getManagers() const
Definition: Runtime.cpp:72
SurgSim::Framework::Runtime::m_applicationData
static std::shared_ptr< ApplicationData > m_applicationData
Definition: Runtime.h:209
SurgSim::Framework::Runtime::isPaused
bool isPaused() const
Query if this object is paused.
Definition: Runtime.cpp:269
SurgSim::Framework::Runtime::step
void step()
Make all managers execute 1 update loop, afterwards they will wait for another step() call or resume(...
Definition: Runtime.cpp:256
SurgSim::Framework::Runtime::tryConvertElements
bool tryConvertElements(const std::string &fileName, const YAML::Node &node, std::vector< std::shared_ptr< SceneElement >> *elements)
Convert nodes to vector of elements.
Definition: Runtime.cpp:449
SurgSim::Framework::Runtime::addManager
void addManager(std::shared_ptr< ComponentManager > thread)
Add a worker thread, this should probably only be possible if the system is not running.
Definition: Runtime.cpp:60
SurgSim::Framework::Runtime::~Runtime
~Runtime()
Destructor.
Definition: Runtime.cpp:54
SurgSim::Framework::Runtime::start
bool start(bool paused=false)
Start all the threads non returns after the startup as succeeded.
Definition: Runtime.cpp:149
SurgSim::Framework::Runtime::m_barrier
std::shared_ptr< Barrier > m_barrier
Definition: Runtime.h:213
SurgSim::Framework::Runtime::isRunning
bool isRunning() const
Query if this object is running.
Definition: Runtime.cpp:264
SurgSim::Framework::Runtime::duplicateSceneElements
std::vector< std::shared_ptr< SceneElement > > duplicateSceneElements(const std::string &fileName)
Loads and duplicates the scene elements from the file, the elements will not have common ids with any...
Definition: Runtime.cpp:396
SurgSim::Framework::Runtime::addComponent
void addComponent(const std::shared_ptr< Component > &component)
Adds a component.
Definition: Runtime.cpp:334
SurgSim::Framework::Runtime::getSharedPtr
std::shared_ptr< Runtime > getSharedPtr()
Gets a shared pointer to the runtime.
Definition: Runtime.cpp:293
SurgSim::Framework::Runtime::removeComponent
void removeComponent(const std::shared_ptr< Component > &component)
Removes the component described by component.
Definition: Runtime.cpp:345
SurgSim::Framework::Runtime::preprocessSceneElements
void preprocessSceneElements()
Preprocess scene elements.
Definition: Runtime.cpp:274
SurgSim::Framework::Runtime::m_isRunning
bool m_isRunning
Definition: Runtime.h:206
SurgSim::Framework::Runtime
This class contains all the information about the runtime environment of the simulation,...
Definition: Runtime.h:49
SurgSim::Framework::Runtime::addSceneElements
void addSceneElements(const std::string &fileName)
Adds the scene elements from the file to the current scene The file format should be just a list of s...
Definition: Runtime.cpp:375
SurgSim::Framework::Runtime::m_scene
std::shared_ptr< Scene > m_scene
Definition: Runtime.h:208
SurgSim::Framework::Runtime::m_managers
std::vector< std::shared_ptr< ComponentManager > > m_managers
Definition: Runtime.h:207
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
SurgSim::Framework::tryLoadNode
bool tryLoadNode(const std::string &fileName, YAML::Node *node)
Perform a YAML load operation.
Definition: Runtime.cpp:424
SurgSim::Framework::Runtime::addSceneElement
bool addSceneElement(std::shared_ptr< SceneElement > sceneElement)
Adds a scene element.
Definition: Runtime.cpp:92
SurgSim::Framework::Runtime::m_sceneHandling
boost::mutex m_sceneHandling
Definition: Runtime.h:211
SurgSim::Framework::Runtime::stop
bool stop()
Stops the simulation.
Definition: Runtime.cpp:191
SurgSim::Framework::Runtime::pause
void pause()
Pause all managers, this will set all managers to synchronous execution, they will all complete their...
Definition: Runtime.cpp:231
SurgSim::Framework::Runtime::getScene
std::shared_ptr< Scene > getScene()
Definition: Runtime.cpp:82