All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RuntimeEnv.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 /**
35  @file RuntimeEnv.hpp
36 
37  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
38 */
39 
40 #ifndef __OPENFLUID_BASE_RUNTIMEENV_HPP__
41 #define __OPENFLUID_BASE_RUNTIMEENV_HPP__
42 
43 #include <string>
44 #include <vector>
45 
46 #include <openfluid/dllexport.hpp>
49 #include <openfluid/ware/TypeDefs.hpp>
50 
51 
52 
53 namespace openfluid { namespace base {
54 
55 
56 // =====================================================================
57 // =====================================================================
58 
59 
60 /**
61 
62 */
64 {
65  private:
66 
67  static RuntimeEnvironment* mp_Singleton;
68 
69  const std::string m_Version;
70  const std::string m_FullVersion;
71  const std::string m_MajorMinorVersion;
72 
73  std::string m_OutputDir;
74  std::string m_InputDir;
75  std::string m_UserDataDir;
76  std::string m_TempDir;
77  std::string m_HomeDir;
78  std::string m_MarketBagDir;
79  std::string m_MarketBagVersionDir;
80 
81  std::string m_MarketBagSimVersionDir;
82  std::string m_MarketBagObsVersionDir;
83  std::string m_MarketBagBuildVersionDir;
84  std::string m_MarketBagDataVersionDir;
85  std::string m_MarketBagBinSubDir;
86  std::string m_MarketBagSrcSubDir;
87 
88  std::string m_InstallPrefix;
89 
90  std::vector<std::string> m_DefaultSimulatorsPlugsDirs;
91  std::vector<std::string> m_ExtraSimulatorsPlugsDirs;
92 
93  std::vector<std::string> m_DefaultObserversPlugsDirs;
94  std::vector<std::string> m_ExtraObserversPlugsDirs;
95 
96 
97  std::string m_ProvidedExamplesDir;
98  std::string m_UserExamplesDir;
99 
100  std::string m_UserID;
101  std::string m_HostName;
102  const std::string m_Arch;
103 
104  unsigned int m_SimulatorsMaxNumThreads;
105 
106  std::string m_DefaultConfigFilePath;
107 
108  bool m_ClearOutputDir;
109 
110  bool m_WriteResults;
111 
112  bool m_WriteSimReport;
113 
114  bool m_Profiling;
115 
116  unsigned int m_ValuesBufferSize;
117 
118  bool m_IsUserValuesBufferSize;
119 
121 
122  openfluid::base::EnvironmentProperties m_ExtraProperties;
123 
124  bool m_IsLinkedToProject;
125 
126  /**
127  Default constructor
128  */
130 
131 
132  public:
133 
134  static RuntimeEnvironment* instance();
135 
136  /**
137  Destructor
138  */
140 
141  /**
142  Returns the version of OpenFLUID
143  @return the version as a std::string
144  */
145  std::string getVersion() const
146  { return m_Version; };
147 
148  /**
149  Returns the full version of OpenFLUID, including status (alpha, beta, rc, ...)
150  @return the full version as a std::string
151  */
152  std::string getFullVersion() const
153  { return m_FullVersion; };
154 
155  /**
156  Returns the version of OpenFLUID, composed of major and minor numbers
157  @return the major.minor version as a std::string
158  */
159  std::string getMajorMinorVersion() const
160  { return m_MajorMinorVersion; };
161 
162 
163 
164  /**
165  Sets the input directory, overriding the default input dir
166  @param[in] InputDir The input directory
167  */
168  void setInputDir(const std::string& InputDir)
169  { m_InputDir = InputDir; mp_WareEnv->setValue("dir.input",m_InputDir); };
170 
171  /**
172  Returns the input directory
173  @return the input directory
174  */
175  inline std::string getInputDir() const
176  { return m_InputDir; };
177 
178  /**
179  Sets the output directory, overriding the default output dir
180  @param[in] OutputDir The output directory
181  */
182  void setOutputDir(const std::string& OutputDir)
183  { m_OutputDir = OutputDir; mp_WareEnv->setValue("dir.output",m_OutputDir); };
184 
185  /**
186  Returns the output directory
187  @return the output directory
188  */
189  inline std::string getOutputDir() const
190  { return m_OutputDir; };
191 
192  /**
193  Sets the output directory as a directory located in the user data directory,
194  with a name based on the current date-time
195  */
196  void setDateTimeOutputDir();
197 
198  /**
199  Returns the market bag directory (i.e. $HOME/.openfluid/market-bag)
200  @return the market bag directory
201  */
202  inline std::string getMarketBagDir() const
203  { return m_MarketBagDir; };
204 
205  /**
206  @return the market bag directory for the current OpenFLUID version
207  (i.e. $HOME/.openfluid/market-bag/2.0.0)
208  */
209  inline std::string getMarketBagVersionDir() const
210  { return m_MarketBagVersionDir; };
211 
212  /**
213  @return the market bag directory for simulators of the current version
214  (i.e. $HOME/.openfluid/market-bag/2.0.0/simulators)
215  */
216  inline std::string getMarketBagSimVersionDir() const
217  { return m_MarketBagSimVersionDir; };
218 
219  /**
220  @return the market bag directory for observers of the current version
221  (i.e. $HOME/.openfluid/market-bag/2.0.0/observers)
222  */
223  inline std::string getMarketBagObsVersionDir() const
224  { return m_MarketBagObsVersionDir; };
225 
226  /**
227  @return the market bag directory for builderexts of the current version
228  (i.e. $HOME/.openfluid/market-bag/2.0.0/builderexts)
229  */
230  inline std::string getMarketBagBuildVersionDir() const
231  { return m_MarketBagBuildVersionDir; };
232 
233  /**
234  @return the market bag directory for datasets of the current version
235  (i.e. $HOME/.openfluid/market-bag/2.0.0/datasets)
236  */
237  inline std::string getMarketBagDataVersionDir() const
238  { return m_MarketBagDataVersionDir; };
239 
240  /**
241  @return the binairies market bag subdirectory
242  */
243  inline std::string getMarketBagBinSubDir() const
244  { return m_MarketBagBinSubDir; };
245 
246  /**
247  @return the sources market bag subdirectory
248  */
249  inline std::string getMarketBagSrcSubDir() const
250  { return m_MarketBagSrcSubDir; };
251 
252  /**
253  Returns the default config file path (i.e. $HOME/.openfluid/openfluid.conf)
254  @return the default config file path
255  */
256  inline std::string getDefaultConfigFile() const
257  { return m_DefaultConfigFilePath; };
258 
259  /**
260  Returns the path for a given config file (i.e. $HOME/.openfluid/Filename)
261  @param[in] Filename the given config file name
262  @return the path
263  */
264  std::string getConfigFilePath(std::string Filename) const
265  { return m_UserDataDir + "/" + Filename; };
266 
267 
268  /**
269  Returns the absolute path of the home directory
270  @return the absolute path
271  */
272  std::string getUserHomeDir() const
273  { return m_HomeDir; };
274 
275  /**
276  Returns the absolute path for a given relative path in the user OpenFLUID directory
277  (i.e. $HOME/.openfluid/RelativePath)
278  @param[in] RelativePath the given relative path
279  @return the absolute path
280  */
281  std::string getUserDataPath(std::string RelativePath) const
282  { return m_UserDataDir + "/" + RelativePath; };
283 
284  /**
285  Checks the user data directory and create missing subdirectories and files
286  */
287  void prepareUserDataDirectory() const;
288 
289  /**
290  Returns the path of the temporary directory
291  @return the path of the temporary directory
292  */
293  inline std::string getTempDir() const
294  { return m_TempDir; };
295 
296  /**
297  Returns the path for a given input file (i.e. InputDir/Filename)
298  @param[in] Filename The given input file name
299  @return the path for a given input file
300  */
301  std::string getInputFullPath(std::string Filename) const
302  { return m_InputDir + "/" + Filename; };
303 
304  /**
305  Returns the path for a given output file (i.e. OutputDir/Filename)
306  @param[in] Filename The given output file name
307  @return the path for a given output file
308  */
309  inline std::string getOutputFullPath(std::string Filename) const
310  { return m_OutputDir + "/" + Filename; };
311 
312  /**
313  Returns the path for a given simulator plugin file, taking into account the simulator plugins path search order
314  @param[in] Filename The given simulator plugin file name
315  @return the first path found for a given simulator plugin file
316  */
317  std::string getSimulatorPluginFullPath(const std::string& Filename) const;
318 
319  /**
320  Adds search paths for plugins, separated by semicolon characters (i.e. /path/to/plugs:another/path/to/plugs).
321  These paths are added at the top of the search paths list.
322  @param[in] SemicolonSeparatedPaths a collection of paths separated by semicolons, as a std::string
323  */
324  void addExtraSimulatorsPluginsPaths(std::string SemicolonSeparatedPaths);
325 
327  { m_ExtraSimulatorsPlugsDirs.clear(); };
328 
329  inline std::vector<std::string> getDefaultSimulatorsPluginsPaths() const
330  { return m_DefaultSimulatorsPlugsDirs; };
331 
332  inline std::vector<std::string> getExtraSimulatorsPluginsPaths() const
333  { return m_ExtraSimulatorsPlugsDirs; };
334 
335  /**
336  Returns the ordered list of paths used to search for simulator plugins
337  @return the ordered list of paths
338  */
339  inline std::vector<std::string> getSimulatorsPluginsPaths() const
340  {
341  std::vector<std::string> ComposedPaths(m_ExtraSimulatorsPlugsDirs);
342  ComposedPaths.insert(ComposedPaths.end(),
343  m_DefaultSimulatorsPlugsDirs.begin(),
344  m_DefaultSimulatorsPlugsDirs.end());
345  return ComposedPaths;
346  };
347 
348  /**
349  Returns the path for a given observer plugin file, taking into account the observer plugins path search order
350  @param[in] Filename The given observer plugin file name
351  @return the first path found for a given observer plugin file
352  */
353  std::string getObserverPluginFullPath(const std::string& Filename) const;
354 
355  /**
356  Adds search paths for plugins, separated by semicolon characters (i.e. /path/to/plugs:another/path/to/plugs).
357  These paths are added at the top of the search paths list.
358  @param[in] SemicolonSeparatedPaths a collection of paths separated by semicolons, as a std::string
359  */
360  void addExtraObserversPluginsPaths(const std::string& SemicolonSeparatedPaths);
361 
363  { m_ExtraObserversPlugsDirs.clear(); };
364 
365  inline std::vector<std::string> getDefaultObserversPluginsPaths() const
366  { return m_DefaultObserversPlugsDirs; };
367 
368  inline std::vector<std::string> getExtraObserversPluginsPaths() const
369  { return m_ExtraObserversPlugsDirs; };
370 
371  /**
372  Returns the ordered list of paths used to search for observer plugins
373  @return the ordered list of paths
374  */
375  inline std::vector<std::string> getObserversPluginsPaths() const
376  {
377  std::vector<std::string> ComposedPaths(m_ExtraObserversPlugsDirs);
378  ComposedPaths.insert(ComposedPaths.end(),
379  m_DefaultObserversPlugsDirs.begin(),
380  m_DefaultObserversPlugsDirs.end());
381  return ComposedPaths;
382  };
383 
384 
385 
386  /**
387  Returns the install prefix path.
388  The install prefix is given at compile time, but can be overriden by the
389  OPENFLUID_INSTALL_PREFIX environment variable (recommended on windows systems).
390  @return the install prefix path
391  */
392  inline std::string getInstallPrefix() const
393  { return m_InstallPrefix; };
394 
395  /**
396  Returns the path for common resources, taking into account the install prefix path
397  @return the path for common resources
398  */
399  std::string getCommonResourcesDir() const;
400 
401  /**
402  Returns the path for a given file, relative to the common resources path
403  @param[in] RelativeFilePath The given file
404  @return the path for a given file in the common resources
405  */
406  std::string getCommonResourceFilePath(std::string RelativeFilePath) const;
407 
408  /**
409  Returns the path for resources of a given application, taking into account the install prefix path
410  @param[in] AppName The given application name
411  @return the path for resources of a given application
412  */
413  std::string getAppResourcesDir(std::string AppName) const;
414 
415  /**
416  Returns the path for a given file, relative a given application resources path
417  @param[in] AppName The given application name
418  @param[in] RelativeFilePath The given file
419  @return the path for a given file in the given application resources
420  */
421  std::string getAppResourceFilePath(std::string AppName, std::string RelativeFilePath) const;
422 
423  /**
424  Returns the translations directory
425  @return the path for translationslocale directory
426  */
427  std::string getTranslationsDir() const;
428 
429  /**
430  Returns the path for provided examples, taking into account the install prefix path
431  @return the path for provided examples
432  */
433  std::string getProvidedExamplesDir() const
434  { return m_ProvidedExamplesDir; };
435 
436 
437  /**
438  Returns the path for examples in user's directory
439  @return the path for user's examples
440  */
441  std::string getUserExamplesDir() const
442  { return m_UserExamplesDir; };
443 
444 
445  /**
446  Returns the extra properties list
447  @return the extra properties list
448  */
450  { return m_ExtraProperties; };
451 
452  /**
453  Returns the clear output directory flag
454  */
455  inline bool isClearOutputDir() const
456  { return m_ClearOutputDir; };
457 
458  /**
459  Sets the clear output directory flag
460  @param[in] ClearDir The value of the flag
461  */
462  inline void setClearOutputDir(bool ClearDir)
463  { m_ClearOutputDir = ClearDir; mp_WareEnv->setValue("mode.clearoutputdir",m_ClearOutputDir); };
464 
465 
467  { return mp_WareEnv; };
468 
469 
470  /**
471  Returns the local host name (if could be determined)
472  @return the local host name
473  */
474  std::string getHostName() const {return m_HostName; };
475 
476  /**
477  Returns the local user ID (if could be determined)
478  @return the local user ID
479  */
480  std::string getUserID() const {return m_UserID; };
481 
482  /**
483  Returns the local system architecture (if could be determined)
484  @return the local architecture
485  */
486  std::string getArch() const {return m_Arch; };
487 
488  unsigned int getSimulatorsMaxNumThreads() const
489  { return m_SimulatorsMaxNumThreads; };
490 
491  void setSimulatorsMaxNumThreads(const unsigned int& MaxNumThreads)
492  { if (MaxNumThreads > 0) m_SimulatorsMaxNumThreads = MaxNumThreads; };
493 
494  // TODO Values buffer size should not be managed here
495  void setValuesBufferSize(const unsigned int StepsNbr)
496  { m_ValuesBufferSize = StepsNbr; m_IsUserValuesBufferSize = true; };
497 
498  inline unsigned int getValuesBufferSize() const
499  { return m_ValuesBufferSize; };
500 
502  { return m_IsUserValuesBufferSize; };
503 
505  { m_IsUserValuesBufferSize = false; }
506 
507  void linkToProject();
508 
509  void detachFromProject();
510 
511  bool isLinkedToProject() const
512  { return m_IsLinkedToProject; };
513 
515  { return m_Profiling; };
516 
517  void setSimulationProfilingEnabled(bool Profiling)
518  { m_Profiling = Profiling; };
519 
520  void processWareParams(openfluid::ware::WareParams_t& Params) const;
521 };
522 
523 
524 } } //namespaces
525 
526 #endif
527 
528 
529 
std::vector< std::string > getExtraObserversPluginsPaths() const
Definition: RuntimeEnv.hpp:368
std::string getMarketBagDir() const
Definition: RuntimeEnv.hpp:202
std::string getUserHomeDir() const
Definition: RuntimeEnv.hpp:272
bool isUserValuesBufferSize() const
Definition: RuntimeEnv.hpp:501
std::vector< std::string > getDefaultSimulatorsPluginsPaths() const
Definition: RuntimeEnv.hpp:329
unsigned int getValuesBufferSize() const
Definition: RuntimeEnv.hpp:498
void setClearOutputDir(bool ClearDir)
Definition: RuntimeEnv.hpp:462
std::string getMarketBagBinSubDir() const
Definition: RuntimeEnv.hpp:243
std::string getMajorMinorVersion() const
Definition: RuntimeEnv.hpp:159
std::string getUserExamplesDir() const
Definition: RuntimeEnv.hpp:441
bool isClearOutputDir() const
Definition: RuntimeEnv.hpp:455
void unsetUserValuesBufferSize()
Definition: RuntimeEnv.hpp:504
std::string getMarketBagDataVersionDir() const
Definition: RuntimeEnv.hpp:237
std::string getMarketBagObsVersionDir() const
Definition: RuntimeEnv.hpp:223
std::string getVersion() const
Definition: RuntimeEnv.hpp:145
std::string getHostName() const
Definition: RuntimeEnv.hpp:474
bool isSimulationProfilingEnabled() const
Definition: RuntimeEnv.hpp:514
std::string getUserID() const
Definition: RuntimeEnv.hpp:480
std::string getInputDir() const
Definition: RuntimeEnv.hpp:175
std::string getTempDir() const
Definition: RuntimeEnv.hpp:293
std::string getFullVersion() const
Definition: RuntimeEnv.hpp:152
std::string getUserDataPath(std::string RelativePath) const
Definition: RuntimeEnv.hpp:281
std::string getArch() const
Definition: RuntimeEnv.hpp:486
std::string getInstallPrefix() const
Definition: RuntimeEnv.hpp:392
std::vector< std::string > getSimulatorsPluginsPaths() const
Definition: RuntimeEnv.hpp:339
std::string getInputFullPath(std::string Filename) const
Definition: RuntimeEnv.hpp:301
std::string getMarketBagSrcSubDir() const
Definition: RuntimeEnv.hpp:249
void resetExtraObserversPluginsPaths()
Definition: RuntimeEnv.hpp:362
void setSimulatorsMaxNumThreads(const unsigned int &MaxNumThreads)
Definition: RuntimeEnv.hpp:491
void setValuesBufferSize(const unsigned int StepsNbr)
Definition: RuntimeEnv.hpp:495
std::string getMarketBagBuildVersionDir() const
Definition: RuntimeEnv.hpp:230
Definition: RuntimeEnv.hpp:63
std::vector< std::string > getObserversPluginsPaths() const
Definition: RuntimeEnv.hpp:375
std::string getMarketBagVersionDir() const
Definition: RuntimeEnv.hpp:209
void setInputDir(const std::string &InputDir)
Definition: RuntimeEnv.hpp:168
openfluid::base::EnvironmentProperties & extraProperties()
Definition: RuntimeEnv.hpp:449
void resetExtraSimulatorsPluginsPaths()
Definition: RuntimeEnv.hpp:326
Definition: EnvProperties.hpp:53
std::string getDefaultConfigFile() const
Definition: RuntimeEnv.hpp:256
std::vector< std::string > getDefaultObserversPluginsPaths() const
Definition: RuntimeEnv.hpp:365
std::string getProvidedExamplesDir() const
Definition: RuntimeEnv.hpp:433
openfluid::base::EnvironmentProperties * wareEnvironment() const
Definition: RuntimeEnv.hpp:466
std::string getOutputFullPath(std::string Filename) const
Definition: RuntimeEnv.hpp:309
std::string getConfigFilePath(std::string Filename) const
Definition: RuntimeEnv.hpp:264
#define OPENFLUID_API
Definition: dllexport.hpp:87
unsigned int getSimulatorsMaxNumThreads() const
Definition: RuntimeEnv.hpp:488
bool isLinkedToProject() const
Definition: RuntimeEnv.hpp:511
std::vector< std::string > getExtraSimulatorsPluginsPaths() const
Definition: RuntimeEnv.hpp:332
std::string getMarketBagSimVersionDir() const
Definition: RuntimeEnv.hpp:216
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:93
void setOutputDir(const std::string &OutputDir)
Definition: RuntimeEnv.hpp:182
std::string getOutputDir() const
Definition: RuntimeEnv.hpp:189
void setSimulationProfilingEnabled(bool Profiling)
Definition: RuntimeEnv.hpp:517