fw4spl
Profile.cpp
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 #include "fwRuntime/profile/Profile.hpp"
8 
9 #include "fwRuntime/Bundle.hpp"
10 #include "fwRuntime/Extension.hpp"
11 #include "fwRuntime/profile/Activater.hpp"
12 #include "fwRuntime/profile/Initializer.hpp"
13 #include "fwRuntime/profile/Starter.hpp"
14 #include "fwRuntime/profile/Stopper.hpp"
15 #include "fwRuntime/profile/Uninitializer.hpp"
16 #include "fwRuntime/Runtime.hpp"
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <functional>
21 
22 namespace fwRuntime
23 {
24 
25 namespace profile
26 {
27 
28 namespace
29 {
30 template< typename E >
31 struct Apply
32 {
33  //------------------------------------------------------------------------------
34 
35  void operator() ( E e )
36  {
37  e->apply();
38  }
39 };
40 }
41 
42 //------------------------------------------------------------------------------
43 
44 Profile::wptr current_profile;
45 
46 //------------------------------------------------------------------------------
47 
48 void setCurrentProfile(Profile::sptr prof)
49 {
50  current_profile = prof;
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 Profile::sptr getCurrentProfile()
56 {
57  return current_profile.lock();
58 }
59 
60 //------------------------------------------------------------------------------
61 
63  m_checkSingleInstance(false),
64 #ifdef ANDROID
65  m_app(nullptr),
66 #endif
67  m_argc(0),
68  m_argv(NULL)
69 {
70  m_run = std::bind(&Profile::defaultRun, this);
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 Profile::~Profile()
76 {
77  if (m_argv)
78  {
79  delete[] m_argv;
80  }
81 }
82 
83 //------------------------------------------------------------------------------
84 
85 void Profile::add( SPTR( Activater )activater )
86 {
87  m_activaters.push_back( activater );
88 }
89 
90 //------------------------------------------------------------------------------
91 
92 void Profile::add( SPTR( Starter )starter )
93 {
94  m_starters.push_back( starter );
95 }
96 
97 //------------------------------------------------------------------------------
98 
99 void Profile::add( SPTR( Stopper )stopper )
100 {
101  m_stoppers.push_back( stopper );
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 void Profile::add( SPTR( Initializer )initializer )
107 {
108  m_initializers.push_back(initializer);
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 void Profile::add( SPTR( Uninitializer )uninitializer )
114 {
115  m_uninitializers.push_back(uninitializer);
116 }
117 
118 //------------------------------------------------------------------------------
119 
121 {
122  std::for_each( m_activaters.begin(), m_activaters.end(), Apply< ActivaterContainer::value_type >() );
123 
124  // Check validity of extension
125  Runtime* rntm( Runtime::getDefault() );
126  for( Runtime::ExtensionIterator i = rntm->extensionsBegin(); i != rntm->extensionsEnd(); ++i )
127  {
128  SPTR( Extension ) extension( *i );
130  "Validation not ok for bundle = '" << extension->getBundle()->getIdentifier() << "' (extension id = '" << extension->getIdentifier() << "' )",
131  extension->getBundle()->isEnable() && extension->validate() == Extension::Invalid );
132  }
133 
134  std::for_each( m_starters.begin(), m_starters.end(), Apply< StarterContainer::value_type >() );
135  OSLM_TRACE( "NB INITIALIZERS: " << m_initializers.size() );
136 }
137 
138 //------------------------------------------------------------------------------
139 
141 {
142  SLM_ASSERT("the 'run' callback is missing", m_run);
143  int result;
144  result = m_run();
145  return result;
146 }
147 
148 //------------------------------------------------------------------------------
149 
150 int Profile::defaultRun()
151 {
152  SLM_TRACE_FUNC();
153  this->setup();
154  this->cleanup();
155  return 0;
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 void Profile::setRunCallback(RunCallbackType callback)
161 {
162  m_run = callback;
163 }
164 
165 //------------------------------------------------------------------------------
166 
167 void Profile::stop()
168 {
169  std::for_each( m_stoppers.rbegin(), m_stoppers.rend(), Apply< StopperContainer::value_type >() );
170 }
171 
172 //------------------------------------------------------------------------------
173 
175 {
176  InitializerContainer initializers;
177  initializers = m_initializers;
178  m_initializers.clear();
179  std::for_each( initializers.begin(), initializers.end(), Apply< InitializerContainer::value_type >() );
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 void Profile::cleanup()
185 {
186  std::for_each( m_uninitializers.rbegin(), m_uninitializers.rend(), Apply< UninitializerContainer::value_type >() );
187  m_uninitializers.clear();
188 }
189 
190 //------------------------------------------------------------------------------
191 
192 void Profile::setParams(int argc, char** argv)
193 {
194  ParamsContainer params;
195  for(int i = 0; i < argc; ++i)
196  {
197  params.push_back( std::string(argv[i]) );
198  }
199  this->setParams(params);
200 }
201 
202 //------------------------------------------------------------------------------
203 
204 void Profile::setParams(const Profile::ParamsContainer& params)
205 {
206  m_params = params;
207 
208  if (m_argv)
209  {
210  delete[] m_argv;
211  }
212 
213  m_argc = static_cast<int>(m_params.size());
214  // allocate memory for an array of character strings
215  m_argv = new char*[m_params.size()];
216 
217  // for each string, allocate memory in the character array and copy
218  for(size_t i = 0; i < m_params.size(); i++)
219  {
220  size_t paramSize = m_params[i].size();
221  m_argv[i] = new char[paramSize+1];
222 #ifndef _WIN32
223  strncpy(m_argv[i], m_params[i].c_str(), paramSize);
224  m_argv[i][paramSize] = '\0';
225 #else
226  strncpy_s(m_argv[i], paramSize+1, m_params[i].c_str(), paramSize);
227 #endif
228  }
229 }
230 
231 //------------------------------------------------------------------------------
232 
233 } // namespace profile
234 
235 } // namespace fwRuntime
static FWRUNTIME_API Runtime * getDefault()
Retrieves the default runtime instance.
Definition: Runtime.cpp:286
Defines the runtime class.
Definition: Runtime.hpp:37
#define SPTR(_cls_)
ExtensionContainer::iterator ExtensionIterator
Defines the extension container type.
Definition: Runtime.hpp:51
FWRUNTIME_API void add(std::shared_ptr< Activater > activater)
Adds a new activator.
Definition: Profile.cpp:85
FWRUNTIME_API void setCurrentProfile(Profile::sptr prof)
Set current profile.
Definition: Profile.cpp:48
Activates a given bundle with optional parameters.
Definition: Activater.hpp:34
Defines the extension class.
Definition: Extension.hpp:30
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWRUNTIME_API ExtensionIterator extensionsEnd()
Retrieves the iterator on the end of the extension collection.
Definition: Runtime.cpp:221
FWRUNTIME_API Profile::sptr getCurrentProfile()
Get current profile.
Definition: Profile.cpp:55
FWRUNTIME_API ExtensionIterator extensionsBegin()
Retrieves the iterator on the beginning of the extension collection.
Definition: Runtime.cpp:214
Starts a given bundle.
Definition: Starter.hpp:26
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
FWRUNTIME_API void setup()
Once started, setup the profile.
Definition: Profile.cpp:174
The extension failed the validation.
Definition: Extension.hpp:42
FWRUNTIME_API int run()
Run the profile.
Definition: Profile.cpp:140
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWRUNTIME_API void start()
Starts the profile.
Definition: Profile.cpp:120
FWRUNTIME_API Profile()
Constructor : does nothing.
Definition: Profile.cpp:62
#define OSLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:289
Starts a given bundle.
Definition: Initializer.hpp:26
Stops a given bundle.
Definition: Stopper.hpp:26