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 
00088     enum process_signal {
00097        READY_TO_PROCESS,
00098        /* Queues a device for refreshing. */
00099        // REFRESH,
00110        STOP_ITERATION, 
00116        STOP_SIMULATION_PREMATURE
00117     };
00118 
00121     DeviceSystemIntf();
00122     
00125     virtual ~DeviceSystemIntf();
00126 
00132     virtual void connectInterface(ControlInterface* interface, const std::string& varName)=0;
00133 
00136     virtual Device* device(int deviceNr) = 0;
00137 
00140     virtual const Device* device(int deviceNr) const = 0;
00141 
00154     virtual void acceptSignal(Device* dev, process_signal sig) = 0;
00155 
00170     template<class T>
00171     void writeParameter(const std::string& parameterName, const T& value) throw (simth::name_conflict);
00172 
00191     template<class T>
00192     T readParameter(const std::string& parameterName) const throw (simth::no_such_parameter, simth::type_error);
00193 
00196     virtual void warningMessage(const std::string& warningMess,int deviceNr) const = 0;
00197 
00198  protected:
00199 
00203     void printParameterList(std::ostream &os) const;
00204 
00205 
00206  private:
00207 
00208     typedef std::map<std::string,ParameterWrapper*>::const_iterator const_map_iter;
00209     typedef std::map<std::string,ParameterWrapper*>::iterator map_iter;
00210     std::map<std::string,ParameterWrapper*> parameterMap;
00211 
00212 
00213 };
00214 
00215 }
00216 
00217 
00218 
00219 
00220 
00221 
00222 
00223 
00224 
00226 //
00227 // Implementation details that have to be 
00228 // declared/defined here
00229 //
00230 
00231 
00232 
00233 namespace{
00234 
00235 class ParameterWrapper;
00236 
00237 std::ostream& operator<<(std::ostream &os, const ParameterWrapper& pW);
00238 
00239 
00245 class ParameterWrapper
00246 {
00247     friend std::ostream& operator<<(std::ostream &os, const ParameterWrapper& pW);
00248 
00249   protected:
00250     virtual void printParameter(std::ostream& os) const = 0;
00251   public:
00252     ParameterWrapper() {};
00253     virtual ~ParameterWrapper() {};
00254 };
00255 
00260 template<class T>
00261 class ParameterWrapperT : public ParameterWrapper
00262 {
00263   private:
00264     T parameter_;
00265   protected:
00266     virtual void printParameter(std::ostream& os) const;
00267 
00268   public:
00272     ParameterWrapperT(const T& parameter__);
00273 
00275     T parameter() const {return parameter_;}
00276 };
00277 
00278 template<class T>
00279 inline ParameterWrapperT<T>::ParameterWrapperT(const T& parameter__)
00280     : parameter_(parameter__) { }
00281 
00282 template<class T>
00283 inline
00284 void ParameterWrapperT<T>::printParameter(std::ostream& os) const
00285 {
00286   os<<parameter();
00287 }
00288 
00289 } // namespace 
00290 
00291 namespace simth {
00292 
00293 template<class T>
00294 inline void DeviceSystemIntf::writeParameter(const std::string& parameterName, const T& value)
00295 throw (simth::name_conflict)
00296 {
00297   //test if a parameter with that name already exists:
00298   const_map_iter i = parameterMap.find(parameterName);
00299   if(i != parameterMap.end()) {
00300     ParameterWrapper* pw = i->second;
00301     ParameterWrapperT<T>* pwt = dynamic_cast<ParameterWrapperT<T>*>(pw);
00302     if(pwt != NULL) {
00303       if (pwt->parameter() == value) {
00304         // value would be unchanged anyway
00305         return;
00306       }
00307     }
00308     else {
00309       if(DEBUG)
00310         throw simth::name_conflict("DeviceSystemIntf::writeParameter: "
00311                                    "While trying to insert the parameter "
00312                                    "with the name '"+parameterName+
00313                                    "' to the global parameter list, "
00314                                    "a name conflict occurs: a parameter "
00315                                    "with that name already exists");
00316       else
00317         std::cout << "DeviceSystemIntf::writeParameter: Warning: "
00318         "While trying to insert the parameter with the name '"
00319         +parameterName+"' to the global "
00320         "parameter list, a name conflict occurs: a parameter with "
00321         "that name already exists" << std::endl;
00322     }
00323   }
00324 
00325   ParameterWrapper* pw = new ParameterWrapperT<T>(value);
00326   parameterMap[parameterName] = pw;
00327 }
00328 
00329 template<class T>
00330 inline T DeviceSystemIntf::readParameter(const std::string& parameterName) const
00331 throw (simth::no_such_parameter, simth::type_error)
00332 {
00333   const_map_iter i = parameterMap.find(parameterName);
00334   if(i != parameterMap.end()) {
00335     ParameterWrapper* pw = i->second;
00336     ParameterWrapperT<T>* pwt = dynamic_cast<ParameterWrapperT<T>*>(pw);
00337     if(pwt == NULL) {
00338       throw simth::type_error("While trying to read the parameter value '"+parameterName+"' from the global parameter list a type error"
00339                               "occured: the stored type with that name and the type you asked for are different.");
00340     }
00341     return pwt->parameter();
00342   }
00343   else {
00344     throw simth::no_such_parameter("Could not find '"+parameterName+"' in the global parameter list. Hint: use "
00345                                    "DeviceSystemIntf::writeParameter() to insert parameters to that list.");
00346   }
00347 }
00348 
00349 
00350 } // namespace simth
00351 
00352 
00353 
00354 #endif

Generated on Mon Aug 15 13:54:26 2005 for simthetic by  doxygen 1.4.1