~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.h

  • Committer: Brian Aker
  • Date: 2008-07-03 00:14:39 UTC
  • Revision ID: brian@tangent.org-20080703001439-pit0mcl0wk8elxlq
Cleanup of sql-common and mysqldump

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
 
#ifndef DRIZZLED_MODULE_REGISTRY_H
21
 
#define DRIZZLED_MODULE_REGISTRY_H
22
 
 
23
 
#include <string>
24
 
#include <vector>
25
 
#include <map>
26
 
#include <algorithm>
27
 
 
28
 
#include "drizzled/gettext.h"
29
 
#include "drizzled/unireg.h"
30
 
#include "drizzled/errmsg_print.h"
31
 
 
32
 
namespace drizzled
33
 
{
34
 
namespace plugin
35
 
{
36
 
class Plugin;
37
 
}
38
 
 
39
 
namespace module
40
 
{
41
 
class Module;
42
 
class Library;
43
 
 
44
 
class Registry
45
 
{
46
 
private:
47
 
  std::map<std::string, Library *> library_map;
48
 
  std::map<std::string, Module *> module_map;
49
 
  std::map<std::string, plugin::Plugin *> plugin_registry;
50
 
 
51
 
  Registry()
52
 
   : module_map(),
53
 
     plugin_registry()
54
 
  { }
55
 
 
56
 
  Registry(const Registry&);
57
 
  Registry& operator=(const Registry&);
58
 
  ~Registry();
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(std::string name);
70
 
 
71
 
  void add(Module *module);
72
 
 
73
 
  void remove(Module *module);
74
 
 
75
 
  std::vector<Module *> getList(bool active);
76
 
 
77
 
  const std::map<std::string, plugin::Plugin *> &getPluginsMap() const
78
 
  {
79
 
    return plugin_registry;
80
 
  }
81
 
 
82
 
  const std::map<std::string, Module *> &getModulesMap() const
83
 
  {
84
 
    return module_map;
85
 
  }
86
 
 
87
 
  Library *addLibrary(const std::string &plugin_name, bool builtin= false);
88
 
  void removeLibrary(const std::string &plugin_name);
89
 
  Library *findLibrary(const std::string &plugin_name) const;
90
 
 
91
 
  void shutdownModules();
92
 
 
93
 
  template<class T>
94
 
  void add(T *plugin)
95
 
  {
96
 
    bool failed= false;
97
 
    std::string plugin_name(plugin->getName());
98
 
    std::transform(plugin_name.begin(), plugin_name.end(),
99
 
                   plugin_name.begin(), ::tolower);
100
 
    if (plugin_registry.find(plugin_name) != plugin_registry.end())
101
 
    {
102
 
      errmsg_printf(ERRMSG_LVL_ERROR,
103
 
                    _("Loading plugin %s failed: a plugin by that name already "
104
 
                      "exists.\n"), plugin->getName().c_str());
105
 
      failed= true;
106
 
    }
107
 
    if (T::addPlugin(plugin))
108
 
      failed= true;
109
 
    if (failed)
110
 
    {
111
 
      errmsg_printf(ERRMSG_LVL_ERROR,
112
 
                    _("Fatal error: Failed initializing %s plugin.\n"),
113
 
                    plugin->getName().c_str());
114
 
      unireg_abort(1);
115
 
    }
116
 
    plugin_registry.insert(std::pair<std::string, plugin::Plugin *>(plugin_name, plugin));
117
 
  }
118
 
 
119
 
  template<class T>
120
 
  void remove(T *plugin)
121
 
  {
122
 
    std::string plugin_name(plugin->getName());
123
 
    std::transform(plugin_name.begin(), plugin_name.end(),
124
 
                   plugin_name.begin(), ::tolower);
125
 
    T::removePlugin(plugin);
126
 
    plugin_registry.erase(plugin_name);
127
 
  }
128
 
 
129
 
 
130
 
};
131
 
 
132
 
} /* namespace module */
133
 
} /* namespace drizzled */
134
 
#endif /* DRIZZLED_MODULE_REGISTRY_H */