# cenvir.h

    00001 //==========================================================================
    00002 //  CENVIR.H - part of
    00003 //
    00004 //                     OMNeT++/OMNEST
    00005 //            Discrete System Simulation in C++
    00006 //
    00007 //
    00008 //  Declaration of the following classes:
    00009 //    cEnvir    : user interface class
    00010 //
    00011 //==========================================================================
    00012
    00013 /*--------------------------------------------------------------*
    00014   Copyright (C) 1992-2005 Andras Varga
    00015
    00016   This file is distributed WITHOUT ANY WARRANTY. See the file
    00017   `license' for details on this and other legal matters.
    00018 *--------------------------------------------------------------*/
    00019
    00020 #ifndef __CENVIR_H
    00021 #define __CENVIR_H
    00022
    00023 #include <sstream>
    00024 #include <iostream>
    00025 #include "defs.h"
    00026
    00027 class cObject;
    00028 class cMessage;
    00029 class cGate;
    00030 class cModule;
    00031 class cSimpleModule;
    00032 class cStatistic;
    00033 class cRNG;
    00034 class cConfiguration;
    00035 class cXMLElement;
    00036 class TOmnetApp;
    00037 class cEnvir;
    00038
    00039 using std::endl;
    00040
    00041 //
    00042 // std::streambuf used by cEnvir's ostream base. It redirects writes to
    00043 // cEnvir::sputn(s,n). Flush is done at the end of each line, meanwhile
    00044 // writes are buffered in a stringbuf.
    00045 //
    00046 template <class E, class T = std::char_traits<E> >
    00047     class basic_evbuf : public std::basic_stringbuf<E,T> {
    00048 public:
    00049     basic_evbuf(cEnvir *ev) : _ev(ev) {}
    00050     // gcc>=3.4 needs either this-> or std::basic_stringbuf<E,T>:: in front of pptr()/pbase()
    00051     bool isempty() {return this->pptr()==this->pbase();}
    00052 protected:
    00053     virtual int sync();
    00054     virtual std::streamsize xsputn(const E *s, std::streamsize n) {
    00055 #if !defined(_MSC_VER) || _MSC_VER>1200
    00056         std::streamsize r = std::basic_stringbuf<E,T>::xsputn(s,n);
    00057 #else
    00058         // **HACK** The above line (calling the same function in the base class)
    00059         // doesn't work with MSVC6.0: it gives a weird compilation error.
    00060         // So as a workaround I copied here the body of the original xsputn()
    00061         // function from the MSVC header, VC98/Include/streambuf.
    00062         const E *_S = s;
    00063         std::streamsize _N = n;
    00064         std::streamsize _M, _Ns;
    00065         for (_Ns = 0; 0 < _N; )
    00066             if (pptr() != 0 && 0 < (_M = epptr() - pptr()))
    00067                 {if (_N < _M)
    00068                     _M = _N;
    00069                 T::copy(pptr(), _S, _M);
    00070                 _S += _M, _Ns += _M, _N -= _M, pbump(_M); }
    00071             else if (T::eq_int_type(T::eof(),
    00072                 overflow(T::to_int_type(*_S))))
    00073                 break;
    00074             else
    00075                 ++_S, ++_Ns, --_N;
    00076         std::streamsize r = _Ns;
    00077 #endif
    00078         for(;n>0;n--,s++)
    00079             if (*s=='\n')
    00080                {sync();break;}
    00081         return r;
    00082     }
    00083     cEnvir *_ev;
    00084 };
    00085
    00086 typedef basic_evbuf<char> evbuf;
    00087
    00088
    00107 ENVIR_API extern cEnvir ev;
    00108
    00109 ENVIR_API bool memoryIsLow();
    00110
    00111
    00112
    00143 class ENVIR_API cEnvir : public std::ostream
    00144 {
    00145   public:
    00146     // internal variables
    00147     TOmnetApp *app;  // the application" instance
    00148     bool disable_tracing;
    00149     bool debug_on_errors;
    00150   private:
    00151     // further internal vars
    00152     evbuf ev_buf;
    00153     bool isgui;
    00154
    00155   public:
    00156     // internal: writes the first n characters of string s.
    00157     // evbuf (the streambuf underlying cEnvir's ostream base class)
    00158     // writes via this function.
    00159     void sputn(const char *s, int n);
    00160
    00161     // internal: flushes the internal stream buffer by terminating last line if needed
    00162     // note: exploits the fact that evbuf does sync() on "\n"'s
    00163     void flushlastline() {if (!ev_buf.isempty()) ev_buf.sputn("\n",1);}
    00164
    00165   public:
    00171
    00175     cEnvir();
    00176
    00180     ~cEnvir();
    00182
    00185
    00190     void setup(int ac, char *av[]);
    00191
    00197     int run();
    00198
    00202     void shutdown();
    00204
    00207
    00214     void objectDeleted(cObject *object);
    00215
    00229     void messageSent(cMessage *msg, cGate *directToGate=NULL);
    00230
    00234     void moduleReparented(cModule *module, cModule *oldparent);
    00235
    00243     void messageDelivered(cMessage *msg);
    00244
    00249     void breakpointHit(const char *lbl, cSimpleModule *mod);
    00250
    00256     void moduleMethodCalled(cModule *from, cModule *to, const char *method);
    00257
    00263     void moduleCreated(cModule *newmodule);
    00264
    00276     void moduleDeleted(cModule *module);
    00277
    00282     void connectionCreated(cGate *srcgate);
    00283
    00288     void connectionRemoved(cGate *srcgate);
    00289
    00294     void displayStringChanged(cGate *gate);
    00295
    00299     // TBD: how does module name change propagate to gui?
    00300     void displayStringChanged(cModule *submodule);
    00301
    00305     void backgroundDisplayStringChanged(cModule *parentmodule);
    00306
    00311     void undisposedObject(cObject *obj);
    00313
    00316
    00322     std::string getParameter(int run_no, const char *parname);
    00323
    00328     bool getParameterUseDefault(int run_no, const char *parname);
    00329
    00339     bool isModuleLocal(cModule *parentmod, const char *modname, int index);
    00340
    00362     cXMLElement *getXMLDocument(const char *filename, const char *path=NULL);
    00363
    00368     unsigned extraStackForEnvir();
    00369
    00377     cConfiguration *config();
    00379
    00382
    00388     bool isGUI()  {return isgui;}
    00389
    00406     bool disabled() {return disable_tracing;}
    00407
    00411     void bubble(cModule *mod, const char *text);
    00412
    00417     void printfmsg(const char *fmt,...);
    00418
    00426     void printf(const char *fmt="\n",...);
    00427
    00432     void puts(const char *s);
    00433
    00440     cEnvir& flush();
    00441
    00446     std::string gets(const char *prompt, const char *defaultreply=NULL);
    00447
    00451     bool gets(const char *prompt, char *buf, int len=255);
    00452
    00458     bool askYesNo(const char *msgfmt,...);
    00460
    00463
    00468     int numRNGs();
    00469
    00473     cRNG *rng(int k);
    00474
    00479     void getRNGMappingFor(cModule *mod);
    00481
    00491
    00498     void *registerOutputVector(const char *modulename, const char *vectorname, int tuple);
    00499
    00503     void deregisterOutputVector(void *vechandle);
    00504
    00510     bool recordInOutputVector(void *vechandle, simtime_t t, double value);
    00511
    00517     bool recordInOutputVector(void *vechandle, simtime_t t, double value1, double value2);
    00519
    00529
    00533     void recordScalar(cModule *module, const char *name, double value);
    00535
    00543
    00547     std::ostream *getStreamForSnapshot();
    00548
    00552     void releaseStreamForSnapshot(std::ostream *os);
    00554
    00560     int argCount();
    00561
    00565     char **argVector();
    00566
    00570     int getParsimProcId();
    00571
    00576     int getParsimNumPartitions();
    00577
    00581     unsigned long getUniqueNumber();
    00582
    00596     bool idle();
    00598 };
    00599
    00600 template <class E, class T>
    00601 int basic_evbuf<E,T>::sync() {
    00602     _ev->sputn(this->pbase(), this->pptr()-this->pbase());
    00603     setp(this->pbase(),this->epptr());
    00604     return 0;
    00605 }
    00606
    00607 #endif