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
32
std::map<std::string, T> item_map;
35
bool addItemEntry(std::string name, T item)
37
if (item_map.count(name) == 0)
45
bool addItem(std::string name, T item)
48
/* First, add with no transform */
49
if (addItemEntry(name, item))
52
/* Transform to lower, then add */
53
transform(name.begin(), name.end(),
54
name.begin(), ::tolower);
55
/* Ignore failures here - the original name could be all lower */
56
addItemEntry(name, item);
61
void removeItem(std::string name)
64
/* First, remove with no transform */
67
/* Transform to lower, then remove */
68
transform(name.begin(), name.end(),
69
name.begin(), ::tolower);
75
typedef typename std::set<T>::const_iterator const_iterator;
76
typedef typename std::set<T>::iterator iterator;
77
typedef size_t size_type;
79
T find(const char *name, size_t length)
81
std::string find_str(name, length);
82
return find(find_str);
85
T find(const std::string &name)
88
typename std::map<std::string, T>::iterator find_iter;
89
find_iter= item_map.find(name);
90
if (find_iter != item_map.end())
91
return (*find_iter).second;
93
/* We must look for lower case, so we make a copy of the input name */
94
std::string lower_name(name);
95
transform(lower_name.begin(), lower_name.end(),
96
lower_name.begin(), ::tolower);
97
find_iter= item_map.find(lower_name);
98
if (find_iter != item_map.end())
99
return (*find_iter).second;
111
if (item_set.insert(item).second == false)
114
if (addItem(item->getName(), item))
117
const std::vector<std::string>& aliases= item->getAliases();
118
if (!(failed) && (aliases.size() > 0))
120
typename std::vector<std::string>::const_iterator iter= aliases.begin();
121
while (iter != aliases.end())
123
if(addItem(*iter, item))
135
* Remove an item from the registry. We don't care about failure
139
removeItem(item->getName());
141
const std::vector<std::string>& aliases= item->getAliases();
142
if (aliases.size() > 0)
144
std::vector<std::string>::const_iterator iter= aliases.begin();
145
while (iter != aliases.end())
153
const_iterator begin()
155
return item_set.begin();
160
return item_set.end();
163
size_type count(std::string name) const
165
/* Transform to lower, then add */
166
transform(name.begin(), name.end(),
167
name.begin(), ::tolower);
168
return item_map.count(name);
172
} /* namespace drizzled */
174
#endif /* DRIZZLED_REGISTRY_H */