library of assembled shared sources

http://lass.cocamware.com

proxy_o_stream.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @class lass::io::ProxyOStream
00046  *  @brief A proxy output stream can distribute output to multiple destination streams.
00047  *  @author BdG
00048  *  @date 2003
00049  *
00050  *  You can add multiple destination streams to a proxy stream, and all output to the
00051  *  proxy stream will be redirected to all these destinations.
00052  *
00053  *  You can also assign a filter to the proxy, and only messages with levels set in the
00054  *  filter will pass.  e.g. if you set the filter of the proxy as
00055  *  proxy.setFilter() = 0, then only messages of warning or error levels will
00056  *  pass.
00057  *
00058  *  How do you tell what level your message has?  Simple, before you use the insertor,
00059  *  you call the function operator on the stream with the correct level.  This returns
00060  *  a lock on the proxy, and now you send the message to the lock (by the insertor)
00061  *  Wheter this message will pass or not, is determined by the lock.
00062  *  At the end of the lifetime of the lock, the proxy is also flushed.
00063  *
00064  *  proxy(Warning) << "This is a warning: " << theWarning;
00065  *
00066  *  In the above example, the proxy is locked in warning level, and the string and
00067  *  theWarning is passed to the lock.  At the end, the lock flushes te proxy.
00068  *
00069  *  If you don't use the function operator to set the message level, the level is implicit
00070  *  set to ProxyOStream::Note.
00071  *
00072  *  OK, seriously, i have no longer any idea why we need this ... get rid of it?
00073  */
00074 
00075 
00076 
00077 #ifndef LASS_GUARDIAN_OF_INCLUSION_IO_PROXY_O_STREAM_H
00078 #define LASS_GUARDIAN_OF_INCLUSION_IO_PROXY_O_STREAM_H
00079 
00080 #include "io_common.h"
00081 #include "../util/bit_manip.h"
00082 
00083 #if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
00084 #   pragma warning(push)
00085 #   pragma warning(disable: 4267) // 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data
00086 #endif
00087 
00088 namespace lass
00089 {
00090 
00091 namespace io
00092 {
00093 
00094 class LASS_DLL ProxyOStream
00095 {
00096 public:
00097 
00098     typedef unsigned TMask;
00099     static const TMask acceptAll = static_cast<TMask>(-1);
00100 
00101     // innerclasses
00102 
00103     class Lock
00104     {
00105     public:
00106         /** Lock (or don't lock if iProxy == 0) a proxy.
00107          */
00108         Lock( ProxyOStream* iProxy, TMask iMessage ): proxy_(iProxy), messageMask_(iMessage) {}
00109 
00110         /** The copy constructor passes the lock on the proxy to the copy.
00111          *  The pointer is passed to the copy, so that this copy can continue the job of the
00112          *  original.  Since the original looses it's proxy pointer, it won't flush it on
00113          *  destruction.
00114          *  @warning DO NOT COPY LOCKS YOURSELF.  It's no good, well, unless you know what you're
00115          *           doing.  But be warned: it might not do what you expect.
00116          */
00117         Lock( const ProxyOStream::Lock& iOther ): proxy_(iOther.proxy_), messageMask_(iOther.messageMask_)
00118         {
00119             iOther.proxy_ = 0;
00120             iOther.messageMask_ = 0;
00121         }
00122 
00123         /** On the end of the lock, flush the proxy.
00124          */
00125         ~Lock()
00126         {
00127             if (proxy_)
00128             {
00129                 proxy_->flush();
00130             }
00131         }
00132 
00133         /** distribute input over all destination streams.
00134         *  Only distribute input if it's still locking a proxy (i.e. if proxy_ != Null).
00135         */
00136         template <typename T> 
00137         Lock& operator<< ( const T& iIn )
00138         {
00139             if (proxy_)
00140             {
00141                 ProxyOStream::TDestinations::iterator dit;
00142                 for (dit = proxy_->destinations_.begin(); dit != proxy_->destinations_.end(); ++dit)
00143                 {
00144                     if (util::checkMaskedSome(dit->second, messageMask_))
00145                     {
00146                         LASS_ENFORCE_STREAM(*(dit->first)) << iIn;
00147                     }
00148                 }
00149             }
00150             return *this;
00151         }
00152 
00153         /** distribute std manipulators over all destination streams.
00154         *  Only distribute input if it's still locking a proxy (i.e. if proxy_ != Null).
00155         */
00156         Lock& operator<<(std::ostream& (*iIn) (std::ostream&))
00157         {
00158             if (proxy_)
00159             {
00160                 ProxyOStream::TDestinations::iterator dit;
00161                 for (dit = proxy_->destinations_.begin(); dit != proxy_->destinations_.end(); ++dit)
00162                 {
00163                     if (util::checkMaskedSome(dit->second, messageMask_))
00164                     {
00165                         LASS_ENFORCE_STREAM(*(dit->first)) << iIn;
00166                     }
00167                 }
00168             }
00169             return *this;
00170         }
00171 
00172     private:
00173         Lock& operator=( const Lock& /*iOther*/ ) { LASS_ASSERT(false); return *this; }
00174         mutable ProxyOStream* proxy_;
00175         mutable TMask messageMask_;
00176     };
00177 
00178 
00179     // structors & methods
00180 
00181     //ProxyOStream();
00182     ProxyOStream( std::ostream* iDestination = &std::cout, TMask iFilterMask = acceptAll);
00183 
00184     void add( std::ostream* iDestination, TMask iFilterMask = acceptAll );
00185     void remove( std::ostream* iDestination );
00186 
00187     const TMask filter( std::ostream* iDestination ) const;
00188     void setFilter( std::ostream* iDestination, TMask iFilterMask  );
00189 
00190     Lock operator()( TMask iFilterMask = acceptAll );
00191 
00192     /** Lock proxy stream for output on 'acceptAll' and distribute input.
00193      *  This command will give command to a lock, as if the message mask is acceptAll.
00194      *  This has the same effect as (*this)(acceptAll) << iIn;
00195      */
00196     template <typename T> Lock operator<< ( const T& iIn )
00197     {
00198         Lock result(this, acceptAll);
00199         result << iIn;
00200         return result;
00201     }
00202 
00203     /** Lock proxy stream for output on 'acceptAll' and distribute the std manipulator.
00204      *  This command will give command to a lock, as if the message mask is acceptAll.
00205      *  This has the same effect as (*this)(acceptAll) << iIn;
00206      */
00207     Lock operator<< ( std::ostream& (*iIn) (std::ostream&) )
00208     {
00209         Lock result(this, acceptAll);
00210         result << iIn;
00211         return result;
00212     }
00213 
00214     void flush();
00215 
00216 private:
00217 
00218     friend class Lock;
00219 
00220     typedef std::map<std::ostream*, TMask> TDestinations;
00221 
00222     TDestinations destinations_;
00223 };
00224 
00225 }
00226 
00227 }
00228 
00229 #if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
00230 #   pragma warning(pop)
00231 #endif
00232 
00233 #endif

Generated on Mon Nov 10 14:21:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo