Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

FactoryImpl2.h

Go to the documentation of this file.
00001 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:
00002 /*
00003  * Copyright (C) 2005 Dell Inc.
00004  *  by Michael Brown <Michael_E_Brown@dell.com>
00005  * Licensed under the Open Software License version 2.1
00006  *
00007  * Alternatively, you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation; either version 2 of the License,
00010  * or (at your option) any later version.
00011 
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00015  * See the GNU General Public License for more details.
00016  */
00017 
00018 
00019 #ifndef FACTORY2_H_
00020 #define FACTORY2_H_
00021 
00022 #define LIBSMBIOS_SOURCE
00023 #include "smbios/compat.h"
00024 
00025 #include <map>
00026 
00027 #include "smbios/types.h"
00028 
00029 // abi_prefix should be last header included before declarations
00030 #include "smbios/config/abi_prefix.hpp"
00031 
00032 namespace factory
00033 {
00034 
00035     template <class S>
00036     class TFactory : public S
00037     {
00038     public:
00039         virtual ~TFactory() throw() 
00040         {
00041             if( _instance )
00042             {
00043                 TFactory<S> *savedInstance = _instance;
00044                 _instance = 0;
00045                 delete savedInstance;
00046             }
00047             _instance = 0;
00048         };
00049 
00050         // default parameter foo=0 to workaround vc6 ICE when trying
00051         // to compile an explicit template function initialization
00052         template <class R>
00053         static TFactory<S> *getFactory(R *foo= 0) 
00054         {  
00055             UNREFERENCED_PARAMETER(foo);
00056             if( _instance == 0 )
00057             {
00058                 _instance = new R();
00059             }
00060             return _instance;
00061         }
00062 
00063         virtual void reset() 
00064         {
00065             if( _instance )
00066             {
00067                 TFactory<S> *savedInstance = _instance;
00068                 _instance = 0;
00069                 delete savedInstance;
00070             }
00071             _instance = 0;
00072         };
00073 
00074         // Set Methods
00075         virtual void setParameter( const std::string name, const std::string value) { strParamMap[ name ] = value; };
00076         virtual void setParameter( const std::string name, const u32 value) { numParamMap[ name ] = value; };
00077         virtual void setMode( const int newMode ) { mode = newMode; };
00078 
00079         // CONST Methods (get)
00080         virtual std::string getParameterString( const std::string name ) const { return strParamMap[ name ]; };
00081         virtual u32 getParameterNum( const std::string name ) const {return numParamMap[ name ];};
00082         virtual int getMode( ) const { return mode ; };
00083 
00084     protected:
00085         TFactory() : S(), mode(0) {};
00086         TFactory( const TFactory<S> &src );
00087         TFactory<S> &operator =( const TFactory<S> &src );
00088 
00089         int mode;
00090         mutable std::map< std::string, std::string > strParamMap;
00091         mutable std::map< std::string, u32 > numParamMap;
00092         static TFactory<S> * _instance;
00093     };
00094 
00095     template<class S> TFactory<S> *TFactory<S>::_instance = 0;
00096 }
00097 
00098 // always should be last thing in header file
00099 #include "smbios/config/abi_suffix.hpp"
00100 
00101 #endif /* FACTORY2_H_ */

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5