~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/registry.h

  • Committer: Brian Aker
  • Date: 2010-02-11 22:43:58 UTC
  • Revision ID: brian@gaz-20100211224358-y0gdvnat2ahg4c1e
Disabling support for memcached plugins until we can test for version of
memcached.

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_PLUGIN_REGISTRY_H
 
21
#define DRIZZLED_PLUGIN_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 Module;
 
37
class Plugin;
 
38
class Library;
 
39
 
 
40
class Registry
 
41
{
 
42
private:
 
43
  std::map<std::string, Library *> library_map;
 
44
  std::map<std::string, Module *> module_map;
 
45
  std::map<std::string, const Plugin *> plugin_registry;
 
46
 
 
47
  Module *current_module;
 
48
 
 
49
  Registry()
 
50
   : module_map(),
 
51
     plugin_registry(),
 
52
     current_module(NULL)
 
53
  { }
 
54
 
 
55
  Registry(const Registry&);
 
56
  Registry& operator=(const Registry&);
 
57
  ~Registry();
 
58
public:
 
59
 
 
60
  static plugin::Registry& singleton()
 
61
  {
 
62
    static plugin::Registry *registry= new plugin::Registry();
 
63
    return *registry;
 
64
  }
 
65
 
 
66
  static void shutdown();
 
67
 
 
68
  Module *find(std::string name);
 
69
 
 
70
  void add(Module *module);
 
71
 
 
72
  void setCurrentModule(Module *module)
 
73
  {
 
74
    current_module= module;
 
75
  }
 
76
 
 
77
  void clearCurrentModule()
 
78
  {
 
79
    current_module= NULL;
 
80
  }
 
81
 
 
82
  std::vector<Module *> getList(bool active);
 
83
 
 
84
  const std::map<std::string, const Plugin *> &getPluginsMap() const
 
85
  {
 
86
    return plugin_registry;
 
87
  }
 
88
 
 
89
  const std::map<std::string, Module *> &getModulesMap() const
 
90
  {
 
91
    return module_map;
 
92
  }
 
93
 
 
94
  Library *addLibrary(const std::string &plugin_name);
 
95
  void removeLibrary(const std::string &plugin_name);
 
96
  Library *findLibrary(const std::string &plugin_name) const;
 
97
 
 
98
  template<class T>
 
99
  void add(T *plugin)
 
100
  {
 
101
    plugin->setModule(current_module);
 
102
    bool failed= false;
 
103
    std::string plugin_name(plugin->getName());
 
104
    std::transform(plugin_name.begin(), plugin_name.end(),
 
105
                   plugin_name.begin(), ::tolower);
 
106
    if (plugin_registry.find(plugin_name) != plugin_registry.end())
 
107
    {
 
108
      errmsg_printf(ERRMSG_LVL_ERROR,
 
109
                    _("Loading plugin %s failed: a plugin by that name already "
 
110
                      "exists.\n"), plugin->getName().c_str());
 
111
      failed= true;
 
112
    }
 
113
    if (T::addPlugin(plugin))
 
114
      failed= true;
 
115
    if (failed)
 
116
    {
 
117
      errmsg_printf(ERRMSG_LVL_ERROR,
 
118
                    _("Fatal error: Failed initializing %s plugin.\n"),
 
119
                    plugin->getName().c_str());
 
120
      unireg_abort(1);
 
121
    }
 
122
    plugin_registry.insert(std::pair<std::string, const Plugin *>(plugin_name, plugin));
 
123
  }
 
124
 
 
125
  template<class T>
 
126
  void remove(T *plugin)
 
127
  {
 
128
    std::string plugin_name(plugin->getName());
 
129
    std::transform(plugin_name.begin(), plugin_name.end(),
 
130
                   plugin_name.begin(), ::tolower);
 
131
    T::removePlugin(plugin);
 
132
    plugin_registry.erase(plugin_name);
 
133
  }
 
134
 
 
135
 
 
136
};
 
137
 
 
138
} /* end namespace plugin */
 
139
} /* end namespace drizzled */
 
140
#endif /* DRIZZLED_PLUGIN_REGISTRY_H */