~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.h

Merged vcol stuff.

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, 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
 
#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
 
#include <iostream>
28
 
 
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
 
 
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
 
namespace module
41
 
{
42
 
class Module;
43
 
class Library;
44
 
class Graph;
45
 
 
46
 
 
47
 
class Registry
48
 
{
49
 
public:
50
 
 
51
 
  typedef std::map<std::string, Library *> LibraryMap;
52
 
  typedef std::map<std::string, Module *> ModuleMap;
53
 
  typedef std::vector<Module *> ModuleList;
54
 
private:
55
 
  LibraryMap library_registry_;
56
 
  ModuleMap module_registry_;
57
 
  boost::scoped_ptr<Graph> depend_graph_; 
58
 
  
59
 
  plugin::Plugin::map plugin_registry;
60
 
 
61
 
  bool deps_built_;
62
 
 
63
 
  Registry();
64
 
  Registry(const Registry&);
65
 
  Registry& operator=(const Registry&);
66
 
  ~Registry();
67
 
 
68
 
  void buildDeps();
69
 
public:
70
 
 
71
 
  static Registry& singleton()
72
 
  {
73
 
    static Registry *registry= new Registry();
74
 
    return *registry;
75
 
  }
76
 
 
77
 
  void copy(plugin::Plugin::vector &arg);
78
 
 
79
 
  static void shutdown();
80
 
 
81
 
  Module *find(std::string name);
82
 
 
83
 
  void add(Module *module);
84
 
 
85
 
  void remove(Module *module);
86
 
 
87
 
  std::vector<Module *> getList();
88
 
 
89
 
  const plugin::Plugin::map &getPluginsMap() const
90
 
  {
91
 
    return plugin_registry;
92
 
  }
93
 
 
94
 
  const ModuleMap &getModulesMap() const
95
 
  {
96
 
    return module_registry_;
97
 
  }
98
 
 
99
 
  Library *addLibrary(const std::string &plugin_name, bool builtin= false);
100
 
  void removeLibrary(const std::string &plugin_name);
101
 
  Library *findLibrary(const std::string &plugin_name) const;
102
 
 
103
 
  void shutdownModules();
104
 
 
105
 
  template<class T>
106
 
  void add(T *plugin)
107
 
  {
108
 
    bool failed= false;
109
 
    std::string plugin_type(plugin->getTypeName());
110
 
    std::transform(plugin_type.begin(), plugin_type.end(),
111
 
                   plugin_type.begin(), ::tolower);
112
 
    std::string plugin_name(plugin->getName());
113
 
    std::transform(plugin_name.begin(), plugin_name.end(),
114
 
                   plugin_name.begin(), ::tolower);
115
 
    if (plugin_registry.find(std::make_pair(plugin_type, plugin_name)) != plugin_registry.end())
116
 
    {
117
 
      errmsg_printf(error::ERROR,
118
 
                    _("Loading plugin %s failed: a %s plugin by that name "
119
 
                      "already exists.\n"),
120
 
                    plugin->getTypeName().c_str(),
121
 
                    plugin->getName().c_str());
122
 
      failed= true;
123
 
    }
124
 
    if (T::addPlugin(plugin))
125
 
    {
126
 
      failed= true;
127
 
    }
128
 
 
129
 
    if (failed)
130
 
    {
131
 
      errmsg_printf(error::ERROR,
132
 
                    _("Fatal error: Failed initializing %s::%s plugin.\n"),
133
 
                    plugin->getTypeName().c_str(),
134
 
                    plugin->getName().c_str());
135
 
      unireg_abort(1);
136
 
    }
137
 
    plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
138
 
  }
139
 
 
140
 
  template<class T>
141
 
  void remove(T *plugin)
142
 
  {
143
 
    std::string plugin_type(plugin->getTypeName());
144
 
    std::transform(plugin_type.begin(), plugin_type.end(),
145
 
                   plugin_type.begin(), ::tolower);
146
 
    std::string plugin_name(plugin->getName());
147
 
    std::transform(plugin_name.begin(), plugin_name.end(),
148
 
                   plugin_name.begin(), ::tolower);
149
 
    T::removePlugin(plugin);
150
 
    plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
151
 
  }
152
 
 
153
 
 
154
 
};
155
 
 
156
 
} /* namespace module */
157
 
} /* namespace drizzled */
158
 
#endif /* DRIZZLED_MODULE_REGISTRY_H */