2
Copyright (C) 2000-2007 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; version 2 of the License.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; see the file COPYING. If not, write to the
15
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19
/* The factory header defines an Object Factory, used by SSL message and
22
* See Desgin Pattern in GoF and Alexandrescu's chapter in Modern C++ Design,
28
#ifndef yaSSL_FACTORY_HPP
29
#define yaSSL_FACTORY_HPP
31
#include STL_VECTOR_FILE
32
#include STL_PAIR_FILE
35
namespace STL = STL_NAMESPACE;
44
// Factory uses its callback map to create objects by id,
45
// returning an abstract base pointer
46
template<class AbstractProduct,
47
typename IdentifierType = int,
48
typename ProductCreator = AbstractProduct* (*)()
51
typedef STL::pair<IdentifierType, ProductCreator> CallBack;
52
typedef STL::vector<CallBack> CallBackVector;
54
CallBackVector callbacks_;
56
// pass function pointer to register all callbacks upon creation
57
explicit Factory(void (*init)(Factory<AbstractProduct, IdentifierType,
63
// reserve place in vector before registering, used by init funcion
64
void Reserve(size_t sz)
66
callbacks_.reserve(sz);
70
void Register(const IdentifierType& id, ProductCreator pc)
72
callbacks_.push_back(STL::make_pair(id, pc));
75
// THE Creator, returns a new object of the proper type or 0
76
AbstractProduct* CreateObject(const IdentifierType& id) const
78
typedef typename STL::vector<CallBack>::const_iterator cIter;
80
cIter first = callbacks_.begin();
81
cIter last = callbacks_.end();
83
while (first != last) {
84
if (first->first == id)
89
if (first == callbacks_.end())
91
return (first->second)();
94
Factory(const Factory&); // hide copy
95
Factory& operator=(const Factory&); // and assign
101
#endif // yaSSL_FACTORY_HPP