00001 /* -*- C++ -*- 00002 * $Id: cached_fn.hh 957 2005-03-07 16:01:20Z sjoyeux $ 00003 */ 00004 #ifndef UTILMM_FUNCTIONAL_CACHED_FN_HEADER 00005 # define UTILMM_FUNCTIONAL_CACHED_FN_HEADER 00006 00007 #include "utilmm/config/config.h" 00008 #include "utilmm/hash/hash_map.hh" 00009 00010 namespace utilmm { 00011 00058 template< typename Arg, typename Ret, class Hash=hash<Arg>, 00059 class Equal=std::equal_to<Arg> > 00060 class cached_fn 00061 :public std::unary_function<Arg, Ret> { 00062 private: 00063 typedef hash_map<Arg, Ret, Hash, Equal> cache_type; 00064 typedef typename arg_traits<Arg>::type arg_type; 00065 00066 cache_type the_cache; 00067 00068 protected: 00078 virtual Ret call(Arg const &arg) =0; 00079 00080 public: 00085 cached_fn() {} 00087 virtual ~cached_fn() {} 00088 00103 Ret const &operator()(arg_type arg); 00104 00111 void empty_cache() { 00112 the_cache.clear(); 00113 } 00114 00119 size_t cache_size() const { 00120 return the_cache.size(); 00121 } 00122 }; // struct utilmm::cached_fn<> 00123 00124 } // namespace utilmm 00125 00126 # define IN_UTILMM_FUNCTIONAL_CACHED_FN_HEADER 00127 #include "utilmm/functional/bits/cached_fn.tcc" 00128 # undef IN_UTILMM_FUNCTIONAL_CACHED_FN_HEADER 00129 #endif // UTILMM_FUNCTIONAL_CACHED_FN_HEADER 00130