]> git.lyx.org Git - lyx.git/blob - src/support/path.h
dont use pragma impementation and interface anymore
[lyx.git] / src / support / path.h
1 // -*- C++ -*-
2 /**
3  * \file path.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #ifndef PATH_H
13 #define PATH_H
14
15 #include "LString.h"
16 #include "lyxlib.h"
17 #include <boost/utility.hpp>
18
19 /**
20  * Path - utility closs for stackable working directories
21  *
22  * You can use a local variable of this type to temporarily
23  * change to a directory as the cwd, for example :
24  *
25  * if (blah) {
26  *      Path p("/tmp/blah");
27  *      ...
28  * }
29  *
30  * At the end of p's scope the cwd is reset to its previous value.
31  */
32 class Path : boost::noncopyable {
33 public:
34         /// change to the given directory
35         explicit
36         Path(string const & path)
37                 : popped_(false)
38         {
39                 if (!path.empty()) {
40                         pushedDir_ = lyx::getcwd();
41                         if (pushedDir_.empty() || lyx::chdir(path))
42                                 /* FIXME: throw */;
43                 } else {
44                         popped_ = true;
45                 }
46         }
47
48         /// set cwd to the previous value if needed
49         ~Path()
50         {
51                 if (!popped_) pop();
52         }
53
54         /// set cwd to the previous value if needed
55         int pop();
56 private:
57         /// whether we are in the new cwd or not
58         bool popped_;
59         /// the previous cwd
60         string pushedDir_;
61 };
62
63 // To avoid the wrong usage:
64 // Path("/tmp");   // wrong
65 // Path p("/tmp");  // right
66 // we add this macro:
67 ///
68 #define Path(x) unnamed_Path;
69 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
70
71 #endif // PATH_H