~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/registry.h

Removing global errbuff and cleaning up two remaining instances that referenced it.

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
#ifndef DRIZZLED_REGISTRY_H
 
21
#define DRIZZLED_REGISTRY_H
 
22
 
 
23
#include <map>
 
24
#include <set>
 
25
#include <string>
 
26
 
 
27
namespace drizzled {
 
28
 
 
29
template<class T>
 
30
class Registry
 
31
{
 
32
  std::map<std::string, T> item_map;
 
33
  std::set<T> item_set;
 
34
 
 
35
  bool addItemEntry(std::string name, T item)
 
36
  {
 
37
    if (item_map.count(name) == 0)
 
38
    {
 
39
      item_map[name]= item;
 
40
      return false;
 
41
    }
 
42
    return true;
 
43
  }
 
44
 
 
45
  bool addItem(std::string name, T item)
 
46
  {
 
47
 
 
48
    /* First, add with no transform */
 
49
    if (addItemEntry(name, item))
 
50
      return true;
 
51
 
 
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);
 
57
 
 
58
    return false;
 
59
  }
 
60
 
 
61
  void removeItem(std::string name)
 
62
  {
 
63
 
 
64
    /* First, remove with no transform */
 
65
    item_map.erase(name);
 
66
 
 
67
    /* Transform to lower, then remove */ 
 
68
    transform(name.begin(), name.end(),
 
69
              name.begin(), ::tolower);
 
70
    item_map.erase(name);
 
71
  }
 
72
 
 
73
public:
 
74
 
 
75
  typedef typename std::set<T>::const_iterator const_iterator;
 
76
  typedef typename std::set<T>::iterator iterator;
 
77
  typedef size_t size_type;
 
78
 
 
79
  T find(const char *name, size_t length)
 
80
  {
 
81
    std::string find_str(name, length);
 
82
    return find(find_str);
 
83
  }
 
84
 
 
85
  T find(const std::string &name)
 
86
  {
 
87
 
 
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;
 
92
    
 
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;
 
100
 
 
101
    return NULL;
 
102
  }
 
103
 
 
104
  /**
 
105
   * True == failure
 
106
   */
 
107
  bool add(T item)
 
108
  {
 
109
    bool failed= false;
 
110
 
 
111
    if (item_set.insert(item).second == false)
 
112
      return true;
 
113
 
 
114
    if (addItem(item->getName(), item))
 
115
      failed= true;
 
116
 
 
117
    const std::vector<std::string>& aliases= item->getAliases();
 
118
    if (!(failed) && (aliases.size() > 0))
 
119
    {
 
120
      typename std::vector<std::string>::const_iterator iter= aliases.begin();
 
121
      while (iter != aliases.end())
 
122
      {
 
123
        if(addItem(*iter, item))
 
124
          failed= true;
 
125
        ++iter;
 
126
      }
 
127
    }
 
128
   
 
129
    if (failed == true)
 
130
      remove(item);
 
131
    return failed; 
 
132
  }
 
133
 
 
134
  /**
 
135
   * Remove an item from the registry. We don't care about failure
 
136
   */
 
137
  void remove(T item)
 
138
  {
 
139
    removeItem(item->getName());
 
140
 
 
141
    const std::vector<std::string>& aliases= item->getAliases();
 
142
    if (aliases.size() > 0)
 
143
    {
 
144
      std::vector<std::string>::const_iterator iter= aliases.begin();
 
145
      while (iter != aliases.end())
 
146
      {
 
147
        removeItem(*iter);
 
148
        ++iter;
 
149
      }
 
150
    }
 
151
  }
 
152
 
 
153
  const_iterator begin()
 
154
  {
 
155
    return item_set.begin();
 
156
  }
 
157
 
 
158
  const_iterator end()
 
159
  {
 
160
    return item_set.end();
 
161
  }
 
162
 
 
163
  size_type count(std::string name) const
 
164
  {
 
165
    /* Transform to lower, then add */
 
166
    transform(name.begin(), name.end(),
 
167
              name.begin(), ::tolower);
 
168
    return item_map.count(name);
 
169
  }
 
170
};
 
171
 
 
172
} /* namespace drizzled */
 
173
 
 
174
#endif /* DRIZZLED_REGISTRY_H */
 
175