/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CUniqueInstance.h  -  description
                             -------------------
    begin                : June 6 2002
    copyright            : (C) 2002-2005 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CUNIQUE_INSTANCE_H__
#define __CUNIQUE_INSTANCE_H__

/// @addtogroup utility Utility Classes
/// @{

#ifdef _WIN32
#include <windows.h>
#endif

#include <sptk3/sptk.h>
#include <sptk3/CStrings.h>

/// @brief Unique instance object
///
/// Tries to create a mutex object and check if it's unique for the
/// program with the instance name. If one instance per user is desired
/// simply incorporate the user name into the instance.
class SP_EXPORT CUniqueInstance {
   /// Instance name
   std::string  m_instanceName;
   /// Lock is created
   bool     m_lockCreated;

#ifdef _WIN32
   /// The named mutex object
   HANDLE   m_mutex;
#else
   /// The lock file name
   std::string  m_fileName;

   /// Gets the process ID
   int      read_pid();

   /// Writes the process ID into the lock file
   int      write_pid();
#endif

public:
   /// Constructor
   /// @param instanceName CString, instance name
   CUniqueInstance(std::string instanceName);

   /// Destructor
   ~CUniqueInstance();

   /// Reports true if the instance is unique
   bool isUnique();
};

/// @}

#endif
