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
34
struct RegistryMapCompare
36
bool operator() (const T& a, const T& b) const
38
return a.getName() < b.getName();
43
struct RegistryMapCompare<T *>
45
bool operator() (const T* a, const T* b) const
47
return a->getName() < b->getName();
51
} /* namespace internal */
56
std::map<std::string, T> item_map;
57
std::set<T,internal::RegistryMapCompare<T> > item_set;
59
bool addItemEntry(std::string name, T item)
61
if (item_map.count(name) == 0)
69
bool addItem(std::string name, T item)
72
/* First, add with no transform */
73
if (addItemEntry(name, item))
76
/* Transform to lower, then add */
77
transform(name.begin(), name.end(),
78
name.begin(), ::tolower);
80
/* Ignore failures here - the original name could be all lower */
81
addItemEntry(name, item);
83
/* Transform to upper, then add */
84
transform(name.begin(), name.end(),
85
name.begin(), ::toupper);
87
/* Ignore failures here - the original name could be all upper */
88
addItemEntry(name, item);
93
void removeItem(std::string name)
96
/* First, remove with no transform */
99
/* Transform to lower, then remove */
100
transform(name.begin(), name.end(),
101
name.begin(), ::tolower);
102
item_map.erase(name);
107
typedef typename std::set<T>::const_iterator const_iterator;
108
typedef typename std::set<T>::iterator iterator;
109
typedef size_t size_type;
111
T find(const char *name, size_t length) const
113
std::string find_str(name, length);
114
return find(find_str);
117
T find(const std::string &name) const
120
typename std::map<std::string, T>::const_iterator find_iter;
121
find_iter= item_map.find(name);
122
if (find_iter != item_map.end())
123
return (*find_iter).second;
125
/* We must look for lower case, so we make a copy of the input name */
126
std::string lower_name(name);
127
transform(lower_name.begin(), lower_name.end(),
128
lower_name.begin(), ::tolower);
129
find_iter= item_map.find(lower_name);
130
if (find_iter != item_map.end())
131
return (*find_iter).second;
143
if (item_set.insert(item).second == false)
146
if (addItem(item->getName(), item))
149
const std::vector<std::string>& aliases= item->getAliases();
150
if (!(failed) && (aliases.size() > 0))
152
typename std::vector<std::string>::const_iterator iter= aliases.begin();
153
while (iter != aliases.end())
155
if(addItem(*iter, item))
167
* Remove an item from the registry. We don't care about failure
171
removeItem(item->getName());
173
const std::vector<std::string>& aliases= item->getAliases();
174
if (aliases.size() > 0)
176
std::vector<std::string>::const_iterator iter= aliases.begin();
177
while (iter != aliases.end())
185
const_iterator begin()
187
return item_set.begin();
192
return item_set.end();
195
size_type count(std::string name) const
197
/* Transform to lower, then add */
198
transform(name.begin(), name.end(),
199
name.begin(), ::tolower);
200
return item_map.count(name);
203
size_type size() const
205
return item_set.size();
209
} /* namespace drizzled */
211
#endif /* DRIZZLED_REGISTRY_H */