00001 #ifndef GSGL_DATA_CACHE_H 00002 #define GSGL_DATA_CACHE_H 00003 00004 // 00005 // $Id: cache.hpp 2 2008-03-01 20:58:50Z kulibali $ 00006 // 00007 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are 00011 // met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, 00014 // this list of conditions and the following disclaimer. 00015 // 00016 // * Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // * Neither the name of the The Periapsis Project nor the names of its 00021 // contributors may be used to endorse or promote products derived from 00022 // this software without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // 00036 00037 #include "data/data.hpp" 00038 #include "data/log.hpp" 00039 #include "data/singleton.hpp" 00040 #include "data/dictionary.hpp" 00041 #include "data/string.hpp" 00042 00043 namespace gsgl 00044 { 00045 00046 namespace data 00047 { 00048 00049 /// Instances of this class's subclasses will automatically register themselves with the global cache, if the cache() constructer is included in the initializer list of the subclass. 00050 template <typename T> 00051 class cache 00052 : public singleton< cache<T> > 00053 { 00054 gsgl::string name; 00055 dictionary<T *, gsgl::string> items; 00056 00057 public: 00058 cache(const gsgl::string & name); 00059 virtual ~cache(); 00060 00061 bool contains_index(const gsgl::string & key) const { return items.contains_index(key); } 00062 T * & operator[] (const gsgl::string & key) { return items[key]; } 00063 }; // class cache 00064 00065 00066 template <typename T> 00067 cache<T>::cache(const gsgl::string & name) 00068 : singleton(), name(name) 00069 { 00070 } // cache<T>::cache() 00071 00072 00073 template <typename T> 00074 cache<T>::~cache() 00075 { 00076 for (dictionary<T *, gsgl::string>::iterator i = items.iter(); i.is_valid(); ++i) 00077 { 00078 T *item = *i; 00079 00080 if (item) 00081 { 00082 if (item->get_ref_count() > 1) 00083 gsgl::log(string(L"cache: MEMORY LEAK: ") + name + L": dangling reference for " + i.get_index()); 00084 item->detach(); 00085 } 00086 else 00087 { 00088 gsgl::log(string(L"cache: ") + name + L": cache error: null pointer for " + i.get_index()); 00089 } 00090 } 00091 00092 items.clear(); 00093 } // cache<T>::~cache() 00094 00095 00096 } // namespace data 00097 00098 } // namespace gsgl 00099 00100 #endif