~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.h

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
merge lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#ifndef DRIZZLED_MODULE_REGISTRY_H
21
 
#define DRIZZLED_MODULE_REGISTRY_H
 
20
#pragma once
22
21
 
23
22
#include <string>
24
23
#include <vector>
25
24
#include <map>
26
25
#include <algorithm>
27
 
#include <iostream>
 
26
#include <iosfwd>
28
27
 
 
28
#include <boost/algorithm/string.hpp>
29
29
#include <boost/scoped_ptr.hpp>
30
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
 
31
#include <drizzled/gettext.h>
 
32
#include <drizzled/unireg.h>
 
33
#include <drizzled/errmsg_print.h>
 
34
#include <drizzled/plugin/plugin.h>
 
35
#include <drizzled/util/find_ptr.h>
 
36
 
 
37
namespace drizzled {
 
38
namespace module {
 
39
 
 
40
class Registry : boost::noncopyable
48
41
{
49
42
public:
50
 
 
51
 
  typedef std::map<std::string, Library *> LibraryMap;
52
 
  typedef std::map<std::string, Module *> ModuleMap;
53
 
  typedef std::vector<Module *> ModuleList;
 
43
  typedef std::map<std::string, Library*> LibraryMap;
 
44
  typedef std::map<std::string, Module*> ModuleMap;
 
45
  typedef std::vector<Module*> ModuleList;
54
46
private:
55
47
  LibraryMap library_registry_;
56
48
  ModuleMap module_registry_;
61
53
  bool deps_built_;
62
54
 
63
55
  Registry();
64
 
  Registry(const Registry&);
65
 
  Registry& operator=(const Registry&);
66
56
  ~Registry();
67
57
 
68
58
  void buildDeps();
70
60
 
71
61
  static Registry& singleton()
72
62
  {
73
 
    static Registry *registry= new Registry();
 
63
    static Registry* registry= new Registry();
74
64
    return *registry;
75
65
  }
76
66
 
77
 
  void copy(plugin::Plugin::vector &arg);
78
 
 
79
67
  static void shutdown();
80
68
 
81
 
  Module *find(std::string name);
82
 
 
83
 
  void add(Module *module);
84
 
 
85
 
  void remove(Module *module);
86
 
 
87
 
  std::vector<Module *> getList();
 
69
  Module* find(const std::string&);
 
70
 
 
71
  void add(Module*);
 
72
  void remove(Module*);
 
73
 
 
74
  ModuleList getList();
88
75
 
89
76
  const plugin::Plugin::map &getPluginsMap() const
90
77
  {
106
93
  void add(T *plugin)
107
94
  {
108
95
    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
 
    }
 
96
    std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
 
97
    std::string plugin_name(boost::to_lower_copy(plugin->getName()));
 
98
    if (find_ptr(plugin_registry, std::make_pair(plugin_type, plugin_name)))
 
99
    {
 
100
      errmsg_printf(error::ERROR, _("Loading plugin %s failed: a %s plugin by that name already exists.\n"), 
 
101
        plugin->getTypeName().c_str(), plugin->getName().c_str());
 
102
      failed= true;
 
103
    }
 
104
    if (T::addPlugin(plugin)) // Olaf: Should addPlugin be called when failed is already true?
 
105
      failed= true; 
128
106
 
129
107
    if (failed)
130
108
    {
131
 
      errmsg_printf(error::ERROR,
132
 
                    _("Fatal error: Failed initializing %s::%s plugin.\n"),
133
 
                    plugin->getTypeName().c_str(),
134
 
                    plugin->getName().c_str());
 
109
      errmsg_printf(error::ERROR, _("Fatal error: Failed initializing %s::%s plugin.\n"), 
 
110
        plugin->getTypeName().c_str(), plugin->getName().c_str());
135
111
      unireg_abort(1);
136
112
    }
137
113
    plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
140
116
  template<class T>
141
117
  void remove(T *plugin)
142
118
  {
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);
 
119
    std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
 
120
    std::string plugin_name(boost::to_lower_copy(plugin->getName()));
149
121
    T::removePlugin(plugin);
150
122
    plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
151
123
  }
155
127
 
156
128
} /* namespace module */
157
129
} /* namespace drizzled */
158
 
#endif /* DRIZZLED_MODULE_REGISTRY_H */