~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/module/registry.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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
 
#include "config.h"
21
 
 
22
 
#include <string>
23
 
#include <vector>
24
 
#include <map>
25
 
 
26
 
#include "drizzled/module/registry.h"
27
 
#include "drizzled/module/library.h"
28
 
 
29
 
#include "drizzled/plugin.h"
30
 
#include "drizzled/show.h"
31
 
#include "drizzled/cursor.h"
32
 
 
33
 
#include <boost/bind.hpp>
34
 
 
35
 
using namespace std;
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
 
 
41
 
module::Registry::~Registry()
42
 
{
43
 
  std::map<std::string, plugin::Plugin *>::iterator plugin_iter;
44
 
 
45
 
  /* Give all plugins a chance to cleanup, before
46
 
   * all plugins are deleted.
47
 
   * This can be used if shutdown code references
48
 
   * other plugins.
49
 
   */
50
 
  plugin_iter= plugin_registry.begin();
51
 
  while (plugin_iter != plugin_registry.end())
52
 
  {
53
 
    (*plugin_iter).second->shutdownPlugin();
54
 
    ++plugin_iter;
55
 
  }
56
 
 
57
 
  plugin_iter= plugin_registry.begin();
58
 
  while (plugin_iter != plugin_registry.end())
59
 
  {
60
 
    delete (*plugin_iter).second;
61
 
    ++plugin_iter;
62
 
  }
63
 
  plugin_registry.clear();
64
 
 
65
 
#if 0
66
 
  @TODO When we delete modules here, we segfault on a bad string. Why?
67
 
    map<string, module::Module *>::iterator module_iter= module_map.begin();
68
 
 
69
 
  while (module_iter != module_map.end())
70
 
  {
71
 
    delete (*module_iter).second;
72
 
    ++module_iter;
73
 
  }
74
 
  module_map.clear();
75
 
#endif
76
 
  std::map<std::string, module::Library *>::iterator library_iter= library_map.begin();
77
 
  while (library_iter != library_map.end())
78
 
  {
79
 
    delete (*library_iter).second;
80
 
    ++library_iter;
81
 
  }
82
 
  library_map.clear();
83
 
}
84
 
 
85
 
void module::Registry::shutdown()
86
 
{
87
 
  module::Registry& registry= singleton();
88
 
  delete &registry;
89
 
}
90
 
 
91
 
module::Module *module::Registry::find(std::string name)
92
 
{
93
 
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
94
 
 
95
 
  std::map<std::string, module::Module *>::iterator map_iter;
96
 
  map_iter= module_map.find(name);
97
 
  if (map_iter != module_map.end())
98
 
    return (*map_iter).second;
99
 
  return(0);
100
 
}
101
 
 
102
 
void module::Registry::add(module::Module *handle)
103
 
{
104
 
  std::string add_str(handle->getName());
105
 
  transform(add_str.begin(), add_str.end(),
106
 
            add_str.begin(), ::tolower);
107
 
 
108
 
  module_map[add_str]= handle;
109
 
}
110
 
 
111
 
void module::Registry::remove(module::Module *handle)
112
 
{
113
 
  std::string remove_str(handle->getName());
114
 
  std::transform(remove_str.begin(), remove_str.end(),
115
 
                 remove_str.begin(), ::tolower);
116
 
 
117
 
  module_map.erase(remove_str);
118
 
}
119
 
 
120
 
void module::Registry::copy(plugin::Plugin::vector &arg)
121
 
{    
122
 
  arg.reserve(plugin_registry.size());
123
 
 
124
 
  std::transform(plugin_registry.begin(),
125
 
                 plugin_registry.end(),
126
 
                 std::back_inserter(arg),
127
 
                 boost::bind(&plugin::Plugin::map::value_type::second, _1) );
128
 
  assert(arg.size() == plugin_registry.size());
129
 
}
130
 
 
131
 
vector<module::Module *> module::Registry::getList(bool active)
132
 
{
133
 
  module::Module *plugin= NULL;
134
 
 
135
 
  std::vector<module::Module *> plugins;
136
 
  plugins.reserve(module_map.size());
137
 
 
138
 
  std::map<std::string, module::Module *>::iterator map_iter;
139
 
  for (map_iter= module_map.begin();
140
 
       map_iter != module_map.end();
141
 
       map_iter++)
142
 
  {
143
 
    plugin= (*map_iter).second;
144
 
    if (active)
145
 
      plugins.push_back(plugin);
146
 
    else if (plugin->isInited)
147
 
      plugins.push_back(plugin);
148
 
  }
149
 
 
150
 
  return plugins;
151
 
}
152
 
 
153
 
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
154
 
                                              bool builtin)
155
 
{
156
 
 
157
 
  /* If this dll is already loaded just return it */
158
 
  module::Library *library= findLibrary(plugin_name);
159
 
  if (library != NULL)
160
 
  {
161
 
    return library;
162
 
  }
163
 
 
164
 
  library= module::Library::loadLibrary(plugin_name, builtin);
165
 
  if (library != NULL)
166
 
  {
167
 
    /* Add this dll to the map */
168
 
    library_map.insert(make_pair(plugin_name, library));
169
 
  }
170
 
 
171
 
  return library;
172
 
}
173
 
 
174
 
void module::Registry::removeLibrary(const std::string &plugin_name)
175
 
{
176
 
  std::map<std::string, module::Library *>::iterator iter=
177
 
    library_map.find(plugin_name);
178
 
  if (iter != library_map.end())
179
 
  {
180
 
    library_map.erase(iter);
181
 
    delete (*iter).second;
182
 
  }
183
 
}
184
 
 
185
 
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
186
 
{
187
 
  std::map<std::string, module::Library *>::const_iterator iter=
188
 
    library_map.find(plugin_name);
189
 
  if (iter != library_map.end())
190
 
    return (*iter).second;
191
 
  return NULL;
192
 
}
193
 
 
194
 
void module::Registry::shutdownModules()
195
 
{
196
 
  module_shutdown(*this);
197
 
}
198
 
 
199
 
} /* namespace drizzled */