snglinst.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef _WX_SNGLINST_H_
  12. #define _WX_SNGLINST_H_
  13. #if wxUSE_SNGLINST_CHECKER
  14. #include "wx/app.h"
  15. #include "wx/utils.h"
  16. // ----------------------------------------------------------------------------
  17. // wxSingleInstanceChecker
  18. // ----------------------------------------------------------------------------
  19. class WXDLLIMPEXP_BASE wxSingleInstanceChecker
  20. {
  21. public:
  22. // default ctor, use Create() after it
  23. wxSingleInstanceChecker() { Init(); }
  24. // like Create() but no error checking (dangerous!)
  25. wxSingleInstanceChecker(const wxString& name,
  26. const wxString& path = wxEmptyString)
  27. {
  28. Init();
  29. Create(name, path);
  30. }
  31. // notice that calling Create() is optional now, if you don't do it before
  32. // calling IsAnotherRunning(), CreateDefault() is used automatically
  33. //
  34. // name it is used as the mutex name under Win32 and the lock file name
  35. // under Unix so it should be as unique as possible and must be non-empty
  36. //
  37. // path is optional and is ignored under Win32 and used as the directory to
  38. // create the lock file in under Unix (default is wxGetHomeDir())
  39. //
  40. // returns false if initialization failed, it doesn't mean that another
  41. // instance is running - use IsAnotherRunning() to check it
  42. bool Create(const wxString& name, const wxString& path = wxEmptyString);
  43. // use the default name, which is a combination of wxTheApp->GetAppName()
  44. // and wxGetUserId() for mutex/lock file
  45. //
  46. // this is called implicitly by IsAnotherRunning() if the checker hadn't
  47. // been created until then
  48. bool CreateDefault()
  49. {
  50. wxCHECK_MSG( wxTheApp, false, "must have application instance" );
  51. return Create(wxTheApp->GetAppName() + '-' + wxGetUserId());
  52. }
  53. // is another copy of this program already running?
  54. bool IsAnotherRunning() const
  55. {
  56. if ( !m_impl )
  57. {
  58. if ( !const_cast<wxSingleInstanceChecker *>(this)->CreateDefault() )
  59. {
  60. // if creation failed, return false as it's better to not
  61. // prevent this instance from starting up if there is an error
  62. return false;
  63. }
  64. }
  65. return DoIsAnotherRunning();
  66. }
  67. // dtor is not virtual, this class is not meant to be used polymorphically
  68. ~wxSingleInstanceChecker();
  69. private:
  70. // common part of all ctors
  71. void Init() { m_impl = NULL; }
  72. // do check if another instance is running, called only if m_impl != NULL
  73. bool DoIsAnotherRunning() const;
  74. // the implementation details (platform specific)
  75. class WXDLLIMPEXP_FWD_BASE wxSingleInstanceCheckerImpl *m_impl;
  76. wxDECLARE_NO_COPY_CLASS(wxSingleInstanceChecker);
  77. };
  78. #endif // wxUSE_SNGLINST_CHECKER
  79. #endif // _WX_SNGLINST_H_