cstlwatch.h

00001 //==========================================================================
00002 //  CSTLWATCH.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  WATCH_VECTOR, WATCH_MAP etc macros
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2008 Andras Varga
00013   Copyright (C) 2006-2008 OpenSim Ltd.
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __CSTLWATCH_H
00020 #define __CSTLWATCH_H
00021 
00022 #include <vector>
00023 #include <list>
00024 #include <set>
00025 #include <map>
00026 #include <string>
00027 #include <iostream>
00028 #include <sstream>
00029 #include "cownedobject.h"
00030 #include "cwatch.h"
00031 
00032 NAMESPACE_BEGIN
00033 
00034 
00035 
00036 //
00037 // Internal class
00038 //
00039 class SIM_API cStdVectorWatcherBase : public cWatchBase
00040 {
00041   public:
00042     cStdVectorWatcherBase(const char *name) : cWatchBase(name) {}
00043 
00044     virtual std::string info() const;
00045     virtual std::string detailedInfo() const;
00046     virtual bool supportsAssignment() const {return false;}
00047 
00048     virtual const char *getElemTypeName() const = 0;
00049     virtual int size() const = 0;
00050     virtual std::string at(int i) const = 0;
00051     virtual cClassDescriptor *getDescriptor();
00052 };
00053 
00054 
00055 //
00056 // Internal class
00057 //
00058 template<class T>
00059 class cStdVectorWatcher : public cStdVectorWatcherBase
00060 {
00061   protected:
00062     std::vector<T>& v;
00063     std::string classname;
00064   public:
00065     cStdVectorWatcher(const char *name, std::vector<T>& var) : cStdVectorWatcherBase(name), v(var) {
00066         classname = std::string("std::vector<")+opp_typename(typeid(T))+">";
00067     }
00068     const char *getClassName() const {return classname.c_str();}
00069     virtual const char *getElemTypeName() const {return opp_typename(typeid(T));}
00070     virtual int size() const {return v.size();}
00071     virtual std::string at(int i) const {std::stringstream out; out << v[i]; return out.str();}
00072 };
00073 
00074 template <class T>
00075 void createStdVectorWatcher(const char *varname, std::vector<T>& v)
00076 {
00077     new cStdVectorWatcher<T>(varname, v);
00078 }
00079 
00080 
00081 //
00082 // Internal class
00083 //
00084 template<class T>
00085 class cStdPointerVectorWatcher : public cStdVectorWatcher<T>
00086 {
00087   public:
00088     cStdPointerVectorWatcher(const char *name, std::vector<T>& var) : cStdVectorWatcher<T>(name, var) {}
00089     virtual std::string at(int i) const {std::stringstream out; out << *(this->v[i]); return out.str();}
00090 };
00091 
00092 template <class T>
00093 void createStdPointerVectorWatcher(const char *varname, std::vector<T>& v)
00094 {
00095     new cStdPointerVectorWatcher<T>(varname, v);
00096 }
00097 
00098 //
00099 // Internal class
00100 //
00101 template<class T>
00102 class cStdListWatcher : public cStdVectorWatcherBase
00103 {
00104   protected:
00105     std::list<T>& v;
00106     std::string classname;
00107     mutable typename std::list<T>::iterator it;
00108     mutable int itPos;
00109   public:
00110     cStdListWatcher(const char *name, std::list<T>& var) : cStdVectorWatcherBase(name), v(var) {
00111         itPos=-1;
00112         classname = std::string("std::list<")+opp_typename(typeid(T))+">";
00113     }
00114     const char *getClassName() const {return classname.c_str();}
00115     virtual const char *getElemTypeName() const {return opp_typename(typeid(T));}
00116     virtual int size() const {return v.size();}
00117     virtual std::string at(int i) const {
00118         // std::list doesn't support random access iterator and iteration is slow,
00119         // so we have to use a trick, knowing that Tkenv will call this function with
00120         // i=0, i=1, etc...
00121         if (i==0) {
00122             it=v.begin(); itPos=0;
00123         } else if (i==itPos+1 && it!=v.end()) {
00124             ++it; ++itPos;
00125         } else {
00126             it=v.begin();
00127             for (int k=0; k<i && it!=v.end(); k++) ++it;
00128             itPos=i;
00129         }
00130         if (it==v.end()) {
00131             return std::string("out of bounds");
00132         }
00133         return atIt();
00134     }
00135     virtual std::string atIt() const {
00136         std::stringstream out;
00137         out << (*it);
00138         return out.str();
00139     }
00140 };
00141 
00142 template <class T>
00143 void createStdListWatcher(const char *varname, std::list<T>& v)
00144 {
00145     new cStdListWatcher<T>(varname, v);
00146 }
00147 
00148 
00149 //
00150 // Internal class
00151 //
00152 template<class T>
00153 class cStdPointerListWatcher : public cStdListWatcher<T>
00154 {
00155   public:
00156     cStdPointerListWatcher(const char *name, std::list<T>& var) : cStdListWatcher<T>(name, var) {}
00157     virtual std::string atIt() const {
00158         std::stringstream out;
00159         out << (**this->it);
00160         return out.str();
00161     }
00162 };
00163 
00164 template <class T>
00165 void createStdPointerListWatcher(const char *varname, std::list<T>& v)
00166 {
00167     new cStdPointerListWatcher<T>(varname, v);
00168 }
00169 
00170 
00171 //
00172 // Internal class
00173 //
00174 template<class T>
00175 class cStdSetWatcher : public cStdVectorWatcherBase
00176 {
00177   protected:
00178     std::set<T>& v;
00179     std::string classname;
00180     mutable typename std::set<T>::iterator it;
00181     mutable int itPos;
00182   public:
00183     cStdSetWatcher(const char *name, std::set<T>& var) : cStdVectorWatcherBase(name), v(var) {
00184         itPos=-1;
00185         classname = std::string("std::set<")+opp_typename(typeid(T))+">";
00186     }
00187     const char *getClassName() const {return classname.c_str();}
00188     virtual const char *getElemTypeName() const {return opp_typename(typeid(T));}
00189     virtual int size() const {return v.size();}
00190     virtual std::string at(int i) const {
00191         // std::set doesn't support random access iterator and iteration is slow,
00192         // so we have to use a trick, knowing that Tkenv will call this function with
00193         // i=0, i=1, etc...
00194         if (i==0) {
00195             it=v.begin(); itPos=0;
00196         } else if (i==itPos+1 && it!=v.end()) {
00197             ++it; ++itPos;
00198         } else {
00199             it=v.begin();
00200             for (int k=0; k<i && it!=v.end(); k++) ++it;
00201             itPos=i;
00202         }
00203         if (it==v.end()) {
00204             return std::string("out of bounds");
00205         }
00206         return atIt();
00207     }
00208     virtual std::string atIt() const {
00209         std::stringstream out;
00210         out << (*it);
00211         return out.str();
00212     }
00213 };
00214 
00215 template <class T>
00216 void createStdSetWatcher(const char *varname, std::set<T>& v)
00217 {
00218     new cStdSetWatcher<T>(varname, v);
00219 }
00220 
00221 
00222 //
00223 // Internal class
00224 //
00225 template<class T>
00226 class cStdPointerSetWatcher : public cStdSetWatcher<T>
00227 {
00228   public:
00229     cStdPointerSetWatcher(const char *name, std::set<T>& var) : cStdSetWatcher<T>(name, var) {}
00230     virtual std::string atIt() const {
00231         std::stringstream out;
00232         out << (**this->it);
00233         return out.str();
00234     }
00235 };
00236 
00237 template <class T>
00238 void createStdPointerSetWatcher(const char *varname, std::set<T>& v)
00239 {
00240     new cStdPointerSetWatcher<T>(varname, v);
00241 }
00242 
00243 
00244 //
00245 // Internal class
00246 //
00247 template<class KeyT, class ValueT, class CmpT>
00248 class cStdMapWatcher : public cStdVectorWatcherBase
00249 {
00250   protected:
00251     std::map<KeyT,ValueT,CmpT>& m;
00252     mutable typename std::map<KeyT,ValueT,CmpT>::iterator it;
00253     mutable int itPos;
00254     std::string classname;
00255   public:
00256     cStdMapWatcher(const char *name, std::map<KeyT,ValueT,CmpT>& var) : cStdVectorWatcherBase(name), m(var) {
00257         itPos=-1;
00258         classname = std::string("std::map<")+opp_typename(typeid(KeyT))+","+opp_typename(typeid(ValueT))+">";
00259     }
00260     const char *getClassName() const {return classname.c_str();}
00261     virtual const char *getElemTypeName() const {return "struct pair<*,*>";}
00262     virtual int size() const {return m.size();}
00263     virtual std::string at(int i) const {
00264         // std::map doesn't support random access iterator and iteration is slow,
00265         // so we have to use a trick, knowing that Tkenv will call this function with
00266         // i=0, i=1, etc...
00267         if (i==0) {
00268             it=m.begin(); itPos=0;
00269         } else if (i==itPos+1 && it!=m.end()) {
00270             ++it; ++itPos;
00271         } else {
00272             it=m.begin();
00273             for (int k=0; k<i && it!=m.end(); k++) ++it;
00274             itPos=i;
00275         }
00276         if (it==m.end()) {
00277             return std::string("out of bounds");
00278         }
00279         return atIt();
00280     }
00281     virtual std::string atIt() const {
00282         std::stringstream out;
00283         out << it->first << " ==> " << it->second;
00284         return out.str();
00285     }
00286 };
00287 
00288 template <class KeyT, class ValueT, class CmpT>
00289 void createStdMapWatcher(const char *varname, std::map<KeyT,ValueT,CmpT>& m)
00290 {
00291     new cStdMapWatcher<KeyT,ValueT,CmpT>(varname, m);
00292 }
00293 
00294 
00295 //
00296 // Internal class
00297 //
00298 template<class KeyT, class ValueT, class CmpT>
00299 class cStdPointerMapWatcher : public cStdMapWatcher<KeyT,ValueT,CmpT>
00300 {
00301   public:
00302     cStdPointerMapWatcher(const char *name, std::map<KeyT,ValueT,CmpT>& var) : cStdMapWatcher<KeyT,ValueT,CmpT>(name, var) {}
00303     virtual std::string atIt() const {
00304         std::stringstream out;
00305         out << this->it->first << "  ==>  " << *(this->it->second);
00306         return out.str();
00307     }
00308 };
00309 
00310 template<class KeyT, class ValueT, class CmpT>
00311 void createStdPointerMapWatcher(const char *varname, std::map<KeyT,ValueT,CmpT>& m)
00312 {
00313     new cStdPointerMapWatcher<KeyT,ValueT,CmpT>(varname, m);
00314 }
00315 
00316 
00317 
00323 
00329 #define WATCH_VECTOR(variable)      createStdVectorWatcher(#variable,(variable))
00330 
00336 #define WATCH_PTRVECTOR(variable)   createStdPointerVectorWatcher(#variable,(variable))
00337 
00343 #define WATCH_LIST(variable)        createStdListWatcher(#variable,(variable))
00344 
00350 #define WATCH_PTRLIST(variable)     createStdPointerListWatcher(#variable,(variable))
00351 
00357 #define WATCH_SET(variable)        createStdSetWatcher(#variable,(variable))
00358 
00364 #define WATCH_PTRSET(variable)     createStdPointerSetWatcher(#variable,(variable))
00365 
00371 #define WATCH_MAP(m)         createStdMapWatcher(#m,(m))
00372 
00378 #define WATCH_PTRMAP(m)      createStdPointerMapWatcher(#m,(m))
00379 
00380 
00381 NAMESPACE_END
00382 
00383 
00384 #endif
00385 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3