/*
 *  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 __ZTFACTORY_H__
#define __ZTFACTORY_H__

#include "zthread/Singleton.h"
#include "zthread/FastMutex.h"

namespace ZThread {

/**
 * @class FactoryImpl
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-03-04T18:41:00-0500>
 * @version 1.2.2
 *
 * Factory implementation.
 */
template<class T>
class ZTHREAD_API FactoryImpl {
  public:

  inline FactoryImpl() {}
  inline virtual ~FactoryImpl() {}

  /**
   * Create a new product or type T
   *
   * @return T* - new product
   */
  virtual T* create() = 0;


};

/**
 * @class Factory
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-03-04T18:41:00-0500>
 * @version 1.2.2
 *
 * Template class to ease Factory object creation. This template just sets
 * up some standard semantics for using and maintaing a Factory object.
 * Its very much like a Singleton with a changable target.
 * 
 */
template<class T, class F=FactoryImpl<T>, class LOCK=ZThread::FastMutex>
class ZTHREAD_API Factory {
  
  Factory();
  ~Factory();

  public:

  /**
   * Create a new instance using the currently set FactoryImpl
   *
   * @return T* - new product
   */
  static T* create();

  /**
   * Set the instance of the desired Factory object
   *
   * @param F* - new instance
   * @return F* - old instance
   *
   * @post the new instance will be owned by this Factory object. It
   * will be deleted when this Factorys SingletonDestroyer goes out of 
   * scope (when the program terminates). The user must assume the 
   * responsibility of destroying the old instance that was previously
   * managed by the Factory.
   */
  static F* setInstance(F*);

  /**
   * Get the current instance of the Factory object. If no instance has been
   * explicitly set with the setInstance() method, a new F created.
   *
   * @return F* - old instance
   */
  static F* getInstance();

  private:

  static F* _impl;
  static LOCK _lock;
  static SingletonDestroyer<F> _destroyer;

};

template<class T, class F, class LOCK>
F* Factory<T,F,LOCK>::_impl = 0;

template<class T, class F, class LOCK>
LOCK Factory<T,F,LOCK>::_lock;

template<class T, class F, class LOCK>
SingletonDestroyer<F> Factory<T,F,LOCK>::_destroyer;


template<class T, class F, class LOCK>
T* Factory<T,F,LOCK>::create() {

  Guard<FastMutex> g(_lock);
  
  if(!_impl)
    _impl = new F;

  return _impl->create();

  // return (!_impl) ? 0 : _impl->create();

}

template<class T, class F, class LOCK>
F* Factory<T,F,LOCK>::setInstance(F* impl) {

  if(impl) {
    
    Guard<FastMutex> g(_lock);
    if(!_impl) {
      
      _impl = impl;
      _destroyer.setDoomed(_impl);
      
      return _impl;
      
    }
    
  }
  
  return 0;

}


template<class T, class F, class LOCK>
F* Factory<T,F,LOCK>::getInstance() {
  
  Guard<FastMutex> g(_lock);  

  if(!_impl)
    _impl = new F;

  return _impl;

}


};

#endif






