OpenWalnut  1.4.0
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 <string>
29 #include <vector>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/filesystem.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/shared_ptr.hpp>
36 #endif
37 
38 /**
39  * 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.
40  * It contains global paths only. Modules have their OWN local paths.
41  */
43 {
44 public:
45  /**
46  * Destructor.
47  */
48  virtual ~WPathHelper();
49 
50  /**
51  * Returns instance of the path helper. If it does not exists, it will be created.
52  *
53  * \return the running path helper instance.
54  */
55  static boost::shared_ptr< WPathHelper > getPathHelper();
56 
57  /**
58  * 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
59  * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
60  *
61  * \param appPath the application path
62  * \param homePath the OW home path
63  */
64  void setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath );
65 
66  /**
67  * 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
68  * OW can write user specific data. A good default here is to specify USERHOME/.OpenWalnut for example.
69  *
70  * \param appPath the application path
71  * \param homePath the OW home path
72  */
73  void setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath );
74 
75  /**
76  * The path where the binary file resides in. This is for example /usr/bin.
77  *
78  * \return the application path.
79  */
80  static boost::filesystem::path getAppPath();
81 
82  /**
83  * The path where font files reside in.
84  *
85  * \return the font path.
86  */
87  static boost::filesystem::path getFontPath();
88 
89  /**
90  * Paths to all known fonts.
91  */
92  typedef struct
93  {
94  /**
95  * The default font to use in most cases.
96  */
97  boost::filesystem::path Default;
98 
99  /**
100  * The Regular font (not bold, not italic)
101  */
102  boost::filesystem::path Regular;
103 
104  /**
105  * Italic font.
106  */
107  boost::filesystem::path Italic;
108 
109  /**
110  * Bold font.
111  */
112  boost::filesystem::path Bold;
113  }
114  Fonts;
115 
116  /**
117  * The paths to all fonts supported.
118  *
119  * \return the file paths to all fonts
120  */
121  static Fonts getAllFonts();
122 
123  /**
124  * The path to the global shaders. Modules usually have their own local shader directory.
125  *
126  * \return global shader path.
127  */
128  static boost::filesystem::path getShaderPath();
129 
130  /**
131  * The path to the globally installed modules. This does not respect any environment variables or config options! Use this only to search
132  * global modules. To get a list of all module search paths, including user defined ones, use getAllModulePaths().
133  *
134  * \return path to globally installed modules.
135  */
136  static boost::filesystem::path getModulePath();
137 
138  /**
139  * 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.
140  *
141  * \return OW home path
142  */
143  static boost::filesystem::path getHomePath();
144 
145  /**
146  * 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
147  * module paths. All of these
148  * directories CAN contain modules. On startup, they get searched in the specified order.
149  *
150  * \return list of search paths for modules
151  */
152  static std::vector< boost::filesystem::path > getAllModulePaths();
153 
154  /**
155  * 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
156  * is searched recursively.
157  *
158  * \param path the path to add.
159  */
160  void addAdditionalModulePath( const boost::filesystem::path& path );
161 
162  /**
163  * Returns the list of paths added using addAdditionalModulePath. This does NOT contain the paths in OW_MODULE_PATH. Use getAllModulePaths
164  * for this.
165  *
166  * \return the list of additional paths
167  */
168  const std::vector< boost::filesystem::path >& getAdditionalModulePaths() const;
169 
170  /**
171  * The path to the OW libs. You normally should not need this.
172  *
173  * \return the path to the libs.
174  */
175  static boost::filesystem::path getLibPath();
176 
177  /**
178  * The path where shared files reside in.
179  *
180  * \return the shared files path.
181  */
182  static boost::filesystem::path getSharePath();
183 
184  /**
185  * The path where the doc files reside in.
186  *
187  * \return the doc file path.
188  */
189  static boost::filesystem::path getDocPath();
190 
191  /**
192  * The path where the config files reside in.
193  *
194  * \return the config file path.
195  */
196  static boost::filesystem::path getConfigPath();
197 
198  /**
199  * 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
200  * module's library path. This method is most useful for the module loader. You should not query your own resource path with this function.
201  * Use your module instance's m_localPath.
202  *
203  * \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
204  * lib itself
205  * \param packageName the name of the resource. This is usually the package name.
206  *
207  * \return the absolute path for the given module path.
208  */
209  static boost::filesystem::path getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName );
210 
211 protected:
212  /**
213  * Constructors are protected because this is a Singleton.
214  */
215  WPathHelper();
216 
217 private:
218  /**
219  * Application path. NOT the path of the binary. The application path is the directory in which the binary is placed.
220  * The binary path is m_appPath+"/openwalnut".
221  */
222  boost::filesystem::path m_appPath;
223 
224  /**
225  * The path where all the shared files reside in.
226  */
227  boost::filesystem::path m_sharePath;
228 
229  /**
230  * The path where all the documentation files reside in.
231  */
232  boost::filesystem::path m_docPath;
233 
234  /**
235  * The path where all the config files reside in.
236  */
237  boost::filesystem::path m_configPath;
238 
239  /**
240  * The path to the globally installed modules.
241  */
242  boost::filesystem::path m_modulePath;
243 
244  /**
245  * The path to the OW libs.
246  */
247  boost::filesystem::path m_libPath;
248 
249  /**
250  * The path of a user specific OW directory.
251  */
252  boost::filesystem::path m_homePath;
253 
254  /**
255  * The path to module resources, relative to the module libraries path.
256  */
257  boost::filesystem::path m_moduleResourcePathRelative;
258 
259  /**
260  * A list of additional paths to search for modules. This does not contain the paths in the environment variable OW_MODULE_PATH. This method
261  * is not thread-safe. You should only use it before the module factory loads the modules.
262  */
263  std::vector< boost::filesystem::path > m_additionalModulePaths;
264 
265  /**
266  * Singleton instance of WPathHelper.
267  */
268  static boost::shared_ptr< WPathHelper > m_instance;
269 };
270 
271 #endif // WPATHHELPER_H
272 
boost::filesystem::path m_moduleResourcePathRelative
The path to module resources, relative to the module libraries path.
Definition: WPathHelper.h:257
static boost::filesystem::path getConfigPath()
The path where the config files reside in.
static boost::filesystem::path getDocPath()
The path where the doc files reside in.
static boost::filesystem::path getHomePath()
The path to the OW dir in the user's home.
void setBasePaths(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:57
static Fonts getAllFonts()
The paths to all fonts supported.
void setBasePathsOSXBundle(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:73
boost::filesystem::path m_appPath
Application path.
Definition: WPathHelper.h:222
static boost::filesystem::path getFontPath()
The path where font files reside in.
Definition: WPathHelper.cpp:94
Paths to all known fonts.
Definition: WPathHelper.h:92
std::vector< boost::filesystem::path > m_additionalModulePaths
A list of additional paths to search for modules.
Definition: WPathHelper.h:263
boost::filesystem::path m_modulePath
The path to the globally installed modules.
Definition: WPathHelper.h:242
static boost::filesystem::path getShaderPath()
The path to the global shaders.
Definition: WPathHelper.cpp:99
Singleton class helping to find files and paths.
Definition: WPathHelper.h:42
virtual ~WPathHelper()
Destructor.
Definition: WPathHelper.cpp:42
boost::filesystem::path m_docPath
The path where all the documentation files reside in.
Definition: WPathHelper.h:232
boost::filesystem::path Default
The default font to use in most cases.
Definition: WPathHelper.h:97
static std::vector< boost::filesystem::path > getAllModulePaths()
This returns a list of search paths for modules.
static boost::shared_ptr< WPathHelper > getPathHelper()
Returns instance of the path helper.
Definition: WPathHelper.cpp:47
boost::filesystem::path m_sharePath
The path where all the shared files reside in.
Definition: WPathHelper.h:227
void addAdditionalModulePath(const boost::filesystem::path &path)
This method adds the given path to the list of module paths.
static boost::shared_ptr< WPathHelper > m_instance
Singleton instance of WPathHelper.
Definition: WPathHelper.h:268
boost::filesystem::path m_homePath
The path of a user specific OW directory.
Definition: WPathHelper.h:252
boost::filesystem::path m_configPath
The path where all the config files reside in.
Definition: WPathHelper.h:237
boost::filesystem::path Italic
Italic font.
Definition: WPathHelper.h:107
static boost::filesystem::path getLibPath()
The path to the OW libs.
static boost::filesystem::path getSharePath()
The path where shared files reside in.
boost::filesystem::path Regular
The Regular font (not bold, not italic)
Definition: WPathHelper.h:102
WPathHelper()
Constructors are protected because this is a Singleton.
Definition: WPathHelper.cpp:37
boost::filesystem::path m_libPath
The path to the OW libs.
Definition: WPathHelper.h:247
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:89
static boost::filesystem::path getModulePath()
The path to the globally installed modules.
static boost::filesystem::path getModuleResourcePath(boost::filesystem::path moduleLibPath, std::string packageName)
The path to a given module's resources.
boost::filesystem::path Bold
Bold font.
Definition: WPathHelper.h:112
const std::vector< boost::filesystem::path > & getAdditionalModulePaths() const
Returns the list of paths added using addAdditionalModulePath.