Drizzled Public API Documentation

registry.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program 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
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #include <string>
00023 #include <vector>
00024 #include <map>
00025 #include <algorithm>
00026 #include <iosfwd>
00027 
00028 #include <boost/algorithm/string.hpp>
00029 #include <boost/scoped_ptr.hpp>
00030 
00031 #include <drizzled/gettext.h>
00032 #include <drizzled/unireg.h>
00033 #include <drizzled/errmsg_print.h>
00034 #include <drizzled/plugin/plugin.h>
00035 #include <drizzled/util/find_ptr.h>
00036 
00037 namespace drizzled {
00038 namespace module {
00039 
00040 class Registry : boost::noncopyable
00041 {
00042 public:
00043   typedef std::map<std::string, Library*> LibraryMap;
00044   typedef std::map<std::string, Module*> ModuleMap;
00045   typedef std::vector<Module*> ModuleList;
00046 private:
00047   LibraryMap library_registry_;
00048   ModuleMap module_registry_;
00049   boost::scoped_ptr<Graph> depend_graph_; 
00050   
00051   plugin::Plugin::map plugin_registry;
00052 
00053   bool deps_built_;
00054 
00055   Registry();
00056   ~Registry();
00057 
00058   void buildDeps();
00059 public:
00060 
00061   static Registry& singleton()
00062   {
00063     static Registry* registry= new Registry();
00064     return *registry;
00065   }
00066 
00067   static void shutdown();
00068 
00069   Module* find(const std::string&);
00070 
00071   void add(Module*);
00072   void remove(Module*);
00073 
00074   ModuleList getList();
00075 
00076   const plugin::Plugin::map &getPluginsMap() const
00077   {
00078     return plugin_registry;
00079   }
00080 
00081   const ModuleMap &getModulesMap() const
00082   {
00083     return module_registry_;
00084   }
00085 
00086   Library *addLibrary(const std::string &plugin_name, bool builtin= false);
00087   void removeLibrary(const std::string &plugin_name);
00088   Library *findLibrary(const std::string &plugin_name) const;
00089 
00090   void shutdownModules();
00091 
00092   template<class T>
00093   void add(T *plugin)
00094   {
00095     bool failed= false;
00096     std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
00097     std::string plugin_name(boost::to_lower_copy(plugin->getName()));
00098     if (find_ptr(plugin_registry, std::make_pair(plugin_type, plugin_name)))
00099     {
00100       std::string error_message;
00101       error_message+=  _("Loading plugin failed, a plugin by that name already exists.");
00102       error_message+= plugin->getTypeName();
00103       error_message+= ":";
00104       error_message+= plugin->getName();
00105       unireg_actual_abort(__FILE__, __LINE__, __func__, error_message);
00106     }
00107 
00108     if (T::addPlugin(plugin))
00109     {
00110       std::string error_message;
00111       error_message+= _("Fatal error: Failed initializing: ");
00112       error_message+= plugin->getTypeName();
00113       error_message+= ":";
00114       error_message+= plugin->getName();
00115       unireg_actual_abort(__FILE__, __LINE__, __func__, error_message);
00116     }
00117 
00118     if (failed)
00119     {
00120       unireg_abort << _("Fatal error: Failed initializing: ") << plugin->getTypeName() << ":" << plugin->getName();
00121     }
00122     plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
00123   }
00124 
00125   template<class T>
00126   void remove(T *plugin)
00127   {
00128     std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
00129     std::string plugin_name(boost::to_lower_copy(plugin->getName()));
00130     T::removePlugin(plugin);
00131     plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
00132   }
00133 
00134 
00135 };
00136 
00137 } /* namespace module */
00138 } /* namespace drizzled */