#ifndef WeakGCMap_h
#define WeakGCMap_h
#include "Collector.h"
#include <wtf/HashMap.h>
namespace JSC {
class JSCell;
template<typename KeyType, typename MappedType>
class WeakGCMap : public FastAllocBase {
public:
typedef typename HashMap<KeyType, MappedType>::iterator iterator;
typedef typename HashMap<KeyType, MappedType>::const_iterator const_iterator;
bool isEmpty() { return m_map.isEmpty(); }
MappedType get(const KeyType& key) const;
pair<iterator, bool> set(const KeyType&, const MappedType&);
MappedType take(const KeyType& key);
MappedType uncheckedGet(const KeyType& key) const { return m_map.get(key); }
bool uncheckedRemove(const KeyType&, const MappedType&);
iterator uncheckedBegin() { return m_map.begin(); }
iterator uncheckedEnd() { return m_map.end(); }
const_iterator uncheckedBegin() const { return m_map.begin(); }
const_iterator uncheckedEnd() const { return m_map.end(); }
private:
HashMap<KeyType, MappedType> m_map;
};
template<typename KeyType, typename MappedType>
inline MappedType WeakGCMap<KeyType, MappedType>::get(const KeyType& key) const
{
MappedType result = m_map.get(key);
if (result == HashTraits<MappedType>::emptyValue())
return result;
if (!Heap::isCellMarked(result))
return HashTraits<MappedType>::emptyValue();
return result;
}
template<typename KeyType, typename MappedType>
MappedType WeakGCMap<KeyType, MappedType>::take(const KeyType& key)
{
MappedType result = m_map.take(key);
if (result == HashTraits<MappedType>::emptyValue())
return result;
if (!Heap::isCellMarked(result))
return HashTraits<MappedType>::emptyValue();
return result;
}
template<typename KeyType, typename MappedType>
pair<typename HashMap<KeyType, MappedType>::iterator, bool> WeakGCMap<KeyType, MappedType>::set(const KeyType& key, const MappedType& value)
{
Heap::markCell(value); pair<iterator, bool> result = m_map.add(key, value);
if (!result.second) { result.second = !Heap::isCellMarked(result.first->second);
result.first->second = value;
}
return result;
}
template<typename KeyType, typename MappedType>
bool WeakGCMap<KeyType, MappedType>::uncheckedRemove(const KeyType& key, const MappedType& value)
{
iterator it = m_map.find(key);
if (it == m_map.end())
return false;
if (it->second != value)
return false;
m_map.remove(it);
return true;
}
}
#endif // WeakGCMap_h