drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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/noncopyable.hpp>
30#include <boost/scoped_ptr.hpp>
31
32#include <drizzled/gettext.h>
33#include <drizzled/drizzled_abort.h>
34#include <drizzled/errmsg_print.h>
35#include <drizzled/plugin/plugin.h>
36#include <drizzled/util/find_ptr.h>
37
38namespace drizzled {
39namespace module {
40
41class Registry : boost::noncopyable
42{
43public:
44 typedef std::map<std::string, Library*> LibraryMap;
45 typedef std::map<std::string, Module*> ModuleMap;
46 typedef std::vector<Module*> ModuleList;
47private:
48 LibraryMap library_registry_;
49 ModuleMap module_registry_;
50 boost::scoped_ptr<Graph> depend_graph_;
51
52 plugin::Plugin::map plugin_registry;
53
54 bool deps_built_;
55
56 Registry();
57 ~Registry();
58
59 void buildDeps();
60public:
61
62 // Delete copy constructor and assignment operator
63 Registry(const Registry&)= delete;
64 Registry& operator=(const Registry&)= delete;
65
66 // Delete move constructor and assignment operator
67 Registry(Registry&&)= delete;
68 Registry& operator=(Registry&&)= delete;
69
70 static Registry& singleton()
71 {
72 static Registry registry;
73 return registry;
74 }
75
76 Module* find(const std::string&);
77
78 void add(Module*);
79 void remove(Module*);
80
81 ModuleList getList();
82
83 const plugin::Plugin::map &getPluginsMap() const
84 {
85 return plugin_registry;
86 }
87
88 const ModuleMap &getModulesMap() const
89 {
90 return module_registry_;
91 }
92
93 Library *addLibrary(const std::string &plugin_name, bool builtin= false);
94 void removeLibrary(const std::string &plugin_name);
95 Library *findLibrary(const std::string &plugin_name) const;
96
97 void shutdownModules();
98
99 template<class T>
100 void add(T *plugin)
101 {
102 add(plugin, &plugin::default_cxx_plugin_deleter);
103 }
104
105 template<class T>
106 void add(T *plugin, plugin::deleter_fn deleter)
107 {
108 // Adopt on the first line, before any early-return / abort path, so every
109 // exit (including the duplicate-name abort) is leak-free and RAII-correct.
110 plugin::Plugin::owned_ptr owned(plugin, deleter);
111
112 bool failed= false;
113 std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
114 std::string plugin_name(boost::to_lower_copy(plugin->getName()));
115 if (find_ptr(plugin_registry, std::make_pair(plugin_type, plugin_name)))
116 {
117 std::string error_message;
118 error_message+= _("Loading plugin failed, a plugin by that name already exists.");
119 error_message+= plugin->getTypeName();
120 error_message+= ":";
121 error_message+= plugin->getName();
122 drizzled_actual_abort(__FILE__, __LINE__, __func__, error_message);
123 }
124
125 if (T::addPlugin(*plugin))
126 {
127 std::string error_message;
128 error_message+= _("Fatal error: Failed initializing: ");
129 error_message+= plugin->getTypeName();
130 error_message+= ":";
131 error_message+= plugin->getName();
132 drizzled_actual_abort(__FILE__, __LINE__, __func__, error_message);
133 }
134
135 if (failed)
136 {
137 drizzled_abort << _("Fatal error: Failed initializing: ") << plugin->getTypeName() << ":" << plugin->getName();
138 }
139 plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), std::move(owned)));
140 }
141
142 template<class T>
143 void remove(T *plugin)
144 {
145 std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
146 std::string plugin_name(boost::to_lower_copy(plugin->getName()));
147 T::removePlugin(*plugin);
148 plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
149 }
150
151
152};
153
154} /* namespace module */
155} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.