1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#ifndef DRIZZLED_REGISTRY_H
21
#define DRIZZLED_REGISTRY_H
35
struct RegistryMapCompare
37
bool operator() (const T& a, const T& b) const
39
return a.getName() < b.getName();
44
struct RegistryMapCompare<T *>
46
bool operator() (const T* a, const T* b) const
48
return a->getName() < b->getName();
52
} /* namespace internal */
57
std::map<std::string, T> item_map;
58
std::set<T,internal::RegistryMapCompare<T> > item_set;
60
bool addItemEntry(std::string name, T item)
62
if (item_map.count(name) == 0)
70
bool addItem(std::string name, T item)
73
/* First, add with no transform */
74
if (addItemEntry(name, item))
77
/* Transform to lower, then add */
78
transform(name.begin(), name.end(),
79
name.begin(), ::tolower);
81
/* Ignore failures here - the original name could be all lower */
82
addItemEntry(name, item);
84
/* Transform to upper, then add */
85
transform(name.begin(), name.end(),
86
name.begin(), ::toupper);
88
/* Ignore failures here - the original name could be all upper */
89
addItemEntry(name, item);
94
void removeItem(std::string name)
97
/* First, remove with no transform */
100
/* Transform to lower, then remove */
101
transform(name.begin(), name.end(),
102
name.begin(), ::tolower);
103
item_map.erase(name);
108
typedef typename std::set<T>::const_iterator const_iterator;
109
typedef typename std::set<T>::iterator iterator;
110
typedef size_t size_type;
112
T find(const char *name, size_t length)
114
std::string find_str(name, length);
115
return find(find_str);
118
T find(const std::string &name)
121
typename std::map<std::string, T>::iterator find_iter;
122
find_iter= item_map.find(name);
123
if (find_iter != item_map.end())
124
return (*find_iter).second;
126
/* We must look for lower case, so we make a copy of the input name */
127
std::string lower_name(name);
128
transform(lower_name.begin(), lower_name.end(),
129
lower_name.begin(), ::tolower);
130
find_iter= item_map.find(lower_name);
131
if (find_iter != item_map.end())
132
return (*find_iter).second;
144
if (item_set.insert(item).second == false)
147
if (addItem(item->getName(), item))
150
const std::vector<std::string>& aliases= item->getAliases();
151
if (!(failed) && (aliases.size() > 0))
153
typename std::vector<std::string>::const_iterator iter= aliases.begin();
154
while (iter != aliases.end())
156
if(addItem(*iter, item))
168
* Remove an item from the registry. We don't care about failure
172
removeItem(item->getName());
174
const std::vector<std::string>& aliases= item->getAliases();
175
if (aliases.size() > 0)
177
std::vector<std::string>::const_iterator iter= aliases.begin();
178
while (iter != aliases.end())
186
const_iterator begin()
188
return item_set.begin();
193
return item_set.end();
196
size_type count(std::string name) const
198
/* Transform to lower, then add */
199
transform(name.begin(), name.end(),
200
name.begin(), ::tolower);
201
return item_map.count(name);
204
size_type size() const
206
return item_set.size();
210
} /* namespace drizzled */
212
#endif /* DRIZZLED_REGISTRY_H */