~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.h

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLED_PLUGIN_REGISTRY_H
21
 
#define DRIZZLED_PLUGIN_REGISTRY_H
 
20
#ifndef DRIZZLED_MODULE_REGISTRY_H
 
21
#define DRIZZLED_MODULE_REGISTRY_H
22
22
 
23
23
#include <string>
24
24
#include <vector>
25
25
#include <map>
26
26
#include <algorithm>
 
27
#include <iostream>
 
28
 
 
29
#include <boost/scoped_ptr.hpp>
27
30
 
28
31
#include "drizzled/gettext.h"
29
32
#include "drizzled/unireg.h"
30
33
#include "drizzled/errmsg_print.h"
 
34
#include "drizzled/plugin/plugin.h"
 
35
 
31
36
 
32
37
namespace drizzled
33
38
{
34
 
namespace plugin
 
39
 
 
40
namespace module
35
41
{
36
42
class Module;
37
 
class Plugin;
38
43
class Library;
 
44
class Graph;
 
45
 
39
46
 
40
47
class Registry
41
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;
42
54
private:
43
 
  std::map<std::string, Library *> library_map;
44
 
  std::map<std::string, Module *> module_map;
45
 
  std::map<std::string, Plugin *> plugin_registry;
46
 
 
47
 
  Registry()
48
 
   : module_map(),
49
 
     plugin_registry()
50
 
  { }
51
 
 
 
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();
52
64
  Registry(const Registry&);
53
65
  Registry& operator=(const Registry&);
54
66
  ~Registry();
 
67
 
 
68
  void buildDeps();
55
69
public:
56
70
 
57
 
  static plugin::Registry& singleton()
 
71
  static Registry& singleton()
58
72
  {
59
 
    static plugin::Registry *registry= new plugin::Registry();
 
73
    static Registry *registry= new Registry();
60
74
    return *registry;
61
75
  }
62
76
 
 
77
  void copy(plugin::Plugin::vector &arg);
 
78
 
63
79
  static void shutdown();
64
80
 
65
81
  Module *find(std::string name);
66
82
 
67
83
  void add(Module *module);
68
84
 
69
 
 
70
 
  std::vector<Module *> getList(bool active);
71
 
 
72
 
  const std::map<std::string, Plugin *> &getPluginsMap() const
 
85
  void remove(Module *module);
 
86
 
 
87
  std::vector<Module *> getList();
 
88
 
 
89
  const plugin::Plugin::map &getPluginsMap() const
73
90
  {
74
91
    return plugin_registry;
75
92
  }
76
93
 
77
 
  const std::map<std::string, Module *> &getModulesMap() const
 
94
  const ModuleMap &getModulesMap() const
78
95
  {
79
 
    return module_map;
 
96
    return module_registry_;
80
97
  }
81
98
 
82
 
  Library *addLibrary(const std::string &plugin_name);
 
99
  Library *addLibrary(const std::string &plugin_name, bool builtin= false);
83
100
  void removeLibrary(const std::string &plugin_name);
84
101
  Library *findLibrary(const std::string &plugin_name) const;
85
102
 
 
103
  void shutdownModules();
 
104
 
86
105
  template<class T>
87
106
  void add(T *plugin)
88
107
  {
89
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);
90
112
    std::string plugin_name(plugin->getName());
91
113
    std::transform(plugin_name.begin(), plugin_name.end(),
92
114
                   plugin_name.begin(), ::tolower);
93
 
    if (plugin_registry.find(plugin_name) != plugin_registry.end())
 
115
    if (plugin_registry.find(std::make_pair(plugin_type, plugin_name)) != plugin_registry.end())
94
116
    {
95
 
      errmsg_printf(ERRMSG_LVL_ERROR,
96
 
                    _("Loading plugin %s failed: a plugin by that name already "
97
 
                      "exists.\n"), plugin->getName().c_str());
 
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());
98
122
      failed= true;
99
123
    }
100
124
    if (T::addPlugin(plugin))
 
125
    {
101
126
      failed= true;
 
127
    }
 
128
 
102
129
    if (failed)
103
130
    {
104
 
      errmsg_printf(ERRMSG_LVL_ERROR,
105
 
                    _("Fatal error: Failed initializing %s plugin.\n"),
 
131
      errmsg_printf(error::ERROR,
 
132
                    _("Fatal error: Failed initializing %s::%s plugin.\n"),
 
133
                    plugin->getTypeName().c_str(),
106
134
                    plugin->getName().c_str());
107
135
      unireg_abort(1);
108
136
    }
109
 
    plugin_registry.insert(std::pair<std::string, Plugin *>(plugin_name, plugin));
 
137
    plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
110
138
  }
111
139
 
112
140
  template<class T>
113
141
  void remove(T *plugin)
114
142
  {
 
143
    std::string plugin_type(plugin->getTypeName());
 
144
    std::transform(plugin_type.begin(), plugin_type.end(),
 
145
                   plugin_type.begin(), ::tolower);
115
146
    std::string plugin_name(plugin->getName());
116
147
    std::transform(plugin_name.begin(), plugin_name.end(),
117
148
                   plugin_name.begin(), ::tolower);
118
149
    T::removePlugin(plugin);
119
 
    plugin_registry.erase(plugin_name);
 
150
    plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
120
151
  }
121
152
 
122
153
 
123
154
};
124
155
 
125
 
} /* end namespace plugin */
126
 
} /* end namespace drizzled */
127
 
#endif /* DRIZZLED_PLUGIN_REGISTRY_H */
 
156
} /* namespace module */
 
157
} /* namespace drizzled */
 
158
#endif /* DRIZZLED_MODULE_REGISTRY_H */