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 <iostream>
00027 
00028 #include <boost/scoped_ptr.hpp>
00029 
00030 #include <drizzled/gettext.h>
00031 #include <drizzled/unireg.h>
00032 #include <drizzled/errmsg_print.h>
00033 #include <drizzled/plugin/plugin.h>
00034 
00035 
00036 namespace drizzled
00037 {
00038 
00039 namespace module
00040 {
00041 class Module;
00042 class Library;
00043 class Graph;
00044 
00045 
00046 class Registry
00047 {
00048 public:
00049 
00050   typedef std::map<std::string, Library *> LibraryMap;
00051   typedef std::map<std::string, Module *> ModuleMap;
00052   typedef std::vector<Module *> ModuleList;
00053 private:
00054   LibraryMap library_registry_;
00055   ModuleMap module_registry_;
00056   boost::scoped_ptr<Graph> depend_graph_; 
00057   
00058   plugin::Plugin::map plugin_registry;
00059 
00060   bool deps_built_;
00061 
00062   Registry();
00063   Registry(const Registry&);
00064   Registry& operator=(const Registry&);
00065   ~Registry();
00066 
00067   void buildDeps();
00068 public:
00069 
00070   static Registry& singleton()
00071   {
00072     static Registry *registry= new Registry();
00073     return *registry;
00074   }
00075 
00076   void copy(plugin::Plugin::vector &arg);
00077 
00078   static void shutdown();
00079 
00080   Module *find(std::string name);
00081 
00082   void add(Module *module);
00083 
00084   void remove(Module *module);
00085 
00086   std::vector<Module *> getList();
00087 
00088   const plugin::Plugin::map &getPluginsMap() const
00089   {
00090     return plugin_registry;
00091   }
00092 
00093   const ModuleMap &getModulesMap() const
00094   {
00095     return module_registry_;
00096   }
00097 
00098   Library *addLibrary(const std::string &plugin_name, bool builtin= false);
00099   void removeLibrary(const std::string &plugin_name);
00100   Library *findLibrary(const std::string &plugin_name) const;
00101 
00102   void shutdownModules();
00103 
00104   template<class T>
00105   void add(T *plugin)
00106   {
00107     bool failed= false;
00108     std::string plugin_type(plugin->getTypeName());
00109     std::transform(plugin_type.begin(), plugin_type.end(),
00110                    plugin_type.begin(), ::tolower);
00111     std::string plugin_name(plugin->getName());
00112     std::transform(plugin_name.begin(), plugin_name.end(),
00113                    plugin_name.begin(), ::tolower);
00114     if (plugin_registry.find(std::make_pair(plugin_type, plugin_name)) != plugin_registry.end())
00115     {
00116       errmsg_printf(error::ERROR,
00117                     _("Loading plugin %s failed: a %s plugin by that name "
00118                       "already exists.\n"),
00119                     plugin->getTypeName().c_str(),
00120                     plugin->getName().c_str());
00121       failed= true;
00122     }
00123     if (T::addPlugin(plugin))
00124     {
00125       failed= true;
00126     }
00127 
00128     if (failed)
00129     {
00130       errmsg_printf(error::ERROR,
00131                     _("Fatal error: Failed initializing %s::%s plugin.\n"),
00132                     plugin->getTypeName().c_str(),
00133                     plugin->getName().c_str());
00134       unireg_abort(1);
00135     }
00136     plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
00137   }
00138 
00139   template<class T>
00140   void remove(T *plugin)
00141   {
00142     std::string plugin_type(plugin->getTypeName());
00143     std::transform(plugin_type.begin(), plugin_type.end(),
00144                    plugin_type.begin(), ::tolower);
00145     std::string plugin_name(plugin->getName());
00146     std::transform(plugin_name.begin(), plugin_name.end(),
00147                    plugin_name.begin(), ::tolower);
00148     T::removePlugin(plugin);
00149     plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
00150   }
00151 
00152 
00153 };
00154 
00155 } /* namespace module */
00156 } /* namespace drizzled */