debian/libbit-dev/usr/include/bit-0.3/bit/obstream.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef BITOBSTREAM_H
00020 #define BITOBSTREAM_H
00021 
00022 #include <ostream>
00023 #include <cstring>
00024 #include <byteswap.h>
00025 
00026 #include <bit/bstream.h>
00027 
00028 namespace bit {
00029 
00036 class obstream: public bstream {
00037 public:
00041     obstream();
00042 
00047     obstream(std::ostream& output);
00048 
00052     virtual ~obstream();
00053 
00058     size_t write_octets(unsigned char* buf, size_t octets) {
00059         return write_octets( (const unsigned char*) buf, octets);
00060     }
00061     size_t write_octets(const unsigned char* buf, size_t octets);
00062 
00070     size_t write_octets(unsigned char* buf,  size_t bufsize, size_t octets) {
00071         return write_octets( (const unsigned char*) buf, bufsize, octets);
00072     }
00073     size_t write_octets(const unsigned char* buf, size_t bufsize, size_t octets);
00074 
00079     size_t write_bits(unsigned char* buf, size_t bits) {
00080         return write_bits( (const unsigned char*) buf, bits);
00081     }
00082     size_t write_bits(const unsigned char* buf, size_t bits);
00083 
00090     size_t write_bits(unsigned char* buf, size_t bufsize, size_t bits) {
00091         return write_bits( (const unsigned char*) buf, bufsize, bits);
00092     }
00093     size_t write_bits(const unsigned char* buf, size_t bufsize, size_t bits);
00094 
00101     template <typename T>
00102     size_t write_octets(T t, size_t n=sizeof(T));
00103 
00104     size_t write_octets(uint16_t t, size_t n=2);
00105     size_t write_octets(uint32_t t, size_t n=4);
00106     size_t write_octets(uint64_t t, size_t n=8);
00107     size_t write_octets(int16_t t, size_t n=2);
00108     size_t write_octets(int32_t t, size_t n=4);
00109     size_t write_octets(int64_t t, size_t n=8);
00110 
00111 
00118     template <typename T>
00119     size_t write_bits(T t, size_t n=sizeof(T)*8);
00120 
00121     size_t write_bits(uint16_t t, size_t n=2);
00122     size_t write_bits(uint32_t t, size_t n=4);
00123     size_t write_bits(uint64_t t, size_t n=8);
00124     size_t write_bits(int16_t t, size_t n=2);
00125     size_t write_bits(int32_t t, size_t n=4);
00126     size_t write_bits(int64_t t, size_t n=8);
00127 
00132     void flush(bool msb=true);
00133 
00134     friend obstream& operator<<(obstream& s, const bits& b);
00135     friend obstream& operator<<(obstream& s, const octets& o);
00136     friend obstream& operator<<(obstream& s, const whole& w);
00137     template <typename T>
00138     friend obstream& operator<<(obstream& s, T& t);
00139 
00145     void attach_stream(std::ostream& stream, bool clearbits=false);
00146 
00154     void detach_stream(bool clearbits=false);
00155 
00156 protected:
00161     std::ostream* m_output;
00162 
00163 };
00164 
00165 template <typename T>
00166 inline        size_t obstream::write_octets(T t, size_t n) {
00167   if (m_output == NULL) return 0;
00168   if (n > sizeof(T))
00169         n = sizeof(T);
00170     write_octets( (unsigned char*) &t, n, sizeof(T));
00171 }
00172 
00173 template <typename T>
00174 inline size_t obstream::write_bits(T t, size_t n) {
00175   if (m_output == NULL) return 0;
00176   if (n > sizeof(T) * 8 )
00177         n = sizeof(T) * 8;
00178     write_bits( (unsigned char*) &t, n, sizeof(T));
00179 }
00180 
00181 inline obstream& operator<<(obstream& s, const bits& b) {
00182     s.m_state = bstream::BITS;
00183     s.m_stateval = b.val;
00184     return s;
00185 }
00186 
00187 inline obstream& operator<<(obstream& s, const octets& o) {
00188     s.m_state = bstream::OCTETS;
00189     s.m_stateval = o.val;
00190     return s;
00191 }
00192 
00193 inline obstream& operator<<(obstream& s, const whole& w) {
00194     s.m_state = bstream::WHOLE;
00195     s.m_stateval = 0;
00196     return s;
00197 }
00198 
00199 
00200 template <typename T>
00201 inline obstream& operator<<(obstream& s, T& t) {
00202     switch (s.m_state) {
00203     case bstream::WHOLE:
00204         s.write_octets(t);
00205         break;
00206     case bstream::BITS:
00207         s.write_bits(t, s.m_stateval);
00208         break;
00209     case bstream::OCTETS:
00210         s.write_octets(t, s.m_stateval);
00211         break;
00212     }
00213     return s;
00214 }
00215 
00216 
00217 }; // namespace bit
00218 
00219 
00220 #endif

Generated on Tue Mar 13 20:00:01 2007 by  doxygen 1.5.1