///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//  Copyright (C) 1995-1998 by the Board of Trustees of Leland Stanford      //
//  Junior University.  See LICENSE for details.                             //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#ifndef _scoped_hash_h_
#define _scoped_hash_h_

#include "hash.h"
#include "scope.h"

template <class _HTKey, class _HTData>
class Scoped_Hash_Table : public Scoped_Obj {
protected:
  Hash_Table<_HTKey,_HTData> *_ht;
  PScoped_Obj MakeCopy();
  void RestoreData();
  void DeleteSelf();

public:
  Scoped_Hash_Table(Hash_Table<_HTKey,_HTData> *ht) : Scoped_Obj(), _ht(ht) {}
  Scoped_Hash_Table(const Scoped_Hash_Table<_HTKey,_HTData> &s) : 
    Scoped_Obj(s) { _ht = new Hash_Table<_HTKey,_HTData>(*(s._ht)); }
  ~Scoped_Hash_Table() { delete _ht; }

  Hash_Table<_HTKey, _HTData> *GetHashTable() { MakeCurrent(); return _ht; }
};

template <class _HTKey, class _HTData>
PScoped_Obj
Scoped_Hash_Table<_HTKey, _HTData>::MakeCopy() {
  return (PScoped_Obj)(new Scoped_Hash_Table<_HTKey, _HTData>(*this));
}

template <class _HTKey, class _HTData>
void
Scoped_Hash_Table<_HTKey, _HTData>::RestoreData() {
  Scoped_Hash_Table<_HTKey, _HTData> *old =
    (Scoped_Hash_Table<_HTKey, _HTData> *)GetRestore();
  *_ht = *(old->_ht);
}

template <class _HTKey, class _HTData>
void
Scoped_Hash_Table<_HTKey, _HTData>::DeleteSelf() {
  delete this;
}

#endif
