/*
 *  ZThreads, a platform-independant, multithreading and 
 *  synchroniation library
 *
 *  Copyright (C) 2000-2002, Eric Crahen, See LGPL.TXT for details
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser 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
 *
 *  SUNY @ Buffalo, hereby disclaims all copyright interest in the
 *  ZThreads library written by Eric Crahen
 */

#ifndef __ZTSINGLETON_H__
#define __ZTSINGLETON_H__

#include "zthread/ZThread.h"
#include "zthread/FastMutex.h"
#include "zthread/Guard.h"

namespace ZThread {
  
/**
 * @class SingletonDestroyer
 * 
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:30:36-0500>
 * @version 1.1.0
 *
 * Based on the work of John Vlissidles in his book 'Pattern Hatching'
 *
 * This template class has one job and that is to kill a Singleton of
 * of type DOOMED. This class is used internally by the template Singleton
 * class an is not needed, in general, by client code.
 *
 */
template <class DOOMED>
class SingletonDestroyer {
  
  public:

  /**
   * Create a SingletonDestroyer that will delete a particular object
   *
   * @param DOOMED* - object to delete
   */
  SingletonDestroyer(DOOMED* = 0) throw();

  /**
   * Destroy the SingletonDestroyer and its target (DOOMED*)
   */
  ~SingletonDestroyer() throw();
  
  /**
   * Change the target object that will be destroyed
   *
   * @parma DOOMED* - target object
   */
  void setDoomed(DOOMED*);
  
  private:
  
  // Prevent users from making copies of a 
  // SingletonDestroyer to avoid double deletion:
  SingletonDestroyer(const SingletonDestroyer<DOOMED>&);
  void operator=(const SingletonDestroyer<DOOMED>&);
  
  private:
  DOOMED* _doomed;
};

/**
 * Create a new SingletonDestroyer whose purpose is to exist only to
 * destroy a specific Singleton of type DOOMED when the SingletonDestroyer
 * goes out of scope
 *
 * @param DOOMED* Singleton to destroy
 */ 
template <class DOOMED>
  SingletonDestroyer<DOOMED>::SingletonDestroyer (DOOMED* d) throw() {
  setDoomed(d);
}

/**
 * Destroy the Singleton with the SingletonDestroyer
 */
template <class DOOMED>
SingletonDestroyer<DOOMED>::~SingletonDestroyer() throw() {
  
  try {

    if(_doomed != (DOOMED*)0)
      delete _doomed;

  } catch(...) { }

  _doomed = (DOOMED*)0;

}

/**
 * Change the target of this SingletonDestroyer to a new DOOMED object
 *
 * @param DOOMED* new target
 */
template <class DOOMED>
void SingletonDestroyer<DOOMED>::setDoomed (DOOMED* d) {
  _doomed = d;
}
  
/**
 * @class Singleton
 * 
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:30:36-0500>
 * @version 1.4.3 
 *
 * Based on the work of John Vlissidles in his book 'Pattern Hatching'
 * and on an article by Douglas Schmidtt on double checked locking.
 *
 * This is a thread safe wrapper for creating Singleton classes. 
 * The Singleton class is of type LONESOME and is automatically constructed
 * the first time it is used, it will then persist for the lifetime of 
 * the program. 
 *
 */
template <class LONESOME, class LOCK=ZThread::FastMutex>
class Singleton {
  
  public:
  /**
   * Provide access to the single instance through double-checked locking 
   *
   * @return LONESOME* single instance 
   */
  static LONESOME* instance() {
    
    if(!_flag) {

      Guard<LOCK> guard(_lock);
      if(!_flag) {
        
        _Instance = new LONESOME;
        _Destroyer.setDoomed(_Instance);
        
        _flag = true;

      } 
    }
    
    return _Instance;
  };
  
  protected:
  
  static LOCK  _lock;
  static bool _flag;

  static LONESOME*  _Instance;
  static SingletonDestroyer<LONESOME> _Destroyer;
  
};

/**
 * Mutex used to perform to double-checked locking that makes this 
 * Singleton thread safe
 */
template <class LONESOME, class LOCK>
LOCK Singleton<LONESOME, LOCK>::_lock;

/**
 * Flag to keep over-aggressive compilers from optimizing away the 
 * double check
 */
template <class LONESOME, class LOCK>
bool Singleton<LONESOME, LOCK>::_flag = false;

/**
 * SingletonDestroyer that is created to destroy this Singleton
 */
template <class LONESOME, class LOCK>
SingletonDestroyer<LONESOME> Singleton<LONESOME, LOCK>::_Destroyer;

/**
 * LONESOME* for the actual object this Singleton is providing access
 * to.
 */
template <class LONESOME, class LOCK>
LONESOME* Singleton<LONESOME, LOCK>::_Instance = 0;
  

/**
 * @class StaticSingleton
 * 
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:30:36-0500>
 * @version 1.1.1
 *
 * This is an alternative implementation of a Singleton that
 * really does not require any form of locking. The Singleton
 * objects are statically initialized. This is quite useful
 * for things like State objects. This was implemented because
 * a state machine problem I was working on and it seemed
 * appropriate to include support for a more lightweight 
 * Singleton.
 */
template <class LONESOME>
class StaticSingleton {

  public:

  /**
   * Get the statically initialized instance of type LONESOME
   *
   * @return LONESOME* - static instance
   */
  static LONESOME* instance() {


    static LONESOME _obj;
    return &_obj;

  }


};

};

#endif


