blocxx

IstreamBufIterator.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 #ifndef BLOCXX_ISTREAMBUFITERATOR_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_ISTREAMBUFITERATOR_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #if defined(BLOCXX_HAVE_ISTREAM)
00042 #include <istream>
00043 #else
00044 #include <iostream>
00045 #endif
00046 
00047 
00048 namespace BLOCXX_NAMESPACE
00049 {
00050 
00056 class IstreamBufIterator
00057 {
00058 public:
00059    class proxy
00060    {
00061       friend class IstreamBufIterator;
00062    public:
00063       char operator*() {  return m_keep; }
00064    private:
00065       proxy(char c) : m_keep(c) {}
00066       char m_keep;
00067    };
00068    IstreamBufIterator() : m_sbuf(0) {}
00069    IstreamBufIterator(std::istream& s) : m_sbuf(s.rdbuf()->sgetc() == EOF ? 0 : s.rdbuf()) {}
00070    IstreamBufIterator(std::streambuf* b) : m_sbuf(b->sgetc() == EOF ? 0 : b) {}
00071    char operator*() const
00072    {
00073       if (m_sbuf)
00074       {
00075          return (m_sbuf->sgetc());
00076       }
00077       else
00078       {
00079          return 0;
00080       }
00081    }
00082    IstreamBufIterator& operator++()
00083    {
00084       m_sbuf->sbumpc();
00085       if (m_sbuf->sgetc() == EOF)
00086       {
00087          m_sbuf = 0;
00088       }
00089       return *this;
00090    }
00091    proxy operator++(int)
00092    {
00093       proxy temp(m_sbuf->sgetc());
00094       m_sbuf->sbumpc();
00095       if (m_sbuf->sgetc() == EOF)
00096       {
00097          m_sbuf = 0;
00098       }
00099       return temp;
00100    }
00101    
00102    bool equal(const IstreamBufIterator& b) const
00103    {
00104       return (m_sbuf == 0) ^ (b.m_sbuf != 0);   // do a little manual strength reduction
00105 //         if (((m_sbuf == 0) && (b.m_sbuf == 0)) || ((m_sbuf != 0) && (b.m_sbuf != 0)))
00106 //         {
00107 //             return true;
00108 //         }
00109 //         else
00110 //         {
00111 //             return false;
00112 //         }
00113    }
00114 private:
00115    std::streambuf* m_sbuf;
00116 };
00117 inline bool operator==(const IstreamBufIterator& lhs, const IstreamBufIterator& rhs)
00118 {
00119    return lhs.equal(rhs);
00120 }
00121 inline bool operator!=(const IstreamBufIterator& lhs, const IstreamBufIterator& rhs)
00122 {
00123    return !lhs.equal(rhs);
00124 }
00125 
00126 } // end namespace BLOCXX_NAMESPACE
00127 
00128 #endif