source: trunk/source/graphics_reps/include/G4CreatorFactoryT.hh@ 1345

Last change on this file since 1345 was 1337, checked in by garnier, 15 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 3.8 KB
RevLine 
[830]1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// $Id: G4CreatorFactoryT.hh,v 1.2 2006/12/13 15:44:03 gunter Exp $
[1337]27// GEANT4 tag $Name: geant4-09-04-beta-01 $
[830]28//
29// Generic identifier-creator based factory. Based on
30// factory presented in "Modern C++ Design, Andrei Alexandrescu"
31//
32// Jane Tinslay, September 2006
33//
34#ifndef G4CREATORFACTORYT_HH
35#define G4CREATORFACTORYT_HH
36
37#include "globals.hh"
38#include <map>
39#include <sstream>
40
41template <typename T, typename Identifier, typename Creator>
42class G4CreatorFactoryT {
43
44public:
45
46 // Constructor
47 G4CreatorFactoryT();
48
49 // Destructor
50 virtual ~G4CreatorFactoryT();
51
52 // Register identifier<->creator pairs
53 G4bool Register(const Identifier& id, Creator creator);
54
55 // Create product with given identifier
56 T* Create(const Identifier& id) const;
57
58private:
59
60 typedef std::map<Identifier, Creator> Map;
61
62 // Data member
63 Map fMap;
64
65};
66
67template <typename T, typename Identifier, typename Creator>
68G4CreatorFactoryT<T, Identifier, Creator>::G4CreatorFactoryT() {}
69
70template <typename T, typename Identifier, typename Creator>
71G4CreatorFactoryT<T, Identifier, Creator>::~G4CreatorFactoryT() {}
72
73template <typename T, typename Identifier, typename Creator>
74G4bool
75G4CreatorFactoryT<T, Identifier, Creator>::Register(const Identifier& id,
76 Creator creator)
77{
78 if (fMap.find(id) != fMap.end()) {
79 std::ostringstream o;
80 o << "Creator with identifier "<<id<<" already exists.";
81 G4Exception
82 ("G4CreatorFactoryT::Register(const Identifier& id, Creator creator)",
83 "CreatorExists", JustWarning, o.str().c_str());
84 return false;
85 }
86
87 // Insert identifier<->creator pair into map
88 std::pair<Identifier, Creator> myPair(id, creator);
89 return fMap.insert(myPair).second;
90}
91
92template <typename T, typename Identifier, typename Creator>
93T*
94G4CreatorFactoryT<T, Identifier, Creator>::Create(const Identifier& id) const
95{
96 typename Map::const_iterator iter = fMap.find(id);
97
98 if (iter == fMap.end()) {
99 std::ostringstream o;
100 o << "Identifier "<<id<<" does not exist.";
101 G4Exception("G4CreatorFactoryT::Create(const Identifier& id)",
102 "NonExistentIdentifier", JustWarning, o.str().c_str());
103 return 0;
104 }
105
106 return iter->second();
107}
108
109#endif
Note: See TracBrowser for help on using the repository browser.