00001 /*************************************************************************** 00002 LOW_deviceFactory.h - description 00003 ------------------- 00004 begin : Mon Aug 5 2002 00005 copyright : (C) 2002 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_DEVICEFACTORY_H 00019 #define LOW_DEVICEFACTORY_H 00020 00021 00022 #include "LOW_exception.h" 00023 #include "LOW_device.h" 00024 #include "LOW_deviceIDRaw.h" 00025 00026 00027 /** Factory class for creating instances of classes derived from LOW_device. 00028 00029 Any class derived from LOW_device must register with this factory. 00030 After that, this factory can create "the right" instance of LOW_device subclasses 00031 on the fly. 00032 00033 To learn how to statically register at program initialization, see an already implemented 00034 subclass, e.g. LOW_devDS1820. 00035 00036 @author Harald Roelle 00037 */ 00038 class LOW_deviceFactory { 00039 00040 //======================================================================================= 00041 public: 00042 00043 //===================================================================================== 00044 // 00045 // exceptions 00046 // 00047 00048 /** Exception base class for all exceptions thrown by LOW_deviceFactory. */ 00049 class_DERIVE_FROM_EXCEPTION( deviceFactory_error, LOW_exception); 00050 00051 00052 //===================================================================================== 00053 // 00054 // type definitions 00055 // 00056 00057 /** Function type for static pseudo constructors. */ 00058 typedef LOW_device* (*newInstanceFkt_t)( LOW_netSegment&, const LOW_deviceID&); 00059 00060 00061 //===================================================================================== 00062 // 00063 // static methods 00064 // 00065 00066 /** Create new dynamic instance of specific device class. 00067 The created instance is disposable with the "delete" operator. 00068 00069 @param inSegment Reference to network segment the device is on. 00070 @param inDevID Reference to device's ID. 00071 @return New dynamic instance of specific device class. 00072 @throw deviceFactory_error Thrown when device type is not registered. 00073 */ 00074 static LOW_device* new_SpecificDevice( LOW_netSegment &inSegment, const LOW_deviceID &inDevID); 00075 00076 00077 /** Register a static pseudo constructor. 00078 00079 @param inFamCode Family code of the specific class. 00080 @param inPseudoCnstr Static pseudo constructor to register. 00081 @throw deviceFactory_error Thrown when family type is already registered. 00082 */ 00083 static void registerSpecificCtor( const LOW_deviceIDRaw::devFamCode_t inFamCode, newInstanceFkt_t inPseudoCnstr); 00084 00085 00086 00087 //======================================================================================= 00088 private: 00089 00090 //===================================================================================== 00091 // 00092 // type definitions 00093 // 00094 00095 /** Map type for registered family types and constructors. */ 00096 typedef std::map< LOW_deviceIDRaw::devFamCode_t, newInstanceFkt_t> ctorFktMap_t; 00097 00098 00099 //===================================================================================== 00100 // 00101 // static attributes 00102 // 00103 00104 /** Map holding registered family types and associated constructors. 00105 <B>Note:</B> Singleton design pattern is important here, because order of static 00106 initialization produced by the compiler cannot be assured. 00107 */ 00108 static ctorFktMap_t *deviceCtorsSingleton; 00109 00110 /** Pseudo constructor for unknown device types. 00111 This constructor is used when no specific constructor is found. 00112 */ 00113 static newInstanceFkt_t unknownDevCtor; 00114 00115 00116 //===================================================================================== 00117 // 00118 // constructors 00119 // 00120 00121 /** Constructor. 00122 Private to prevent instaciation as this is a static factory. 00123 */ 00124 LOW_deviceFactory(); 00125 00126 /** Destructor. 00127 Private to prevent instaciation as this is a static factory. 00128 */ 00129 virtual ~LOW_deviceFactory(); 00130 00131 }; 00132 00133 #endif