QGpCoreTools/AbstractStream.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created: 2010-09-11
00022 **  Authors:
00023 **    Marc Wathelet (LGIT, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef ABSTRACTSTREAM_H
00028 #define ABSTRACTSTREAM_H
00029 
00030 #include <QtCore>
00031 
00032 #include "SharedObject.h"
00033 #include "QGpCoreToolsDLLExport.h"
00034 
00035 namespace QGpCoreTools {
00036 
00037   class QGPCORETOOLS_EXPORT AbstractStream: public SharedObject
00038   {
00039   public:
00040     AbstractStream();
00041     virtual ~AbstractStream();
00042 
00043     // rtti not available under Windows, let identification of stream type
00044     enum ClassId {StandardId=0, StringId, FileId, LogViewId, CustomId};
00045     virtual ClassId classId() const=0;
00046 
00047     void setPrefix(const QString & prefix);
00048 
00049     void freeze(bool b) {_frozen=b;}
00050     bool isFrozen() const {return _frozen;}
00051 
00052     void setNext(AbstractStream * s) {_next=s;}
00053     AbstractStream * next() const {return _next;}
00054 
00055     void setPrevious(AbstractStream * s);
00056     AbstractStream * previous() const {return _previous;}
00057 
00058     AbstractStream& operator<<(const QString& val);
00059     AbstractStream& operator<<(int val) {return operator<<(QString::number(val));}
00060     AbstractStream& operator<<(double val) {return operator<<(QString::number(val));}
00061     AbstractStream& operator<<(const char * val) {return operator<<(QString(val));}
00062     AbstractStream& operator<<(QChar val) {return operator<<(QString(val));}
00063     AbstractStream& operator<<(char val) {return operator<<(QString(QChar::fromAscii(val)));}
00064     void endl();
00065     void flush();
00066   protected:
00067     virtual void sendToStream(const QString& val)=0;
00068     virtual void flushStream()=0;
00069 
00070     void lock() {_mutex.lock();}
00071     void unlock() {_mutex.unlock();}
00072   private:
00073     inline QString timeStampedPrefix();
00074 
00075     AbstractStream * _next, * _previous;
00076     bool _frozen, _lineOpen;
00077     enum PrefixType {NoPrefix, String, TimeStamp};
00078     PrefixType _prefixType;
00079     QString _prefix1, _prefix2;
00080     QMutex _mutex;
00081   };
00082 
00083   typedef AbstractStream& (*AbstractStreamFunction)(AbstractStream&); // manipulator function
00084 
00085   inline AbstractStream& operator<<(AbstractStream &s, AbstractStreamFunction f)
00086   {
00087     return (*f)(s);
00088   }
00089 
00090   QGPCORETOOLS_EXPORT AbstractStream& endl(AbstractStream &s);
00091   QGPCORETOOLS_EXPORT AbstractStream& flush(AbstractStream &s);
00092 
00093 } // namespace QGpCoreTools
00094 
00095 #endif // ABSTRACTSTREAM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines