OpenWalnut  1.5.0dev
WPathHelper.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPATHHELPER_H
26 #define WPATHHELPER_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <boost/filesystem.hpp>
33 
34 /**
35  * Singleton class helping to find files and paths. It is a useful to to search for resources and the central place to "hardcode" relative paths.
36  * It contains global paths only. Modules have their OWN local paths.
37  */
39 {
40 public:
41  /**
42  * Destructor.
43  */
44  virtual ~WPathHelper();
45 
46  /**
47  * Returns instance of the path helper. If it does not exists, it will be created.
48  *
49  * \return the running path helper instance.
50  */
51  static std::shared_ptr< WPathHelper > getPathHelper();
52 
53  /**
54  * Set the current application path. This should be called only once. The home path hereby is NOT the users home. It is a directory, where
55  * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
56  *
57  * \param appPath the application path
58  * \param homePath the OW home path
59  */
60  void setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath );
61 
62  /**
63  * Set the current application path. This should be called only once. The home path hereby is NOT the users home. It is a directory, where
64  * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
65  *
66  * \param appPath the application path
67  * \param homePath the OW home path
68  */
69  void setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath );
70 
71  /**
72  * The path where the binary file resides in. This is for example /usr/bin.
73  *
74  * \return the application path.
75  */
76  static boost::filesystem::path getAppPath();
77 
78  /**
79  * The path where font files reside in.
80  *
81  * \return the font path.
82  */
83  static boost::filesystem::path getFontPath();
84 
85  /**
86  * Paths to all known fonts.
87  */
88  typedef struct
89  {
90  /**
91  * The default font to use in most cases.
92  */
93  boost::filesystem::path Default;
94 
95  /**
96  * The Regular font (not bold, not italic)
97  */
98  boost::filesystem::path Regular;
99 
100  /**
101  * Italic font.
102  */
103  boost::filesystem::path Italic;
104 
105  /**
106  * Bold font.
107  */
108  boost::filesystem::path Bold;
109  }
110  Fonts;
111 
112  /**
113  * The paths to all fonts supported.
114  *
115  * \return the file paths to all fonts
116  */
117  static Fonts getAllFonts();
118 
119  /**
120  * The path to the global shaders. Modules usually have their own local shader directory.
121  *
122  * \return global shader path.
123  */
124  static boost::filesystem::path getShaderPath();
125 
126  /**
127  * The path to the globally installed modules. This does not respect any environment variables or config options! Use this only to search
128  * global modules. To get a list of all module search paths, including user defined ones, use getAllModulePaths().
129  *
130  * \return path to globally installed modules.
131  */
132  static boost::filesystem::path getModulePath();
133 
134  /**
135  * The path to the OW dir in the user's home. This will not be the home dir directly. It is something like $HOME/.OpenWalnut.
136  *
137  * \return OW home path
138  */
139  static boost::filesystem::path getHomePath();
140 
141  /**
142  * This returns a list of search paths for modules. This list is defined by the environment variable "OW_MODULE_PATH" and the list of additional
143  * module paths. All of these
144  * directories CAN contain modules. On startup, they get searched in the specified order.
145  *
146  * \return list of search paths for modules
147  */
148  static std::vector< boost::filesystem::path > getAllModulePaths();
149 
150  /**
151  * This method adds the given path to the list of module paths. This way, arbitrary paths can be specified to search for modules. Each path
152  * is searched recursively.
153  *
154  * \param path the path to add.
155  */
156  void addAdditionalModulePath( const boost::filesystem::path& path );
157 
158  /**
159  * Returns the list of paths added using addAdditionalModulePath. This does NOT contain the paths in OW_MODULE_PATH. Use getAllModulePaths
160  * for this.
161  *
162  * \return the list of additional paths
163  */
164  const std::vector< boost::filesystem::path >& getAdditionalModulePaths() const;
165 
166  /**
167  * The path to the OW libs. You normally should not need this.
168  *
169  * \return the path to the libs.
170  */
171  static boost::filesystem::path getLibPath();
172 
173  /**
174  * The path where shared files reside in.
175  *
176  * \return the shared files path.
177  */
178  static boost::filesystem::path getSharePath();
179 
180  /**
181  * The path where the doc files reside in.
182  *
183  * \return the doc file path.
184  */
185  static boost::filesystem::path getDocPath();
186 
187  /**
188  * The path where the config files reside in.
189  *
190  * \return the config file path.
191  */
192  static boost::filesystem::path getConfigPath();
193 
194  /**
195  * The path to a given module's resources. This should be used to get a share-like path for the module. The path is relative to the
196  * module's library path. This method is most useful for the module loader. You should not query your own resource path with this function.
197  * Use your module instance's m_localPath.
198  *
199  * \param moduleLibPath the path to the lib. Can be relative or absolute. This must be the directory the lib contains and NOT the path to the
200  * lib itself
201  * \param packageName the name of the resource. This is usually the package name.
202  *
203  * \return the absolute path for the given module path.
204  */
205  static boost::filesystem::path getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName );
206 
207 protected:
208  /**
209  * Constructors are protected because this is a Singleton.
210  */
211  WPathHelper();
212 
213 private:
214  /**
215  * Application path. NOT the path of the binary. The application path is the directory in which the binary is placed.
216  * The binary path is m_appPath+"/openwalnut".
217  */
218  boost::filesystem::path m_appPath;
219 
220  /**
221  * The path where all the shared files reside in.
222  */
223  boost::filesystem::path m_sharePath;
224 
225  /**
226  * The path where all the documentation files reside in.
227  */
228  boost::filesystem::path m_docPath;
229 
230  /**
231  * The path where all the config files reside in.
232  */
233  boost::filesystem::path m_configPath;
234 
235  /**
236  * The path to the globally installed modules.
237  */
238  boost::filesystem::path m_modulePath;
239 
240  /**
241  * The path to the OW libs.
242  */
243  boost::filesystem::path m_libPath;
244 
245  /**
246  * The path of a user specific OW directory.
247  */
248  boost::filesystem::path m_homePath;
249 
250  /**
251  * The path to module resources, relative to the module libraries path.
252  */
253  boost::filesystem::path m_moduleResourcePathRelative;
254 
255  /**
256  * A list of additional paths to search for modules. This does not contain the paths in the environment variable OW_MODULE_PATH. This method
257  * is not thread-safe. You should only use it before the module factory loads the modules.
258  */
259  std::vector< boost::filesystem::path > m_additionalModulePaths;
260 
261  /**
262  * Singleton instance of WPathHelper.
263  */
264  static std::shared_ptr< WPathHelper > m_instance;
265 };
266 
267 #endif // WPATHHELPER_H
268 
Singleton class helping to find files and paths.
Definition: WPathHelper.h:39
static boost::filesystem::path getFontPath()
The path where font files reside in.
Definition: WPathHelper.cpp:98
static std::shared_ptr< WPathHelper > m_instance
Singleton instance of WPathHelper.
Definition: WPathHelper.h:264
const std::vector< boost::filesystem::path > & getAdditionalModulePaths() const
Returns the list of paths added using addAdditionalModulePath.
static boost::filesystem::path getSharePath()
The path where shared files reside in.
virtual ~WPathHelper()
Destructor.
Definition: WPathHelper.cpp:47
boost::filesystem::path m_modulePath
The path to the globally installed modules.
Definition: WPathHelper.h:238
static boost::filesystem::path getConfigPath()
The path where the config files reside in.
WPathHelper()
Constructors are protected because this is a Singleton.
Definition: WPathHelper.cpp:42
boost::filesystem::path m_appPath
Application path.
Definition: WPathHelper.h:218
static boost::filesystem::path getShaderPath()
The path to the global shaders.
static boost::filesystem::path getModulePath()
The path to the globally installed modules.
std::vector< boost::filesystem::path > m_additionalModulePaths
A list of additional paths to search for modules.
Definition: WPathHelper.h:259
void setBasePaths(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:62
boost::filesystem::path m_moduleResourcePathRelative
The path to module resources, relative to the module libraries path.
Definition: WPathHelper.h:253
static boost::filesystem::path getLibPath()
The path to the OW libs.
boost::filesystem::path m_libPath
The path to the OW libs.
Definition: WPathHelper.h:243
static Fonts getAllFonts()
The paths to all fonts supported.
static boost::filesystem::path getHomePath()
The path to the OW dir in the user's home.
static std::shared_ptr< WPathHelper > getPathHelper()
Returns instance of the path helper.
Definition: WPathHelper.cpp:52
static boost::filesystem::path getModuleResourcePath(boost::filesystem::path moduleLibPath, std::string packageName)
The path to a given module's resources.
boost::filesystem::path m_configPath
The path where all the config files reside in.
Definition: WPathHelper.h:233
void addAdditionalModulePath(const boost::filesystem::path &path)
This method adds the given path to the list of module paths.
void setBasePathsOSXBundle(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:80
static std::vector< boost::filesystem::path > getAllModulePaths()
This returns a list of search paths for modules.
boost::filesystem::path m_docPath
The path where all the documentation files reside in.
Definition: WPathHelper.h:228
boost::filesystem::path m_homePath
The path of a user specific OW directory.
Definition: WPathHelper.h:248
static boost::filesystem::path getDocPath()
The path where the doc files reside in.
boost::filesystem::path m_sharePath
The path where all the shared files reside in.
Definition: WPathHelper.h:223
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
Paths to all known fonts.
Definition: WPathHelper.h:89
boost::filesystem::path Regular
The Regular font (not bold, not italic)
Definition: WPathHelper.h:98
boost::filesystem::path Bold
Bold font.
Definition: WPathHelper.h:108
boost::filesystem::path Default
The default font to use in most cases.
Definition: WPathHelper.h:93
boost::filesystem::path Italic
Italic font.
Definition: WPathHelper.h:103