1
////////////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2001 by Andrei Alexandrescu
4
// This code accompanies the book:
5
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7
// Permission to use, copy, modify, distribute and sell this software for any
8
// purpose is hereby granted without fee, provided that the above copyright
9
// notice appear in all copies and that both that copyright notice and this
10
// permission notice appear in supporting documentation.
11
// The author or Addison-Welsey Longman make no representations about the
12
// suitability of this software for any purpose. It is provided "as is"
13
// without express or implied warranty.
14
////////////////////////////////////////////////////////////////////////////////
16
// Last update: June 20, 2001
22
#include "AssocVector.h"
28
////////////////////////////////////////////////////////////////////////////////
29
// class template DefaultFactoryError
30
// Manages the "Unknown Type" error in an object factory
31
////////////////////////////////////////////////////////////////////////////////
33
template <typename IdentifierType, class AbstractProduct>
34
struct DefaultFactoryError
36
struct Exception : public std::exception
38
const char* what() const { return "Unknown Type"; }
41
static AbstractProduct* OnUnknownType(IdentifierType)
47
////////////////////////////////////////////////////////////////////////////////
48
// class template Factory
49
// Implements a generic object factory
50
////////////////////////////////////////////////////////////////////////////////
54
class AbstractProduct,
55
typename IdentifierType,
56
typename ProductCreator = AbstractProduct* (*)(),
57
template<typename, class>
58
class FactoryErrorPolicy = DefaultFactoryError
61
: public FactoryErrorPolicy<IdentifierType, AbstractProduct>
64
bool Register(const IdentifierType& id, ProductCreator creator)
66
return associations_.insert(
67
IdToProductMap::value_type(id, creator)).second;
70
bool Unregister(const IdentifierType& id)
72
return associations_.erase(id) == 1;
75
AbstractProduct* CreateObject(const IdentifierType& id)
77
typename IdToProductMap::const_iterator i = associations_.find(id);
78
if (i != associations_.end())
82
return OnUnknownType(id);
86
typedef AssocVector<IdentifierType, ProductCreator> IdToProductMap;
87
IdToProductMap associations_;
90
////////////////////////////////////////////////////////////////////////////////
91
// class template CloneFactory
92
// Implements a generic cloning factory
93
////////////////////////////////////////////////////////////////////////////////
97
class AbstractProduct,
98
class ProductCreator =
99
AbstractProduct* (*)(const AbstractProduct*),
100
template<typename, class>
101
class FactoryErrorPolicy = DefaultFactoryError
104
: public FactoryErrorPolicy<TypeInfo, AbstractProduct>
107
bool Register(const TypeInfo& ti, ProductCreator creator)
109
return associations_.insert(
110
IdToProductMap::value_type(ti, creator)).second;
113
bool Unregister(const TypeInfo& id)
115
return associations_.erase(id) == 1;
118
AbstractProduct* CreateObject(const AbstractProduct* model)
120
if (model == 0) return 0;
122
typename IdToProductMap::const_iterator i =
123
associations_.find(typeid(*model));
124
if (i != associations_.end())
126
return (i->second)(model);
128
return OnUnknownType(typeid(*model));
132
typedef AssocVector<TypeInfo, ProductCreator> IdToProductMap;
133
IdToProductMap associations_;
137
////////////////////////////////////////////////////////////////////////////////
139
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
140
////////////////////////////////////////////////////////////////////////////////
142
#endif // FACTORY_INC_
1
///////////////////////////////
2
// Generated header: Factory.h
3
// Forwards to the appropriate code
4
// that works on the detected compiler
5
// Generated on Mon Sep 30 23:14:48 2002
6
///////////////////////////////
8
#ifdef LOKI_USE_REFERENCE
9
# include "Reference/Factory.h"
11
# if (__INTEL_COMPILER)
12
# include "Reference/Factory.h"
14
# include "Reference/Factory.h"
15
# elif (__BORLANDC__ >= 0x560)
16
# include "Borland/Factory.h"
17
# elif (_MSC_VER >= 1301)
18
# include "Reference/Factory.h"
19
# elif (_MSC_VER >= 1300)
20
# include "MSVC/1300/Factory.h"
21
# elif (_MSC_VER >= 1200)
22
# include "MSVC/1200/Factory.h"
24
# include "Reference/Factory.h"