PluginInterface.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 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@gmail.com */
00025 
00030 #ifndef _GG_PluginInterface_h_
00031 #define _GG_PluginInterface_h_
00032 
00033 #include <GG/GUI.h>
00034 
00035 #if defined(__APPLE__) && defined(__MACH__)
00036 # include "../libltdl/ltdl.h"
00037 #else
00038 # include <GG/ltdl.h>
00039 #endif
00040 
00041 #include <string>
00042 
00043 
00044 namespace boost { namespace archive {
00045     class xml_oarchive;
00046     class xml_iarchive;
00047 } }
00048 
00049 namespace GG {
00050 
00067 class GG_API PluginInterface
00068 {
00069 private:
00070     struct ConvertibleToBoolDummy {int _;};
00071 
00072 public:
00074     typedef const char*                     (*PluginNameFn)();
00077     typedef const char*                     (*DefaultFontNameFn)();
00079     typedef unsigned int                    (*DefaultFontSizeFn)();
00081     typedef boost::shared_ptr<StyleFactory> (*GetStyleFactoryFn)();
00082 
00084     typedef GUI::SaveWndFn                  SaveWndFn;
00086     typedef GUI::LoadWndFn                  LoadWndFn;
00087  
00089     PluginInterface(); 
00090 
00094     PluginInterface(const std::string& lib_name);
00095 
00096     ~PluginInterface(); 
00097 
00098  
00100 
00105     operator int ConvertibleToBoolDummy::* () const;
00107  
00109 
00111     bool Load(const std::string& lib_name);
00112 
00113     PluginNameFn                PluginName;             
00114     DefaultFontNameFn           DefaultFontName;        
00115     DefaultFontSizeFn           DefaultFontSize;        
00116     GetStyleFactoryFn           GetStyleFactory;        
00117     SaveWndFn                   SaveWnd;                
00118     LoadWndFn                   LoadWnd;                
00119 
00124     template <class T>
00125     void LoadWndT(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar);
00127 
00128 private:
00129     lt_dlhandle m_handle;
00130     boost::archive::xml_oarchive* m_out_archive;
00131     boost::archive::xml_iarchive* m_in_archive;
00132 };
00133 
00140 class GG_API PluginManager
00141 {
00142 public: 
00144 
00147     boost::shared_ptr<PluginInterface> GetPlugin(const std::string& name);
00148 
00152     void FreePlugin(const std::string& name);
00154 
00158     static void InitDynamicLoader();
00159 
00161     static void AddSearchDirectory(const std::string& dir);
00162 
00166     static void CleanupDynamicLoader();
00167 
00168 private:
00169     PluginManager();
00170 
00171     std::map<std::string, boost::shared_ptr<PluginInterface> > m_plugins;
00172 
00173     static bool s_lt_dl_initialized;
00174 
00175     friend GG_API PluginManager& GetPluginManager();
00176 };
00177 
00179 GG_API PluginManager& GetPluginManager();
00180 
00181 // template implementations
00182 template <class T>
00183 void PluginInterface::LoadWndT(T*& wnd, const std::string& name, boost::archive::xml_iarchive& ar)
00184 {
00185     Wnd* wnd_as_base = wnd;
00186     LoadWnd(wnd_as_base, name, ar);
00187     wnd = dynamic_cast<T*>(wnd_as_base);
00188     assert(wnd);
00189 }
00190 
00191 } // namespace GG
00192 
00193 #endif // _GG_PluginInterface_h_

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9