Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

devicesystemintf.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                           devicesystemintf.h  -  description
00003                              -------------------
00004     begin                : Mon Nov 17 2003
00005     copyright            : (C) 2003 by Peter Haase
00006     email                : mail@p-haase.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00033 #ifndef DEVICE_SYSTEM_INTF_HEADER
00034 #define DEVICE_SYSTEM_INTF_HEADER
00035 
00036 
00037 
00038 #include <string>
00039 #include <map>
00040 #include <iostream>
00041 
00042 #include <simthetic/misc.h> // required for DEBUG
00043 
00044 
00045 namespace{
00046   //For internal use:
00047   class ParameterWrapper;
00048 
00049 }
00050 
00051 
00052 namespace simth
00053 {
00054 
00055 
00056 class Device;
00057 class ControlInterface;
00058 class name_conflict;
00059 class no_such_parameter;
00060 class type_error;
00061 
00062 
00068 class DeviceSystemIntf 
00069 {
00070   public:
00071 
00074     DeviceSystemIntf();
00075     
00078     virtual ~DeviceSystemIntf();
00079 
00085     virtual void connectInterface(ControlInterface* interface, const std::string& varName)=0;
00086 
00089     virtual Device* device(int deviceNr) = 0;
00090 
00093     virtual const Device* device(int deviceNr) const = 0;
00094 
00108     virtual void queueForProcess(Device *dev) {};
00109 
00120     virtual void stopIteration()
00121       {};
00122 
00127     virtual void stopSimulationPremature()
00128       {};
00129 
00144     template<class T>
00145     void writeParameter(const std::string& parameterName, const T& value) throw (simth::name_conflict);
00146 
00165     template<class T>
00166     T readParameter(const std::string& parameterName) const throw (simth::no_such_parameter, simth::type_error);
00167 
00170     virtual void warningMessage(const std::string& warningMess,int deviceNr) const = 0;
00171 
00172  protected:
00173 
00177     void printParameterList(std::ostream &os) const;
00178 
00179 
00180  private:
00181 
00182     typedef std::map<std::string,ParameterWrapper*>::const_iterator const_map_iter;
00183     typedef std::map<std::string,ParameterWrapper*>::iterator map_iter;
00184     std::map<std::string,ParameterWrapper*> parameterMap;
00185 
00186 
00187 };
00188 
00189 }
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00200 //
00201 // Implementation details that have to be 
00202 // declared/defined here
00203 //
00204 
00205 
00206 
00207 namespace{
00208 
00209 class ParameterWrapper;
00210 
00211 std::ostream& operator<<(std::ostream &os, const ParameterWrapper& pW);
00212 
00213 
00219 class ParameterWrapper
00220 {
00221     friend std::ostream& operator<<(std::ostream &os, const ParameterWrapper& pW);
00222 
00223   protected:
00224     virtual void printParameter(std::ostream& os) const = 0;
00225   public:
00226     ParameterWrapper() {};
00227     virtual ~ParameterWrapper() {};
00228 };
00229 
00234 template<class T>
00235 class ParameterWrapperT : public ParameterWrapper
00236 {
00237   private:
00238     T parameter_;
00239   protected:
00240     virtual void printParameter(std::ostream& os) const;
00241 
00242   public:
00246     ParameterWrapperT(const T& parameter__);
00247 
00249     T parameter() const {return parameter_;}
00250 };
00251 
00252 template<class T>
00253 inline ParameterWrapperT<T>::ParameterWrapperT(const T& parameter__)
00254     : parameter_(parameter__) { }
00255 
00256 template<class T>
00257 inline
00258 void ParameterWrapperT<T>::printParameter(std::ostream& os) const
00259 {
00260   os<<parameter();
00261 }
00262 
00263 } // namespace 
00264 
00265 namespace simth {
00266 
00267 template<class T>
00268 inline void DeviceSystemIntf::writeParameter(const std::string& parameterName, const T& value)
00269 throw (simth::name_conflict)
00270 {
00271   //test if a parameter with that name already exists:
00272   const_map_iter i = parameterMap.find(parameterName);
00273   if(i != parameterMap.end()) {
00274     ParameterWrapper* pw = i->second;
00275     ParameterWrapperT<T>* pwt = dynamic_cast<ParameterWrapperT<T>*>(pw);
00276     if(pwt != NULL) {
00277       if (pwt->parameter() == value) {
00278         // value would be unchanged anyway
00279         return;
00280       }
00281     }
00282     else {
00283       if(DEBUG)
00284         throw simth::name_conflict("DeviceSystemIntf::writeParameter: "
00285                                    "While trying to insert the parameter "
00286                                    "with the name '"+parameterName+
00287                                    "' to the global parameter list, "
00288                                    "a name conflict occurs: a parameter "
00289                                    "with that name already exists");
00290       else
00291         std::cout << "DeviceSystemIntf::writeParameter: Warning: "
00292         "While trying to insert the parameter with the name '"
00293         +parameterName+"' to the global "
00294         "parameter list, a name conflict occurs: a parameter with "
00295         "that name already exists" << std::endl;
00296     }
00297   }
00298 
00299   ParameterWrapper* pw = new ParameterWrapperT<T>(value);
00300   parameterMap[parameterName] = pw;
00301 }
00302 
00303 template<class T>
00304 inline T DeviceSystemIntf::readParameter(const std::string& parameterName) const
00305 throw (simth::no_such_parameter, simth::type_error)
00306 {
00307   const_map_iter i = parameterMap.find(parameterName);
00308   if(i != parameterMap.end()) {
00309     ParameterWrapper* pw = i->second;
00310     ParameterWrapperT<T>* pwt = dynamic_cast<ParameterWrapperT<T>*>(pw);
00311     if(pwt == NULL) {
00312       throw simth::type_error("While trying to read the parameter value '"+parameterName+"' from the global parameter list a type error"
00313                               "occured: the stored type with that name and the type you asked for are different.");
00314     }
00315     return pwt->parameter();
00316   }
00317   else {
00318     throw simth::no_such_parameter("Could not find '"+parameterName+"' in the global parameter list. Hint: use "
00319                                    "DeviceSystemIntf::writeParameter() to insert parameters to that list.");
00320   }
00321 }
00322 
00323 
00324 } // namespace simth
00325 
00326 
00327 
00328 #endif

Generated on Mon Apr 24 21:19:19 2006 for simthetic by  doxygen 1.4.1