Advertisement
spacechase0

Cache Template

Aug 21st, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. // util/Cache.h
  2. #ifndef UTIL_CACHE_H
  3. #define UTIL_CACHE_H
  4.  
  5. namespace util
  6. {
  7.     template < class T >
  8.     class Cache
  9.     {
  10.         public:
  11.             Cache();
  12.             Cache( T theCached );
  13.            
  14.             T& Get();
  15.             const T& Get() const;
  16.             operator T&();
  17.             operator const T&() const;
  18.            
  19.             void Set( const T& theCached );
  20.             Cache< T >& operator =( const T& theCached );
  21.            
  22.             bool IsValid();
  23.             operator bool() const;
  24.             void Invalidate();
  25.        
  26.         protected:
  27.             T cached;
  28.             bool isValid;
  29.     };
  30.    
  31.     #include "util/Cache.inl"
  32. }
  33.  
  34. #endif // UTIL_CACHE_H
  35.  
  36.  
  37.  
  38. // util/Cache.inl
  39. template < class T >
  40. Cache< T >::Cache()
  41.    : cached(),
  42.      isValid( false )
  43. {
  44. }
  45.  
  46. template < class T >
  47. Cache< T >::Cache( T theCached )
  48.    : cached( theCached ),
  49.      isValid( true )
  50. {
  51. }
  52.  
  53. template < class T >
  54. T& Cache< T >::Get()
  55. {
  56.     return cached;
  57. }
  58.  
  59. template < class T >
  60. const T& Cache< T >::Get() const
  61. {
  62.     return cached;
  63. }
  64.  
  65. template < class T >
  66. Cache< T >::operator T&()
  67. {
  68.     return cached;
  69. }
  70.  
  71. template < class T >
  72. Cache< T >::operator const T&() const
  73. {
  74.     return cached;
  75. }
  76.  
  77. template < class T >
  78. void Cache< T >::Set( const T& theCached )
  79. {
  80.     cached = theCached;
  81.     isValid = true;
  82. }
  83.  
  84. template < class T >
  85. Cache< T >& Cache< T >::operator =( const T& theCached )
  86. {
  87.     cached = theCached;
  88.     isValid = true;
  89. }
  90.  
  91. template < class T >
  92. bool Cache< T >::IsValid()
  93. {
  94.     return isValid;
  95. }
  96.  
  97. template < class T >
  98. Cache< T >::operator bool() const
  99. {
  100.     return isValid;
  101. }
  102.  
  103. template < class T >
  104. void Cache< T >::Invalidate()
  105. {
  106.     isValid = false;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement