⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferwritertemplate.h

📁 Linux下比较早的基于命令行的DVD播放器
💻 H
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#ifndef DXR3PLAYER_BUFFERWRITERTEMPLATE_H#define DXR3PLAYER_BUFFERWRITERTEMPLATE_H//------------------------------------------------------------------------------#include <cstdlib>#include <cassert>//------------------------------------------------------------------------------/** * Buffer writer template.  */template <class BufferT>class BufferWriterTemplate : public BufferT{private:    /**     * The current offset.     */    size_t offset;public:    /**     * Construct the writer.     */    BufferWriterTemplate();    /**     * Get the offset.     */    size_t getOffset() const;    /**     * Set the offset.     */    void setOffset(size_t o);    /**     * Write a byte into the buffer.     */    void write(unsigned char c);    /**     * Write a number of bytes into the buffer.     */    void write(const void* src, size_t length);    /**     * Write a 16-bit value.     */    void write16(unsigned x);    /**     * Import 'getData' from the buffer class.     */    BufferT::getData;    /**     * Import 'ensureCapacity' from the buffer class.     */    BufferT::ensureCapacity;};//------------------------------------------------------------------------------/** * Writer for buffers that can also write individual bits or sequences * of bits. */template <class BufferT>class BufferBitWriterTemplate : public BufferWriterTemplate<BufferT>{private:    /**     * The current byte being written for bit-level writing.     */    unsigned char currentByte;    /**     * The number of bits remaining of the current byte.     */    unsigned char bitsLeft;public:    /**     * Construct a bit writer.     */    BufferBitWriterTemplate();    /**     * Write a byte into the buffer.     */    void write(unsigned char c);    /**     * Write a number of bytes into the buffer.     */    void write(const void* src, size_t length);    /**     * Write a 16-bit value.     */    void write16(unsigned x);    /**     * Write the given number of bits.     */    void writeBits(unsigned x, size_t bitsToWrite);    /**     * Finalize the current byte.     */    void finishCurrentByte();};//------------------------------------------------------------------------------/** * Buffer reference. It contains a pointer to a buffer of a certain * type, and provides an interface for the buffer writers. */template <class BufferT>class WritableBufferReferenceTemplate{private:    /**     * The pointer to the buffer.     */    BufferT* buffer;public:    /**     * Construct a buffer reference with no pointer.     */    WritableBufferReferenceTemplate();    /**     * Construct a buffer reference pointing to the given buffer.     */    explicit WritableBufferReferenceTemplate(BufferT& buffer);    /**     * Set the buffer.     */    void setBuffer(BufferT& buf);    /**     * Get the buffer.     */    BufferT& getBuffer() const;    /**     * Get the data of the buffer.     */    unsigned char* getData() const;    /**     * Ensure that the buffer has the given number of bytes.     */    void ensureCapacity(size_t capacity) const;};//------------------------------------------------------------------------------// Template definitions//------------------------------------------------------------------------------template <class BufferT>inline BufferWriterTemplate<BufferT>::BufferWriterTemplate() : offset(0){}//------------------------------------------------------------------------------template <class BufferT>inline size_t BufferWriterTemplate<BufferT>::getOffset() const{    return offset;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferWriterTemplate<BufferT>::setOffset(size_t o){    offset = o;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferWriterTemplate<BufferT>::write(unsigned char c){    ensureCapacity(offset + 1);    getData()[offset++] = c;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferWriterTemplate<BufferT>::write(const void* src, size_t length){    ensureCapacity(offset + length);        memcpy(getData()+offset, src, length);    offset += length;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferWriterTemplate<BufferT>::write16(unsigned x){    write( (x>>8)&0xff );    write( (x>>0)&0xff );}//------------------------------------------------------------------------------//------------------------------------------------------------------------------template <class BufferT>inline BufferBitWriterTemplate<BufferT>::BufferBitWriterTemplate() :    currentByte(0),    bitsLeft(8){}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitWriterTemplate<BufferT>::write(unsigned char c){    assert(bitsLeft==8);    BufferWriterTemplate<BufferT>::write(c);}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitWriterTemplate<BufferT>::write(const void* src, size_t length){    assert(bitsLeft==8);    BufferWriterTemplate<BufferT>::write(src, length);}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitWriterTemplate<BufferT>::write16(unsigned x){    assert(bitsLeft==8);    BufferWriterTemplate<BufferT>::write16(x);}//------------------------------------------------------------------------------template <class BufferT>void BufferBitWriterTemplate<BufferT>::writeBits(unsigned x, size_t bitsToWrite){    while(bitsToWrite>0) {        x &= (1<<bitsToWrite)-1;        if (bitsToWrite >= bitsLeft) {            currentByte |= x >> (bitsToWrite - bitsLeft);            bitsToWrite -= bitsLeft;            BufferWriterTemplate<BufferT>::write(currentByte);            currentByte = 0; bitsLeft = 8;        } else {            currentByte |= x << (bitsLeft - bitsToWrite);            bitsLeft -= bitsToWrite;            bitsToWrite = 0;        }    }}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitWriterTemplate<BufferT>::finishCurrentByte(){    if (bitsLeft<8) {        BufferWriterTemplate<BufferT>::write(currentByte);        currentByte = 0;        bitsLeft = 8;    }}//------------------------------------------------------------------------------//------------------------------------------------------------------------------template <class BufferT>inline WritableBufferReferenceTemplate<BufferT>::WritableBufferReferenceTemplate() :    buffer(0){}//------------------------------------------------------------------------------template <class BufferT>inline WritableBufferReferenceTemplate<BufferT>::WritableBufferReferenceTemplate(BufferT& buffer) :    buffer(&buffer){}//------------------------------------------------------------------------------template <class BufferT>inline void WritableBufferReferenceTemplate<BufferT>::setBuffer(BufferT& buf){    buffer = &buf;}//------------------------------------------------------------------------------template <class BufferT>inline BufferT& WritableBufferReferenceTemplate<BufferT>::getBuffer() const{    assert(buffer!=0);    return *buffer;}//------------------------------------------------------------------------------template <class BufferT>inline unsigned char* WritableBufferReferenceTemplate<BufferT>::getData() const{    assert(buffer!=0);    return buffer->getData();}//------------------------------------------------------------------------------template <class BufferT>inline void WritableBufferReferenceTemplate<BufferT>::ensureCapacity(size_t capacity) const{    assert(buffer!=0);    buffer->ensureCapacity(capacity);}//------------------------------------------------------------------------------#endif // DXR3PLAYER_BUFFERWRITERTEMPLATE_H// Local variables:// mode: c++// End:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -