PluginInterface.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@hotmail.com */
00025 
00029 #ifndef _GG_PluginInterface_h_
00030 #define _GG_PluginInterface_h_
00031 
00032 #include <GG/GUI.h>
00033 
00034 #if defined(__APPLE__) && defined(__MACH__)
00035 # include "../libltdl/ltdl.h"
00036 #else
00037 # include <GG/ltdl.h>
00038 #endif
00039 
00040 #include <string>
00041 
00042 
00043 namespace boost { namespace archive {
00044     class xml_oarchive;
00045     class xml_iarchive;
00046 } }
00047 
00048 namespace GG {
00049 
00059 class GG_API PluginInterface
00060 {
00061 private:
00062     struct ConvertibleToBoolDummy {int _;};
00063 
00064 public:
00066     typedef const char*                     (*PluginNameFn)();
00068     typedef const char*                     (*DefaultFontNameFn)();
00070     typedef int                             (*DefaultFontSizeFn)();
00072     typedef boost::shared_ptr<StyleFactory> (*GetStyleFactoryFn)();
00073 
00075     typedef GUI::SaveWndFn                  SaveWndFn;
00077     typedef GUI::LoadWndFn                  LoadWndFn;
00078  
00080     PluginInterface(); 
00081 
00084     PluginInterface(const std::string& lib_name);
00085 
00086     ~PluginInterface(); 
00087 
00088  
00090 
00093     operator int ConvertibleToBoolDummy::* () const;
00095  
00097 
00098     bool Load(const std::string& lib_name);
00099 
00100     PluginNameFn                PluginName;             
00101     DefaultFontNameFn           DefaultFontName;        
00102     DefaultFontSizeFn           DefaultFontSize;        
00103     GetStyleFactoryFn           GetStyleFactory;        
00104     SaveWndFn                   SaveWnd;                
00105     LoadWndFn                   LoadWnd;                
00106 
00109     template <class T>
00110     void LoadWndT(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00112 
00113 private:
00114     lt_dlhandle m_handle;
00115     boost::archive::xml_oarchive* m_out_archive;
00116     boost::archive::xml_iarchive* m_in_archive;
00117 };
00118 
00122 class GG_API PluginManager
00123 {
00124 public: 
00126 
00128     boost::shared_ptr<PluginInterface> GetPlugin(const std::string& name);
00129 
00132     void FreePlugin(const std::string& name);
00134 
00137     static void InitDynamicLoader();
00138 
00140     static void AddSearchDirectory(const std::string& dir);
00141 
00144     static void CleanupDynamicLoader();
00145 
00146 private:
00147     PluginManager();
00148 
00149     std::map<std::string, boost::shared_ptr<PluginInterface> > m_plugins;
00150 
00151     static bool s_lt_dl_initialized;
00152 
00153     friend GG_API PluginManager& GetPluginManager();
00154 };
00155 
00157 GG_API PluginManager& GetPluginManager();
00158 
00159 // template implementations
00160 template <class T>
00161 void PluginInterface::LoadWndT(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar)
00162 {
00163     Wnd* wnd_as_base = wnd;
00164     LoadWnd(wnd_as_base, name, ar);
00165     wnd = dynamic_cast<T*>(wnd_as_base);
00166     assert(wnd);
00167 }
00168 
00169 } // namespace GG
00170 
00171 #endif // _GG_PluginInterface_h_

Generated on Wed Mar 26 14:35:42 2008 for GG by  doxygen 1.5.2