BinaryWriter.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef UTILS__ENCODINGS__BINARY_WRITER_H
00024 #define UTILS__ENCODINGS__BINARY_WRITER_H
00025 
00026 namespace Utils {
00027 namespace Encodings {
00028 
00030 
00050 class BinaryWriter
00051 {
00052     REFERENCE_OBJECT (BinaryWriter)
00053 
00054 public:
00055     enum ByteOrder
00056     {
00057         BigEndian       = 0,
00058         LittleEndian    = 1
00059     };
00060 
00061                     BinaryWriter (ByteOrder, bool autoGrow = true);
00062                     BinaryWriter (QByteArray *buffer, ByteOrder,
00063                                   bool autoGrow = false);
00064                     ~BinaryWriter();
00065 
00066     void            reserve (size_t bytesCount);
00067 
00068     void            writeByte (uchar);
00069     void            writeUInt16 (quint16);
00070     void            writeUInt32 (quint32);
00071     void            writeUInt64 (quint64);
00072     void            writeInt16 (qint16);
00073     void            writeInt32 (qint32);
00074     void            writeInt64 (qint64);
00075 
00076     void            writeBytes (const QByteArray &);
00077     void            writeString (const QByteArray &, char terminator = '\0');
00078 
00079     ByteOrder       byteOrder() const;
00080     void            setByteOrder (ByteOrder);
00081 
00082     bool            hasWrittenAll() const;
00083     bool            hasWrittenPastEnd() const;
00084     size_t          bytesWritten() const;
00085 
00086     QByteArray      buffer() const;
00087 
00088 private:
00089     void            growBuffer (size_t count, bool force = false);
00090     void            doWrite (char *destination, size_t size);
00091 
00092     class Data
00093     {
00094         REFERENCE_OBJECT (Data)
00095 
00096     public:
00097         QByteArray  *buffer;
00098         ByteOrder   byteOrder;
00099         bool        autoGrow;
00100         bool        ownsBuffer;
00101         bool        swapBytes;
00102         char        *dataStart;
00103         char        *dataEnd;
00104 
00105         Data() :    buffer(), byteOrder (BigEndian), autoGrow (false),
00106                     ownsBuffer (false), swapBytes (false),
00107                     dataStart (0), dataEnd (0)
00108         {}
00109     };
00110 
00111     Data    d;
00112 };
00113 
00115 
00118 inline BinaryWriter::ByteOrder BinaryWriter::byteOrder() const
00119 { return d.byteOrder; }
00120 
00122 inline QByteArray BinaryWriter::buffer() const
00123 { return QByteArray (*d.buffer); }
00124 
00126 inline void BinaryWriter::writeInt16 (qint16 value)
00127 { writeUInt16 (static_cast <qint16> (value)); }
00128 
00130 inline void BinaryWriter::writeInt32 (qint32 value)
00131 { writeUInt32 (static_cast <qint32> (value)); }
00132 
00134 inline void BinaryWriter::writeInt64 (qint64 value)
00135 { writeUInt64 (static_cast <qint64> (value)); }
00136 
00137 } // namespace Encodings
00138 } // namespace Utils
00139 
00140 #endif // UTILS__ENCODINGS__BINARY_WRITER_H