]> git.saurik.com Git - wxWidgets.git/blob - include/wx/snglinst.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[wxWidgets.git] / include / wx / snglinst.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/snglinst.h
3 // Purpose: wxSingleInstanceChecker can be used to restrict the number of
4 // simultaneously running copies of a program to one
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 08.06.01
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_SNGLINST_H_
13 #define _WX_SNGLINST_H_
14
15 #if wxUSE_SNGLINST_CHECKER
16
17 #include "wx/app.h"
18 #include "wx/utils.h"
19
20 // ----------------------------------------------------------------------------
21 // wxSingleInstanceChecker
22 // ----------------------------------------------------------------------------
23
24 class WXDLLIMPEXP_BASE wxSingleInstanceChecker
25 {
26 public:
27 // default ctor, use Create() after it
28 wxSingleInstanceChecker() { Init(); }
29
30 // like Create() but no error checking (dangerous!)
31 wxSingleInstanceChecker(const wxString& name,
32 const wxString& path = wxEmptyString)
33 {
34 Init();
35 Create(name, path);
36 }
37
38 // notice that calling Create() is optional now, if you don't do it before
39 // calling IsAnotherRunning(), CreateDefault() is used automatically
40 //
41 // name it is used as the mutex name under Win32 and the lock file name
42 // under Unix so it should be as unique as possible and must be non-empty
43 //
44 // path is optional and is ignored under Win32 and used as the directory to
45 // create the lock file in under Unix (default is wxGetHomeDir())
46 //
47 // returns false if initialization failed, it doesn't mean that another
48 // instance is running - use IsAnotherRunning() to check it
49 bool Create(const wxString& name, const wxString& path = wxEmptyString);
50
51 // use the default name, which is a combination of wxTheApp->GetAppName()
52 // and wxGetUserId() for mutex/lock file
53 //
54 // this is called implicitly by IsAnotherRunning() if the checker hadn't
55 // been created until then
56 bool CreateDefault()
57 {
58 wxCHECK_MSG( wxTheApp, false, "must have application instance" );
59 return Create(wxTheApp->GetAppName() + '-' + wxGetUserId());
60 }
61
62 // is another copy of this program already running?
63 bool IsAnotherRunning() const
64 {
65 if ( !m_impl )
66 {
67 if ( !const_cast<wxSingleInstanceChecker *>(this)->CreateDefault() )
68 {
69 // if creation failed, return false as it's better to not
70 // prevent this instance from starting up if there is an error
71 return false;
72 }
73 }
74
75 return DoIsAnotherRunning();
76 }
77
78 // dtor is not virtual, this class is not meant to be used polymorphically
79 ~wxSingleInstanceChecker();
80
81 private:
82 // common part of all ctors
83 void Init() { m_impl = NULL; }
84
85 // do check if another instance is running, called only if m_impl != NULL
86 bool DoIsAnotherRunning() const;
87
88 // the implementation details (platform specific)
89 class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl;
90
91 wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker);
92 };
93
94 #endif // wxUSE_SNGLINST_CHECKER
95
96 #endif // _WX_SNGLINST_H_