csutil/cfgnotifier.h
00001 /* 00002 Copyright (C) 2010 by Jelle Hellemans 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSUTIL_CSCONFIGEVENTNOTIFIER_H__ 00020 #define __CS_CSUTIL_CSCONFIGEVENTNOTIFIER_H__ 00021 00022 #include "csextern.h" 00023 #include "csutil/ref.h" 00024 #include "csutil/scf_implementation.h" 00025 00026 #include "iutil/eventh.h" 00027 #include "csutil/cseventq.h" 00028 #include "iutil/event.h" 00029 00030 #include "iutil/cfgnotifier.h" 00031 00032 struct iObjectRegistry; 00033 struct iEventQueue; 00034 struct iEventNameRegistry; 00035 00036 namespace CS 00037 { 00038 namespace Utility 00039 { 00040 class CS_CRYSTALSPACE_EXPORT ConfigEventNotifier : 00041 public scfImplementation1<ConfigEventNotifier, iConfigListener> 00042 { 00043 public: 00044 ConfigEventNotifier(iObjectRegistry* obj_reg); 00045 ~ConfigEventNotifier(); 00046 00048 virtual void Set (const char* key, const char* value); 00050 virtual void Set (const char* key, int value); 00052 virtual void Set (const char* key, float value); 00054 virtual void Set (const char* key, bool value); 00056 virtual void Set (const char* key, iStringArray* value); 00057 00058 private: 00059 iObjectRegistry* obj_reg; 00061 csRef<iEventQueue> eventQueue; 00063 csRef<iEventNameRegistry> nameRegistry; 00064 }; 00065 00066 class CS_CRYSTALSPACE_EXPORT ConfigListenerBase : 00067 public scfImplementation1<ConfigListenerBase, iEventHandler> 00068 { 00069 public: 00070 ConfigListenerBase (iObjectRegistry* obj_reg, const char* key); 00071 ~ConfigListenerBase (); 00072 00073 private: 00074 iObjectRegistry* obj_reg; 00076 csRef<iEventQueue> eventQueue; 00078 csRef<iEventNameRegistry> nameRegistry; 00079 00080 CS_EVENTHANDLER_NAMES ("crystalspace.ConfigListener") 00081 CS_EVENTHANDLER_NIL_CONSTRAINTS 00082 }; 00083 00084 template<typename T> 00085 class ConfigListener : public scfImplementationExt0<ConfigListener<T>, ConfigListenerBase> 00086 { 00087 public: 00088 ConfigListener(iObjectRegistry* obj_reg, const char* key, T& val) 00089 : scfImplementationExt0<ConfigListener<T>, ConfigListenerBase> (this, obj_reg, key), 00090 value(val) 00091 { 00092 } 00093 00094 private: 00095 T& value; 00096 00097 virtual bool HandleEvent(iEvent& ev) 00098 { 00099 if (ev.Retrieve("value", value) != csEventErrNone) 00100 printf("E: ConfigListener::HandleEvent failed!\n"); 00101 return true; 00102 } 00103 }; 00104 00105 template<> 00106 class ConfigListener<csString> : public scfImplementationExt0<ConfigListener<csString>, ConfigListenerBase> 00107 { 00108 public: 00109 ConfigListener(iObjectRegistry* obj_reg, const char* key, csString& val) 00110 : scfImplementationExt0<ConfigListener<csString>, ConfigListenerBase> (this, obj_reg, key), 00111 value (val) 00112 { 00113 } 00114 00115 private: 00116 csString& value; 00117 00118 virtual bool HandleEvent(iEvent& ev) 00119 { 00120 const char* tmp; 00121 if (ev.Retrieve("value", tmp) != csEventErrNone) 00122 printf("E: ConfigListener<csString>::HandleEvent failed!\n"); 00123 value = tmp; 00124 return true; 00125 } 00126 }; 00127 00128 } // namespace Utility 00129 } // namespace CS 00130 00131 #endif // __CS_CSUTIL_CSCONFIGEVENTNOTIFIER_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1