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

#include "zthread/ZThread.h"
#include "zthread/SynchronizationExceptions.h"

namespace ZThread {


/**
 * @class AbstractThreadLocal
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:18:31-0500>
 * @version 2.0.0a
 *
 * This class implements a common set of functionality for thread specific 
 * value classes. By default, on most systems, the native libraries only
 * provide support for void* value and no support for inhertiable or initial
 * value setting.
 *
 * This class implements the minimum set of functionality needed to provide
 * more specific, and strongly typed support than is otherwise available.
 * 
 * NOTE: 
 * Any AbstractThreadLocal MUST be a static variable. This allows for the 
 * fastest and most logical use of thread sepcific variables. In addition,
 * it is not worth the extra overhead involved with keeping track of variables
 * that may go out of scope of be deteled from one thread while others are 
 * using. While this would be possible, there is a speed tradeoff & a strange 
 * relationship between a thread and ThreadLocal variables that might 
 * disappear. It seems a little too awkawrd with very little, if any,
 * benefit to use these variables in that way. 
 *
 * @see ThreadLocal
 * @see InheritableThreadLocal
 * @see FastThreadLocal
 * 
 */
class ZTHREAD_API AbstractThreadLocal {

  friend class ThreadLocalImpl;
  friend class ThreadImpl;
  
  //! delegate
  ThreadLocalImpl* _impl;

  //! Disabled 
  AbstractThreadLocal(const AbstractThreadLocal&);
  AbstractThreadLocal& operator=(const AbstractThreadLocal&);

  public:

  /**
   * Create an AbstractThreadLocal
   */
  AbstractThreadLocal() throw();

  /**
   * Destroy this AbstractThreadLocal
   */
  virtual ~AbstractThreadLocal() throw();

  protected:

  /** 
   * Invoked by the framework the first time get() is invoked by the
   * current thread. The value return is associated with the current 
   * thread and this object. 
   */
  virtual void* initialValue() const throw() = 0;

  /** 
   * Invoked by the framework whenever the value associated with current
   * thread and object is about to propogate to a child thread. This 
   * method is not invoked if there is no value associated with the
   * current thread and this object.
   *
   * @param void* - related initialization value
   */
  inline virtual void* childValue(void* parentValue) const throw() {

    return (void*)0;
  }


  /** 
   * Invoked by the framework whenever it needs to be determined wether or not 
   * this object should propogate values to child threads.
   *
   * @param bool - true if this value should propgate, otherwise false. Unless
   * overidden, this method will always return false
   */
  virtual bool propogateValue() const throw() {
    return false;
  }

  /**
   * Get the value associated with the current thread and this object via fetch().
   * If no association exists, then initialValue() is invoked.
   *
   * @exception Synchronization_Exception - thrown if there is an error
   * allocating native thread local storage
   */
  void* get() const
    throw(Synchronization_Exception);
  
  /**
   * Set the value associated with the current thread and this object. This value 
   * can only be retrieved from the current thread.
   *
   * @exception Synchronization_Exception - thrown if there is an error
   * allocating native thread local storage
   */
  void* set(void*) const
    throw(Synchronization_Exception);

  
  void* fetch() const
    throw(Synchronization_Exception);

  void* put(void*) const
    throw(Synchronization_Exception);

};


};

#endif

