prime/include/Kyoto/TToken.hpp

87 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(); }
2023-01-14 07:55:03 +00:00
static rstl::auto_ptr< TObjOwnerDerivedFromIObj< T > >
2022-09-18 06:05:46 +00:00
GetIObjObjectFor(const rstl::auto_ptr< T >& obj) {
2022-08-13 01:26:00 +00:00
return TObjOwnerDerivedFromIObj< T >::GetNewDerivedObject(obj);
}
2023-06-20 02:20:15 +00:00
TToken< T > NonConstCopy() const { return *const_cast< TToken< T >* >(this); }
2022-08-13 01:26:00 +00:00
};
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;
}
2022-12-22 04:26:49 +00:00
if (CToken::HasLock() && CToken::IsLoaded()) {
x8_item = TToken< T >::GetT();
2022-11-15 20:06:18 +00:00
return true;
} else {
return false;
}
}
void Unlock() {
x8_item = nullptr;
2023-06-20 02:20:15 +00:00
TToken< T >::Unlock();
2022-11-15 20:06:18 +00:00
}
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