SMBIOS Library
FactoryImpl2.h
Go to the documentation of this file.
1 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:
2 /*
3  * Copyright (C) 2005 Dell Inc.
4  * by Michael Brown <Michael_E_Brown@dell.com>
5  * Licensed under the Open Software License version 2.1
6  *
7  * Alternatively, you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License,
10  * or (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  */
17 
18 
19 #ifndef FACTORY2_H_
20 #define FACTORY2_H_
21 
22 #define LIBSMBIOS_SOURCE
23 #include "smbios/compat.h"
24 
25 #include <map>
26 
27 #include "smbios/types.h"
28 
29 // abi_prefix should be last header included before declarations
31 
32 namespace factory
33 {
34 
35  template <class S>
36  class TFactory : public S
37  {
38  public:
39  virtual ~TFactory() throw()
40  {
41  if( _instance )
42  {
43  TFactory<S> *savedInstance = _instance;
44  _instance = 0;
45  delete savedInstance;
46  }
47  _instance = 0;
48  };
49 
50  // default parameter foo=0 to workaround vc6 ICE when trying
51  // to compile an explicit template function initialization
52  template <class R>
53  static TFactory<S> *getFactory(R *foo= 0)
54  {
56  if( _instance == 0 )
57  {
58  _instance = new R();
59  }
60  return _instance;
61  }
62 
63  virtual void reset()
64  {
65  if( _instance )
66  {
67  TFactory<S> *savedInstance = _instance;
68  _instance = 0;
69  delete savedInstance;
70  }
71  _instance = 0;
72  };
73 
74  // Set Methods
75  virtual void setParameter( const std::string name, const std::string value) { strParamMap[ name ] = value; };
76  virtual void setParameter( const std::string name, const u32 value) { numParamMap[ name ] = value; };
77  virtual void setMode( const int newMode ) { mode = newMode; };
78 
79  // CONST Methods (get)
80  virtual std::string getParameterString( const std::string name ) const { return strParamMap[ name ]; };
81  virtual u32 getParameterNum( const std::string name ) const {return numParamMap[ name ];};
82  virtual int getMode( ) const { return mode ; };
83 
84  protected:
85  TFactory() : S(), mode(0) {};
86  TFactory( const TFactory<S> &src );
87  TFactory<S> &operator =( const TFactory<S> &src );
88 
89  int mode;
90  mutable std::map< std::string, std::string > strParamMap;
91  mutable std::map< std::string, u32 > numParamMap;
93  };
94 
95  template<class S> TFactory<S> *TFactory<S>::_instance = 0;
96 }
97 
98 // always should be last thing in header file
100 
101 #endif /* FACTORY2_H_ */