prime/include/Kyoto/TToken.hpp

85 lines
2.0 KiB
C++
Raw Normal View History

#ifndef _TTOKEN
#define _TTOKEN
2022-08-13 01:26:00 +00:00
#include "types.h"
#include "Kyoto/CSimplePool.hpp"
#include "Kyoto/CToken.hpp"
#include "Kyoto/IObj.hpp"
#include "rstl/auto_ptr.hpp"
template < typename T >
class TToken : public CToken {
public:
TToken() {}
TToken(const CToken& token) : CToken(token) {}
2022-10-01 06:19:09 +00:00
TToken(T* obj) : CToken(TObjOwnerDerivedFromIObj< T >::GetNewDerivedObject(obj).release()) {}
2022-08-13 01:26:00 +00:00
TToken(const rstl::auto_ptr< T >& obj) : CToken(GetIObjObjectFor(obj).release()) {}
T* GetT() { return reinterpret_cast< T* >(CToken::GetObj()->GetContents()); }
T* operator*() { return GetT(); }
2022-10-20 11:18:42 +00:00
T* operator->() { return GetT(); }
2022-09-18 06:05:46 +00:00
static inline rstl::auto_ptr< TObjOwnerDerivedFromIObj< T > >
GetIObjObjectFor(const rstl::auto_ptr< T >& obj) {
2022-08-13 01:26:00 +00:00
return TObjOwnerDerivedFromIObj< T >::GetNewDerivedObject(obj);
}
};
template < typename T >
class TCachedToken : public TToken< T > {
public:
TCachedToken() {}
TCachedToken(const CToken& token) : TToken< T >(token), x8_item(nullptr) {}
2022-08-13 01:26:00 +00:00
T* operator*() { return x8_item; }
2022-11-15 20:06:18 +00:00
bool IsLoaded() {
if (x8_item != nullptr) {
return true;
}
if (HasLock() && CToken::IsLoaded()) {
2022-11-25 21:02:17 +00:00
x8_item = TToken::GetT();
2022-11-15 20:06:18 +00:00
return true;
} else {
return false;
}
}
void Unlock() {
x8_item = nullptr;
TToken<T>::Unlock();
}
2022-08-13 01:26:00 +00:00
private:
T* x8_item;
};
template < typename T >
class TLockedToken {
2022-08-13 01:26:00 +00:00
public:
TLockedToken() {}
TLockedToken(const CToken& token) : x0_token(token), x8_item(*x0_token) {}
2022-09-18 06:05:46 +00:00
TLockedToken(const TLockedToken< T >& token) : x0_token(token), x8_item(*token) {
x0_token.Lock();
}
TLockedToken& operator=(const TLockedToken< T >& token) {
x0_token = token;
x8_item = *token;
return *this;
}
operator const TToken< T >&() const { return x0_token; }
T* operator*() const { return x8_item; }
2022-10-17 02:33:08 +00:00
T* operator->() const { return x8_item; }
bool IsLoaded() const { return x0_token.IsLoaded(); }
private:
TToken< T > x0_token;
T* x8_item;
2022-08-13 01:26:00 +00:00
};
#endif // _TTOKEN