~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DRIZZLED_REGISTRY_H
#define DRIZZLED_REGISTRY_H

#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <functional>

namespace drizzled {

namespace internal {

template<class T>
struct RegistryMapCompare
{
  bool operator() (const T& a, const T& b) const
  {
    return a.getName() < b.getName();
  }
};

template<class T>
struct RegistryMapCompare<T *>
{
  bool operator() (const T* a, const T* b) const
  {
    return a->getName() < b->getName();
  }
};

} /* namespace internal */

template<class T>
class Registry
{
  std::map<std::string, T> item_map;
  std::set<T,internal::RegistryMapCompare<T> > item_set;

  bool addItemEntry(std::string name, T item)
  {
    if (item_map.count(name) == 0)
    {
      item_map[name]= item;
      return false;
    }
    return true;
  }

  bool addItem(std::string name, T item)
  {

    /* First, add with no transform */
    if (addItemEntry(name, item))
      return true;

    /* Transform to lower, then add */ 
    transform(name.begin(), name.end(),
              name.begin(), ::tolower);

    /* Ignore failures here - the original name could be all lower */
    addItemEntry(name, item);

    /* Transform to upper, then add */ 
    transform(name.begin(), name.end(),
              name.begin(), ::toupper);

    /* Ignore failures here - the original name could be all upper */
    addItemEntry(name, item);

    return false;
  }

  void removeItem(std::string name)
  {

    /* First, remove with no transform */
    item_map.erase(name);

    /* Transform to lower, then remove */ 
    transform(name.begin(), name.end(),
              name.begin(), ::tolower);
    item_map.erase(name);
  }

public:

  typedef typename std::set<T>::const_iterator const_iterator;
  typedef typename std::set<T>::iterator iterator;
  typedef size_t size_type;

  T find(const char *name, size_t length)
  {
    std::string find_str(name, length);
    return find(find_str);
  }

  T find(const std::string &name)
  {

    typename std::map<std::string, T>::iterator find_iter;
    find_iter=  item_map.find(name);
    if (find_iter != item_map.end())
      return (*find_iter).second;
    
    /* We must look for lower case, so we make a copy of the input name */
    std::string lower_name(name);
    transform(lower_name.begin(), lower_name.end(),
              lower_name.begin(), ::tolower);
    find_iter=  item_map.find(lower_name);
    if (find_iter != item_map.end())
      return (*find_iter).second;

    return NULL;
  }

  /**
   * True == failure
   */
  bool add(T item)
  {
    bool failed= false;

    if (item_set.insert(item).second == false)
      return true;

    if (addItem(item->getName(), item))
      failed= true;

    const std::vector<std::string>& aliases= item->getAliases();
    if (!(failed) && (aliases.size() > 0))
    {
      typename std::vector<std::string>::const_iterator iter= aliases.begin();
      while (iter != aliases.end())
      {
        if(addItem(*iter, item))
          failed= true;
        ++iter;
      }
    }
   
    if (failed == true)
      remove(item);
    return failed; 
  }

  /**
   * Remove an item from the registry. We don't care about failure
   */
  void remove(T item)
  {
    removeItem(item->getName());

    const std::vector<std::string>& aliases= item->getAliases();
    if (aliases.size() > 0)
    {
      std::vector<std::string>::const_iterator iter= aliases.begin();
      while (iter != aliases.end())
      {
        removeItem(*iter);
        ++iter;
      }
    }
  }

  const_iterator begin()
  {
    return item_set.begin();
  }

  const_iterator end()
  {
    return item_set.end();
  }

  size_type count(std::string name) const
  {
    /* Transform to lower, then add */
    transform(name.begin(), name.end(),
              name.begin(), ::tolower);
    return item_map.count(name);
  }

  size_type size() const
  {
    return item_set.size();
  }
};

} /* namespace drizzled */

#endif /* DRIZZLED_REGISTRY_H */