template<class T>
Cached_entry_lock<T>::Cached_entry_lock(const Entry* e, Caching_bb<T>* cbb)
{
   target = e->clone();
   source = cbb;
   locked = true;
}

template<class T>
Cached_entry_lock<T>::~Cached_entry_lock()
{
   if (locked) source->unlock_entry(target);
   delete target;
}

template<class T>
bool Cached_entry_lock<T>::operator==(const Opus_lock* l) const
{
   const Cached_entry_lock<T>* tmp = 
      dynamic_cast<const Cached_entry_lock<T>*>(l);
   if (!tmp || source != tmp->source) return false;

   return(*target == tmp->target);
}

template<class T>
void Cached_entry_lock<T>::lock()
{
   if (locked) return;
   Cached_entry_lock<T>* lock = 
      dynamic_cast<Cached_entry_lock<T>*>(source->lock_entry(target));

   delete target;
   target = lock->target->clone();
   lock->locked = false; // prevents entry from being unlocked on delete
   delete lock;
   locked = true;
}

template<class T>
void Cached_entry_lock<T>::release()
{
   if (locked) source->unlock_entry(target);
   locked = false;
}

template<class T>
Entry* Cached_entry_lock<T>::get_locked_entry() const
{
   return target->clone();
}
