Drizzled Public API Documentation

registry.h
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #pragma once
21 
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include <algorithm>
26 #include <iosfwd>
27 
28 #include <boost/algorithm/string.hpp>
29 #include <boost/scoped_ptr.hpp>
30 
31 #include <drizzled/gettext.h>
32 #include <drizzled/unireg.h>
33 #include <drizzled/errmsg_print.h>
34 #include <drizzled/plugin/plugin.h>
35 #include <drizzled/util/find_ptr.h>
36 
37 namespace drizzled {
38 namespace module {
39 
40 class Registry : boost::noncopyable
41 {
42 public:
43  typedef std::map<std::string, Library*> LibraryMap;
44  typedef std::map<std::string, Module*> ModuleMap;
45  typedef std::vector<Module*> ModuleList;
46 private:
47  LibraryMap library_registry_;
48  ModuleMap module_registry_;
49  boost::scoped_ptr<Graph> depend_graph_;
50 
51  plugin::Plugin::map plugin_registry;
52 
53  bool deps_built_;
54 
55  Registry();
56  ~Registry();
57 
58  void buildDeps();
59 public:
60 
61  static Registry& singleton()
62  {
63  static Registry* registry= new Registry();
64  return *registry;
65  }
66 
67  static void shutdown();
68 
69  Module* find(const std::string&);
70 
71  void add(Module*);
72  void remove(Module*);
73 
74  ModuleList getList();
75 
76  const plugin::Plugin::map &getPluginsMap() const
77  {
78  return plugin_registry;
79  }
80 
81  const ModuleMap &getModulesMap() const
82  {
83  return module_registry_;
84  }
85 
86  Library *addLibrary(const std::string &plugin_name, bool builtin= false);
87  void removeLibrary(const std::string &plugin_name);
88  Library *findLibrary(const std::string &plugin_name) const;
89 
90  void shutdownModules();
91 
92  template<class T>
93  void add(T *plugin)
94  {
95  bool failed= false;
96  std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
97  std::string plugin_name(boost::to_lower_copy(plugin->getName()));
98  if (find_ptr(plugin_registry, std::make_pair(plugin_type, plugin_name)))
99  {
100  std::string error_message;
101  error_message+= _("Loading plugin failed, a plugin by that name already exists.");
102  error_message+= plugin->getTypeName();
103  error_message+= ":";
104  error_message+= plugin->getName();
105  unireg_actual_abort(__FILE__, __LINE__, __func__, error_message);
106  }
107 
108  if (T::addPlugin(plugin))
109  {
110  std::string error_message;
111  error_message+= _("Fatal error: Failed initializing: ");
112  error_message+= plugin->getTypeName();
113  error_message+= ":";
114  error_message+= plugin->getName();
115  unireg_actual_abort(__FILE__, __LINE__, __func__, error_message);
116  }
117 
118  if (failed)
119  {
120  unireg_abort << _("Fatal error: Failed initializing: ") << plugin->getTypeName() << ":" << plugin->getName();
121  }
122  plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
123  }
124 
125  template<class T>
126  void remove(T *plugin)
127  {
128  std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
129  std::string plugin_name(boost::to_lower_copy(plugin->getName()));
130  T::removePlugin(plugin);
131  plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
132  }
133 
134 
135 };
136 
137 } /* namespace module */
138 } /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
Definition: engine.cc:41