/* * Copyright (C) 2007-2014 Xagasoft, All rights reserved. * * This file is part of the libbu++ library and is released under the * terms of the license contained in the file LICENSE. */ #ifndef BU_CACHE_OBJECT_H #define BU_CACHE_OBJECT_H #ifndef NULL #define NULL 0 #endif namespace Bu { template class CacheBase; template class CacheEntry; template class CacheObject { friend class CacheBase; //friend class CacheObject::Initializer; public: CacheObject() : pCache( NULL ), pEntry( NULL ), bChanged( false ) { } virtual ~CacheObject() { } typedef CacheObject MyType; typedef CacheBase CacheType; typedef CacheEntry EntryType; virtual keytype getKey() const=0; virtual int getPersistenceScore() const { return 0; } void lock() const { if( pEntry ) pEntry->lock(); } void unlock() const { if( pEntry ) pEntry->unlock(); } int getRefCount() const { if( pEntry ) return pEntry->getRefCount(); return -1; } bool hasChanged() const { return bChanged; } class Locker { public: Locker( const MyType *pObj ) : pObj( pObj ) { pObj->lock(); } ~Locker() { pObj->unlock(); } private: const MyType *pObj; }; protected: void changed( bool bChanged=true ) { if( this->bChanged == false && bChanged == true && pCache && pEntry && !pEntry->isDeleted() ) pCache->objectChanged( getKey() ); this->bChanged = bChanged; } void changed( bool bChanged=true ) const // not really! { if( bChanged == true ) return; this->bChanged = bChanged; } CacheType *getCache() { return pCache; } public: class Initializer { friend class CacheBase; private: Initializer( CacheBase *pCache ) : pCache( pCache ) { } public: template subtype *operator()( subtype *pNewObj ) { pNewObj->setCache( pCache ); return pNewObj; } private: CacheBase *pCache; }; private: void setCache( CacheType *pCache ) { this->pCache = pCache; } void setCacheEntry( EntryType *pEntry ) { this->pEntry = pEntry; } private: CacheType *pCache; EntryType *pEntry; mutable bool bChanged; }; } #endif