Advanced Strategic Command
factory.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
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; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #ifndef factoryH
22 #define factoryH
23 
24 
25 #include <map>
26 #include "messaginghub.h"
27 
28 template<class Base, class Derived>
30 {
31  return new Derived;
32 }
33 
34 
35 template < class AbstractProduct,
36  typename IdentifierType >
37 class Factory{
38  public:
39  typedef AbstractProduct* (*ObjectCreatorCallBack)();
40  private:
41  typedef std::map<IdentifierType, ObjectCreatorCallBack> CallbackMap;
42  CallbackMap callbackMap;
43  public:
44  bool registerClass( IdentifierType id, ObjectCreatorCallBack createFn ) { callbackMap[id] = createFn; return true; };
45  AbstractProduct* createObject( IdentifierType id )
46  {
47  typename CallbackMap::const_iterator i = callbackMap.find(id);
48  if ( i != callbackMap.end() )
49  return (i->second)();
50  else {
51  fatalError("Factory: Object ID not found");
52  return NULL;
53  }
54  };
55 };
56 
57 template < class AbstractProduct,
58  typename IdentifierType,
59  typename creatorParam >
60 class Factory1{
61  public:
62  typedef AbstractProduct* (*ObjectCreatorCallBack)(creatorParam param);
63  private:
64  typedef std::map<IdentifierType, ObjectCreatorCallBack> CallbackMap;
65  CallbackMap callbackMap;
66  public:
67  bool registerClass( IdentifierType id, ObjectCreatorCallBack createFn ) { callbackMap[id] = createFn; return true; };
68  AbstractProduct* createObject( IdentifierType id, creatorParam param )
69  {
70  typename CallbackMap::const_iterator i = callbackMap.find(id);
71  if ( i != callbackMap.end() )
72  return (i->second)(param);
73  else {
74  fatalError("Factory: Object ID not found");
75  return NULL;
76  }
77  };
78 };
79 
80 
81 #endif
AbstractProduct *(* ObjectCreatorCallBack)()
Definition: factory.h:39
AbstractProduct *(* ObjectCreatorCallBack)(creatorParam param)
Definition: factory.h:62
bool registerClass(IdentifierType id, ObjectCreatorCallBack createFn)
Definition: factory.h:67
bool registerClass(IdentifierType id, ObjectCreatorCallBack createFn)
Definition: factory.h:44
Base * ObjectCreator()
Definition: factory.h:29
AbstractProduct * createObject(IdentifierType id)
Definition: factory.h:45
void fatalError(const ASCString &string)
AbstractProduct * createObject(IdentifierType id, creatorParam param)
Definition: factory.h:68