~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.h

  • Committer: Brian Aker
  • Date: 2010-08-18 20:55:22 UTC
  • mfrom: (1711.6.3 staging)
  • Revision ID: brian@tangent.org-20100818205522-esgel82hp9kyl3l2
Merge mutex patch

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, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
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
 
#pragma once
 
20
#ifndef DRIZZLED_MODULE_REGISTRY_H
 
21
#define DRIZZLED_MODULE_REGISTRY_H
21
22
 
22
23
#include <string>
23
24
#include <vector>
24
25
#include <map>
25
26
#include <algorithm>
26
 
#include <iosfwd>
27
 
 
28
 
#include <boost/algorithm/string.hpp>
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
 
#include <drizzled/util/find_ptr.h>
36
 
 
37
 
namespace drizzled {
38
 
namespace module {
39
 
 
40
 
class Registry : boost::noncopyable
41
 
{
42
 
public:
43
 
  typedef std::map<std::string, Library*> LibraryMap;
44
 
  typedef std::map<std::string, Module*> ModuleMap;
45
 
  typedef std::vector<Module*> ModuleList;
 
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
46
private:
47
 
  LibraryMap library_registry_;
48
 
  ModuleMap module_registry_;
49
 
  boost::scoped_ptr<Graph> depend_graph_; 
50
 
  
51
 
  plugin::Plugin::map plugin_registry;
52
 
 
53
 
  bool deps_built_;
54
 
 
55
 
  Registry();
 
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&);
56
58
  ~Registry();
57
 
 
58
 
  void buildDeps();
59
59
public:
60
60
 
61
61
  static Registry& singleton()
62
62
  {
63
 
    static Registry* registry= new Registry();
 
63
    static Registry *registry= new Registry();
64
64
    return *registry;
65
65
  }
66
66
 
67
67
  static void shutdown();
68
68
 
69
 
  Module* find(const std::string&);
70
 
 
71
 
  void add(Module*);
72
 
  void remove(Module*);
73
 
 
74
 
  ModuleList getList();
75
 
 
76
 
  const plugin::Plugin::map &getPluginsMap() const
 
69
  Module *find(std::string name);
 
70
 
 
71
  void add(Module *module);
 
72
 
 
73
 
 
74
  std::vector<Module *> getList(bool active);
 
75
 
 
76
  const std::map<std::string, plugin::Plugin *> &getPluginsMap() const
77
77
  {
78
78
    return plugin_registry;
79
79
  }
80
80
 
81
 
  const ModuleMap &getModulesMap() const
 
81
  const std::map<std::string, Module *> &getModulesMap() const
82
82
  {
83
 
    return module_registry_;
 
83
    return module_map;
84
84
  }
85
85
 
86
86
  Library *addLibrary(const std::string &plugin_name, bool builtin= false);
93
93
  void add(T *plugin)
94
94
  {
95
95
    bool failed= false;
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)))
 
96
    std::string plugin_name(plugin->getName());
 
97
    std::transform(plugin_name.begin(), plugin_name.end(),
 
98
                   plugin_name.begin(), ::tolower);
 
99
    if (plugin_registry.find(plugin_name) != plugin_registry.end())
99
100
    {
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());
 
101
      errmsg_printf(ERRMSG_LVL_ERROR,
 
102
                    _("Loading plugin %s failed: a plugin by that name already "
 
103
                      "exists.\n"), plugin->getName().c_str());
102
104
      failed= true;
103
105
    }
104
 
    if (T::addPlugin(plugin)) // Olaf: Should addPlugin be called when failed is already true?
105
 
      failed= true; 
106
 
 
 
106
    if (T::addPlugin(plugin))
 
107
      failed= true;
107
108
    if (failed)
108
109
    {
109
 
      errmsg_printf(error::ERROR, _("Fatal error: Failed initializing %s::%s plugin.\n"), 
110
 
        plugin->getTypeName().c_str(), plugin->getName().c_str());
 
110
      errmsg_printf(ERRMSG_LVL_ERROR,
 
111
                    _("Fatal error: Failed initializing %s plugin.\n"),
 
112
                    plugin->getName().c_str());
111
113
      unireg_abort(1);
112
114
    }
113
 
    plugin_registry.insert(std::make_pair(std::make_pair(plugin_type, plugin_name), plugin));
 
115
    plugin_registry.insert(std::pair<std::string, plugin::Plugin *>(plugin_name, plugin));
114
116
  }
115
117
 
116
118
  template<class T>
117
119
  void remove(T *plugin)
118
120
  {
119
 
    std::string plugin_type(boost::to_lower_copy(plugin->getTypeName()));
120
 
    std::string plugin_name(boost::to_lower_copy(plugin->getName()));
 
121
    std::string plugin_name(plugin->getName());
 
122
    std::transform(plugin_name.begin(), plugin_name.end(),
 
123
                   plugin_name.begin(), ::tolower);
121
124
    T::removePlugin(plugin);
122
 
    plugin_registry.erase(std::make_pair(plugin_type, plugin_name));
 
125
    plugin_registry.erase(plugin_name);
123
126
  }
124
127
 
125
128
 
127
130
 
128
131
} /* namespace module */
129
132
} /* namespace drizzled */
 
133
#endif /* DRIZZLED_MODULE_REGISTRY_H */