home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / cached.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.1 KB  |  95 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #pragma once
  21.  
  22. template <class T>
  23. class cached
  24.         /*
  25.             ...
  26.         */
  27.     {
  28.         public:
  29.  
  30.             cached()
  31.                     : _cached_value_is_valid(false)
  32.                 {
  33.                     // nothing else to do
  34.                 }
  35.  
  36.             explicit
  37.             cached( const T& initial_value_to_cache )
  38.                     : _cached_value                    (initial_value_to_cache),
  39.                         _cached_value_is_valid(true)
  40.                 {
  41.                     // nothing else to do
  42.                 }
  43.  
  44.  
  45.  
  46.             operator const T&() const
  47.                     // ...(not explicit) so you can treat a cached<T> as a T in _most_ cases.
  48.                 {
  49.                     return _cached_value;
  50.                 }
  51.  
  52.             cached<T>&
  53.             operator=( const T& value )
  54.                     // ...assigning a value into a cache means 'cache this value'
  55.                 {
  56.                     set_cached_value(value);
  57.                     return *this;
  58.                 }
  59.  
  60.             cached<T>&
  61.             operator=( const cached<T>& other )
  62.                     // ...otherwise assignment might make a valid copy of an invalid cache.
  63.                 {
  64.                     if ( (_cached_value_is_valid = other._cached_value_is_valid) == true )
  65.                         _cached_value = other._cached_value;
  66.                     return *this;
  67.                 }                
  68.  
  69.  
  70.             bool
  71.             is_valid() const
  72.                 {
  73.                     return _cached_value_is_valid;
  74.                 }
  75.  
  76.             void
  77.             invalidate()
  78.                 {
  79.                     _cached_value_is_valid = false;
  80.                 }
  81.  
  82.         protected:
  83.  
  84.             void
  85.             set_cached_value( const T& value )
  86.                 {
  87.                     _cached_value                        = value;
  88.                     _cached_value_is_valid    = true;
  89.                 }
  90.  
  91.         private:
  92.             T            _cached_value;
  93.             bool    _cached_value_is_valid;
  94.     };
  95.