fw4spl
operations.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWRUNTIME_OPERATIONS_HPP__
8 #define __FWRUNTIME_OPERATIONS_HPP__
9 
10 #include "fwRuntime/config.hpp"
11 #include "fwRuntime/ConfigurationElement.hpp"
12 #include "fwRuntime/Executable.hpp"
13 #include "fwRuntime/Extension.hpp"
14 #include "fwRuntime/ExtensionPoint.hpp"
15 #include "fwRuntime/Runtime.hpp"
16 #include "fwRuntime/RuntimeException.hpp"
17 #include "fwRuntime/Version.hpp"
18 
19 #include <fwCore/base.hpp>
20 
21 #include <boost/filesystem/path.hpp>
22 
23 #include <iterator>
24 #include <memory>
25 #include <string>
26 
27 namespace fwRuntime
28 {
29 struct Bundle;
30 
31 namespace profile
32 {
33 class Profile;
34 }
35 }
36 
37 namespace fwRuntime
38 {
39 
47 FWRUNTIME_API std::shared_ptr<ExtensionPoint> findExtensionPoint( const std::string& identifier );
48 
60 template<typename T>
62  const std::shared_ptr<Extension> extension,
63  const std::string& element = "executable",
64  const std::string& attribute = "class" )
65 {
66  // Retrieves the right configuration element.
67  std::shared_ptr<ConfigurationElement> elt( extension->findConfigurationElement(element) );
68  if( elt == 0 )
69  {
70  throw RuntimeException(element + ": configuration element not found in extension.");
71  }
72 
73  // Creates the executable instance from the found configuration element.
74  return createExecutableInstance<T>( elt, attribute );
75 }
76 
86 template<typename T>
88  const std::shared_ptr<ConfigurationElement> element,
89  const std::string& attribute = "class" )
90 {
91 
92  // Retrieves the executable type.
93  if( element->hasAttribute( attribute ) == false )
94  {
95  throw RuntimeException( "Configuration element has no attribute '" + attribute + "'." );
96  }
97  const std::string type( element->getExistingAttributeValue(attribute) );
98 
99  // Creates the executable instance.
100  Runtime* rntm( Runtime::getDefault() );
101  std::unique_ptr< IExecutable > executable( rntm->createExecutableInstance(type, element) );
102 
103  // Converts the executable instance to the right type.
104  T* result = dynamic_cast<T*>( executable.get() );
105  if( result == 0 )
106  {
107  throw RuntimeException( "Executable creation failed. Bad cast" );
108  }
109  executable.release();
110 
111  // That's all folks !
112  return result;
113 }
114 
122 template<typename OutputIterator>
124  const std::string& identifier,
125  OutputIterator output
126  )
127 {
128  std::shared_ptr< ExtensionPoint > point = findExtensionPoint(identifier);
129 
130  if( !point )
131  {
132  throw RuntimeException( identifier + ": invalid extension point identifier." );
133  }
134  point->getAllExtensions( output );
135 }
136 
145 template<typename OutputIterator>
147  const std::string& identifier,
148  OutputIterator output
149  )
150 {
151  std::shared_ptr< ExtensionPoint > point = findExtensionPoint(identifier);
152 
153  OSLM_TRACE("getAllConfigurationElementsForPoint(" << identifier << "Bundle" <<
154  point->getBundle()->getIdentifier() );
155 
156  if( !point )
157  {
158  throw RuntimeException( identifier + ": invalid extension point identifier." );
159  }
160 
161  // VAG test if ExtensionPoint is enable
162  if ( point->isEnable() )
163  {
164  point->getAllConfigurationElements( output );
165  }
166  else
167  {
168  OSLM_DEBUG( "IGNORING getAllConfigurationElementsForPoint(" << identifier << ") extension point disabled");
169  }
170 
171 }
172 
184 template<typename Container>
185 const Container getAllConfigurationElementsForPoint(const std::string& identifier)
186 {
187  // Defines an insert iterator type for the container.
188  typedef std::back_insert_iterator< Container > Inserter;
189 
190  // Collects all contributed configuratoin elements.
191  Container elements;
192  Inserter inserter(elements);
193  getAllConfigurationElementsForPoint(identifier, inserter);
194 
195  // The job is done!
196  return elements;
197 }
198 
212 template< typename Container, typename T >
213 const Container getAllExecutableForPoint( const std::string& identifier,
214  const std::string& attribute = "class" )
215 {
216  // Defines the element container
217  typedef std::vector< std::shared_ptr< ConfigurationElement > > ConfigurationElementContainer;
218 
219  // Retrieves all configuration elements.
220  ConfigurationElementContainer elements( getAllConfigurationElementsForPoint< ConfigurationElementContainer >(
221  identifier) );
222 
223  // Defines an insert iterator type for the executable container.
224  typedef std::back_insert_iterator< Container > Inserter;
225 
226  // Walks through collected configuration elements and create desired executable instances
227  Container result;
228  ConfigurationElementContainer::iterator iElement;
229  Inserter iInserter( result );
230  for( iElement = elements.begin(); iElement != elements.end(); ++iElement, ++iInserter )
231  {
232  std::shared_ptr< ConfigurationElement > element( *iElement );
233  std::shared_ptr< T > executable( createExecutableInstance< T >(element, attribute) );
234 
235  iInserter = executable;
236  }
237  return result;
238 }
239 
249 FWRUNTIME_API std::shared_ptr< ConfigurationElement > findConfigurationElement( const std::string& identifier,
250  const std::string& pointIdentifier );
251 
259 FWRUNTIME_API std::shared_ptr<Extension> findExtension( const std::string& identifier );
260 
268 FWRUNTIME_API ::boost::filesystem::path getBundleResourcePath(const std::string& bundleIdentifier) noexcept;
269 
278 FWRUNTIME_API ::boost::filesystem::path getBundleResourceFilePath(const std::string& bundleIdentifier,
279  const ::boost::filesystem::path& path) noexcept;
280 
290 FWRUNTIME_API ::boost::filesystem::path getBundleResourceFilePath(const ::boost::filesystem::path& path) noexcept;
291 
301 FWRUNTIME_API ::boost::filesystem::path getLibraryResourceFilePath(const ::boost::filesystem::path& path) noexcept;
302 
321 FWRUNTIME_API ::boost::filesystem::path getResourceFilePath(const ::boost::filesystem::path& path) noexcept;
322 
331 FWRUNTIME_API ::boost::filesystem::path getBundleResourcePath( std::shared_ptr<Bundle> bundle,
332  const ::boost::filesystem::path& path) noexcept;
333 
342 FWRUNTIME_API ::boost::filesystem::path getBundleResourcePath( std::shared_ptr<ConfigurationElement> element,
343  const ::boost::filesystem::path& path) noexcept;
344 
353 FWRUNTIME_API ::boost::filesystem::path getBundleResourcePath(const IExecutable* executable,
354  const ::boost::filesystem::path& path) noexcept;
355 
361 FWRUNTIME_API void addBundles( const boost::filesystem::path& directory );
362 
370 FWRUNTIME_API std::shared_ptr< ::fwRuntime::profile::Profile > startProfile( const boost::filesystem::path& path );
371 
380 FWRUNTIME_API std::shared_ptr<Bundle> findBundle( const std::string& identifier, const Version& version = Version() );
381 
387 FWRUNTIME_API void startBundle(const std::string& identifier);
388 
389 } // namespace fwRuntime
390 
391 #endif // __FWRUNTIME_OPERATIONS_HPP__
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Definition: Runtime.cpp:286
Defines the runtime class.
Definition: Runtime.hpp:37
FWRUNTIME_API::boost::filesystem::path getLibraryResourceFilePath(const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a resource path whose first element is a library identifier...
Definition: operations.cpp:161
FWRUNTIME_API::boost::filesystem::path getResourceFilePath(const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a resource path whose first element is a library or a bundle ide...
Definition: operations.cpp:171
FWRUNTIME_API std::shared_ptr< ConfigurationElement > findConfigurationElement(const std::string &identifier, const std::string &pointIdentifier)
Retrieve the configuation element with the given identifier for the given extension point...
Definition: operations.cpp:56
FWRUNTIME_API Iterator begin()
Retrieves the iterator on the first managed configuration element.
Defines the runtime exception class.
const Container getAllExecutableForPoint(const std::string &identifier, const std::string &attribute="class")
Retrieves all executable objects for the point having the specified identifier.
Definition: operations.hpp:213
Defines the base executable interface.An executable object is an instance created by an extension poi...
Definition: IExecutable.hpp:41
FWRUNTIME_API std::shared_ptr< Extension > findExtension(const std::string &identifier)
Retrieve the extension having the specified identifier.
Definition: operations.cpp:75
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
FWRUNTIME_API std::vector< std::shared_ptr< ::fwRuntime::Extension > > getAllExtensionsForPoint(std::string extension_pt)
Returns extensions extending the _extension_pt extension point.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
FWRUNTIME_API std::shared_ptr< ExtensionPoint > findExtensionPoint(const std::string &identifier)
Retrieves the extension point having the specified identifier.
Definition: operations.cpp:83
FWRUNTIME_API void addBundles(const boost::filesystem::path &directory)
Loads all bundles that can be found in the specified directory.
FWRUNTIME_API void startBundle(const std::string &identifier)
Starts the bundle specified by the given identifier.
Definition: operations.cpp:240
FWRUNTIME_API std::shared_ptr< ::fwRuntime::profile::Profile > startProfile(const boost::filesystem::path &path)
Starts the given bundle set profile.
FWRUNTIME_API std::shared_ptr< Bundle > findBundle(const std::string &identifier, const Version &version=Version())
Retrieves the bundle with the given identifier and version.
Definition: operations.cpp:233
T * createExecutableInstance(const std::shared_ptr< Extension > extension, const std::string &element="executable", const std::string &attribute="class")
Creates an executable instance for the specified configuration element.
Definition: operations.hpp:61
FWRUNTIME_API::boost::filesystem::path getBundleResourceFilePath(const std::string &bundleIdentifier, const ::boost::filesystem::path &path) noexcept
Retrieve a filesystem valid path for a path relative to the bundle having the specified identifier...
Definition: operations.cpp:106
FWRUNTIME_API::boost::filesystem::path getBundleResourcePath(const std::string &bundleIdentifier) noexcept
Retrieve the filesystem valid path of resources of a bundle.
Definition: operations.cpp:91
Defines the generic configuration element container class.
void getAllConfigurationElementsForPoint(const std::string &identifier, OutputIterator output)
Retrieves all configuration elements for the point having the specified identifier.
Definition: operations.hpp:146
Holds version information for libraries and bundles.
FWRUNTIME_API Iterator end()
Retrieves the iterator on the end of the configuration element container.
#define OSLM_DEBUG(message)
Definition: spyLog.hpp:241