// ライトバリア
// # define DWB(type) MMgc::WriteBarrier< type >
template<class T>
class WriteBarrier {
private:
// ここのインライン化は重要らしい
REALLY_INLINE
T set(const T tNew) {
GC::WriteBarrier(&t, (const void*)tNew); // t を更新
return tNew;
}
public:
explicit REALLY_INLINE WriteBarrier() : t(0) {
}
explicit REALLY_INLINE WriteBarrier(T _t) {
set(_t);
}
REALLY_INLINE ~WriteBarrier() {
t = 0;
}
REALLY_INLINE T operator=(const WriteBarrier<T>& wb) {
return set(wb.t);
}
REALLY_INLINE T operator=(T tNew) {
return set(tNew);
}
// 目を背けてはいけない
REALLY_INLINE operator T() const { return t; }
REALLY_INLINE T value() const { return t; }
REALLY_INLINE operator ZeroPtr<T>() const { return t; }
REALLY_INLINE bool operator!=(T other) const { return other != t; }
REALLY_INLINE T operator->() const { return t; }
private:
// コピーを防ぐ (宣言のみで実装はされない)
WriteBarrier(const WriteBarrier<T>& toCopy);
T t;
};
template<class T>
class ZeroPtr {
public:
ZeroPtr() { t = NULL; }
ZeroPtr(T _t) : t(_t) { }
~ZeroPtr() { t = NULL; }
operator T() { return t; }
bool operator!=(T other) { return other != t; }
T operator->() const { return t; }
private:
T t;
};
// -----------------------------------------------------------
// 弱参照
template<class T>
class GCWeakRefPtr {
public:
GCWeakRefPtr() {}
GCWeakRefPtr(T t) { set(t); }
~GCWeakRefPtr() { t = NULL; }
T operator=(const GCWeakRefPtr<T>& wb) { return set(wb.t); }
T operator=(T tNew) { return set(tNew); }
operator T() const { return (T) t->get(); }
bool operator!=(T other) const { return other != t; }
T operator->() const { return (T) t->get(); }
private:
T set(const T tNew) {
t = tNew->GetWeakRef();
}
// GCObject への Fat Pointer、GCはハッシュテーブルを持つ({weakref:object, ...})
GCWeakRef* t;
};