Templated Cache Class

Cache.h

#ifndef CACHE_H
#define CACHE_H
 
template < class T >
class Cache
{
    public:
        Cache();
        Cache( T theCached );
 
        T& Get();
        const T& Get() const;
        operator T&();
        operator const T&() const;
 
        void Set( const T& theCached );
        Cache< T >& operator =( const T& theCached );
 
        bool IsValid() const;
        operator bool() const;
        void Invalidate();
 
    protected:
        T cached;
        bool isValid;
};
 
#include "Cache.inl"
 
#endif // CACHE_H

Cache.inl

template < class T >
Cache< T >::Cache()
   : cached(),
     isValid( false )
{
}
 
template < class T >
Cache< T >::Cache( T theCached )
   : cached( theCached ),
     isValid( true )
{
}
 
template < class T >
T& Cache< T >::Get()
{
    return cached;
}
 
template < class T >
const T& Cache< T >::Get() const
{
    return cached;
}
 
template < class T >
Cache< T >::operator T&()
{
    return cached;
}
 
template < class T >
Cache< T >::operator const T&() const
{
    return cached;
}
 
template < class T >
void Cache< T >::Set( const T& theCached )
{
    cached = theCached;
    isValid = true;
}
 
template < class T >
Cache< T >& Cache< T >::operator =( const T& theCached )
{
    cached = theCached;
    isValid = true;
}
 
template < class T >
bool Cache< T >::IsValid() const
{
    return isValid;
}
 
template < class T >
Cache< T >::operator bool() const
{
    return isValid;
}
 
template < class T >
void Cache< T >::Invalidate()
{
    isValid = false;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License