00001 #ifndef GSGL_DATA_GLOBAL_H 00002 #define GSGL_DATA_GLOBAL_H 00003 00004 // 00005 // $Id: global.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/exception.hpp" 00039 #include "data/singleton.hpp" 00040 #include "data/list.hpp" 00041 00042 namespace gsgl 00043 { 00044 00045 namespace data 00046 { 00047 00048 /// Base class for global resource registers. They are singleton objects which delete themselves when their last registered resource unregisters itself. 00049 /// The resource class must implement a \c cleanup() function. 00050 template <typename R, typename L = gsgl::data::list<R *> > 00051 class global_register 00052 : public singleton<global_register<R,L> > 00053 { 00054 protected: 00055 L registered_resources; 00056 00057 global_register(); 00058 virtual ~global_register(); 00059 00060 /// Reimplemented in order to auto-create. 00061 static global_register<R,L> *global_instance(); 00062 00063 public: 00064 void register_resource(R *); 00065 void unregister_resource(R *); 00066 }; // class global_register 00067 00068 00069 template <typename R, typename L> 00070 global_register<R,L>::global_register() 00071 : singleton<global_register<R,L> >() 00072 { 00073 } // global_register<R,L>::global_register() 00074 00075 00076 template <typename R, typename L> 00077 global_register<R,L>::~global_register() 00078 { 00079 } // global_register<R,L>::~global_register() 00080 00081 00082 template <typename R, typename L> 00083 void global_register<R,L>::register_resource(R *r) 00084 { 00085 global_register_resource_aux<R>(registered_resources, r); 00086 } // global_register<R,L>::register_resource() 00087 00088 00089 /// Auxiliary function to get around the inability to partially specialize template member functions. 00090 template <typename R> 00091 void global_register_resource_aux(gsgl::data::list<R *> & l, R *r) 00092 { 00093 assert(r); 00094 l.append(r); 00095 } // global_register_resource_aux() 00096 00097 00098 template <typename R, typename L> 00099 void global_register<R,L>::unregister_resource(R *r) 00100 { 00101 global_unregister_resource_aux<R>(registered_resources, r); 00102 00103 if (registered_resources.is_empty()) 00104 delete this; 00105 } // global_register<R,L>::unregister_resource() 00106 00107 00108 template <typename R> 00109 void global_unregister_resource_aux(gsgl::data::list<R *> & l, R *r) 00110 { 00111 assert(r); 00112 00113 gsgl::data::list<R *>::iterator i = l.find_value(r); 00114 00115 if (i.is_valid()) 00116 l.remove(i); 00117 else 00118 throw internal_exception(__FILE__, __LINE__, L"Attempted to unregister an invalid resource."); 00119 } // global_unregister_resource_aux() 00120 00121 00122 } // namespace data 00123 00124 } // namespace gsgl 00125 00126 00127 #endif