00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #ifndef CEYLAN_RESOURCE_MANAGER_H_ 00028 #define CEYLAN_RESOURCE_MANAGER_H_ 00029 00030 00031 #include "CeylanException.h" // for inheritance 00032 00033 #include "CeylanUtils.h" // for Ceylan::emergencyShutdown 00034 #include "CeylanResource.h" // for Ceylan::Resource 00035 #include "CeylanOperators.h" // for string operations 00036 00037 00038 #include <string> 00039 00040 00041 00042 namespace Ceylan 00043 { 00044 00045 00046 00052 class CEYLAN_DLL ResourceManagerException : public Ceylan::Exception 00053 { 00054 00055 public: 00056 00057 explicit ResourceManagerException( const std::string & reason ) ; 00058 00059 virtual ~ResourceManagerException() throw() ; 00060 00061 } ; 00062 00063 00064 00133 template <typename Key> 00134 class ResourceManager : public Ceylan::TextDisplayable 00135 { 00136 00137 00138 public: 00139 00140 00141 00146 explicit ResourceManager() ; 00147 00148 00149 00157 virtual ~ResourceManager() throw() ; 00158 00159 00160 00169 virtual bool isKeyAlreadyAssociated( const Key & key ) const = 0 ; 00170 00171 00172 00190 virtual const Resource * get( const Key & key ) = 0 ; 00191 00192 00193 00202 virtual void flush() = 0 ; 00203 00204 00205 00216 virtual const std::string toString( 00217 Ceylan::VerbosityLevels level = Ceylan::high ) const ; 00218 00219 00220 00221 protected: 00222 00223 00229 Ceylan::Uint32 _cacheHits ; 00230 00231 00237 Ceylan::Uint32 _cacheMisses ; 00238 00239 00240 00241 00242 private: 00243 00244 00252 ResourceManager( const ResourceManager & source ) ; 00253 00254 00262 ResourceManager & operator = ( const ResourceManager & source ) ; 00263 00264 00265 } ; 00266 00267 00268 00270 00271 00272 template <typename Key> 00273 ResourceManager<Key>::ResourceManager() : 00274 _cacheHits( 0 ), 00275 _cacheMisses( 0 ) 00276 { 00277 00278 } 00279 00280 00281 template <typename Key> 00282 ResourceManager<Key>::~ResourceManager() throw() 00283 { 00284 00285 /* 00286 * Cannot be called at this level since 'flush()' is 00287 * implementation-dependent. 00288 * 00289 */ 00290 00291 } 00292 00293 00294 template <typename Key> 00295 const std::string ResourceManager<Key>::toString( VerbosityLevels level ) 00296 const 00297 { 00298 00299 std::string res = "Abstract Resource manager" ; 00300 00301 if ( level == Ceylan::low ) 00302 return res ; 00303 00304 Ceylan::Uint32 total = _cacheHits + _cacheMisses ; 00305 00306 return res + ". The average cache success is " 00307 + Ceylan::toNumericalString( static_cast<Ceylan::Uint8>( 00308 ( 100.0f * _cacheHits ) / total ) ) 00309 + "% (" + Ceylan::toString( _cacheHits ) + " cache hit(s) for " 00310 + Ceylan::toString( _cacheMisses ) + " cache miss(es))" ; 00311 00312 } 00313 00314 00315 } 00316 00317 00318 00319 #endif // CEYLAN_RESOURCE_MANAGER_H_ 00320