/*
 * Copyright (C) 2007-2023 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<typename keytype, typename obtype> class CacheBase;
    template<typename keytype, typename obtype> class CacheEntry;

    template<typename keytype, typename obtype>
    class CacheObject
    {
    friend class CacheBase<keytype, obtype>;
    //friend class CacheObject<keytype, obtype>::Initializer;
    public:
        CacheObject() :
            pCache( NULL ),
            pEntry( NULL ),
            bChanged( false )
        {
        }

        virtual ~CacheObject()
        {
        }

        typedef CacheObject<keytype, obtype> MyType;
        typedef CacheBase<keytype, obtype> CacheType;
        typedef CacheEntry<keytype, obtype> 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 ),
                bLocked( true )
            {
                pObj->lock();
            }

            ~Locker()
            {
                unlock();
            }

            void unlock()
            {
                if( !bLocked )
                    return;
                pObj->unlock();
                bLocked = false;
            }

            void lock()
            {
                if( bLocked )
                    return;
                pObj->lock();
                bLocked = true;
            }

        private:
            const MyType *pObj;
            bool bLocked;
        };

    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;
        }
        
        CacheType *getCache() const
        {
            return pCache;
        }
    
    public:
        class Initializer
        {
        friend class CacheBase<keytype,obtype>;
        private:
            Initializer( CacheBase<keytype,obtype> *pCache ) :
                pCache( pCache )
            {
            }

        public:
            template<typename subtype>
            subtype *operator()( subtype *pNewObj )
            {
                pNewObj->setCache( pCache );
                return pNewObj;
            }
            
        private:
            CacheBase<keytype,obtype> *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
