gfx/skia/trunk/src/core/SkBuffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkBuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkBuffer_DEFINED
    1.14 +#define SkBuffer_DEFINED
    1.15 +
    1.16 +#include "SkScalar.h"
    1.17 +
    1.18 +/** \class SkRBuffer
    1.19 +
    1.20 +    Light weight class for reading data from a memory block.
    1.21 +    The RBuffer is given the buffer to read from, with either a specified size
    1.22 +    or no size (in which case no range checking is performed). It is iillegal
    1.23 +    to attempt to read a value from an empty RBuffer (data == null).
    1.24 +*/
    1.25 +class SkRBuffer : SkNoncopyable {
    1.26 +public:
    1.27 +    SkRBuffer() : fData(0), fPos(0), fStop(0) {}
    1.28 +    /** Initialize RBuffer with a data pointer, but no specified length.
    1.29 +        This signals the RBuffer to not perform range checks during reading.
    1.30 +    */
    1.31 +    SkRBuffer(const void* data) {
    1.32 +        fData = (const char*)data;
    1.33 +        fPos = (const char*)data;
    1.34 +        fStop = 0;  // no bounds checking
    1.35 +    }
    1.36 +    /** Initialize RBuffer with a data point and length.
    1.37 +    */
    1.38 +    SkRBuffer(const void* data, size_t size) {
    1.39 +        SkASSERT(data != 0 || size == 0);
    1.40 +        fData = (const char*)data;
    1.41 +        fPos = (const char*)data;
    1.42 +        fStop = (const char*)data + size;
    1.43 +    }
    1.44 +
    1.45 +    virtual ~SkRBuffer() { }
    1.46 +
    1.47 +    /** Return the number of bytes that have been read from the beginning
    1.48 +        of the data pointer.
    1.49 +    */
    1.50 +    size_t  pos() const { return fPos - fData; }
    1.51 +    /** Return the total size of the data pointer. Only defined if the length was
    1.52 +        specified in the constructor or in a call to reset().
    1.53 +    */
    1.54 +    size_t  size() const { return fStop - fData; }
    1.55 +    /** Return true if the buffer has read to the end of the data pointer.
    1.56 +        Only defined if the length was specified in the constructor or in a call
    1.57 +        to reset(). Always returns true if the length was not specified.
    1.58 +    */
    1.59 +    bool    eof() const { return fPos >= fStop; }
    1.60 +
    1.61 +    /** Read the specified number of bytes from the data pointer. If buffer is not
    1.62 +        null, copy those bytes into buffer.
    1.63 +    */
    1.64 +    virtual bool read(void* buffer, size_t size) {
    1.65 +        if (size) {
    1.66 +            this->readNoSizeCheck(buffer, size);
    1.67 +        }
    1.68 +        return true;
    1.69 +    }
    1.70 +
    1.71 +    const void* skip(size_t size); // return start of skipped data
    1.72 +    size_t  skipToAlign4();
    1.73 +
    1.74 +    bool readPtr(void** ptr) { return read(ptr, sizeof(void*)); }
    1.75 +    bool readScalar(SkScalar* x) { return read(x, 4); }
    1.76 +    bool readU32(uint32_t* x) { return read(x, 4); }
    1.77 +    bool readS32(int32_t* x) { return read(x, 4); }
    1.78 +    bool readU16(uint16_t* x) { return read(x, 2); }
    1.79 +    bool readS16(int16_t* x) { return read(x, 2); }
    1.80 +    bool readU8(uint8_t* x) { return read(x, 1); }
    1.81 +    bool readBool(bool* x) {
    1.82 +        uint8_t u8;
    1.83 +        if (this->readU8(&u8)) {
    1.84 +            *x = (u8 != 0);
    1.85 +            return true;
    1.86 +        }
    1.87 +        return false;
    1.88 +    }
    1.89 +
    1.90 +protected:
    1.91 +    void    readNoSizeCheck(void* buffer, size_t size);
    1.92 +
    1.93 +    const char* fData;
    1.94 +    const char* fPos;
    1.95 +    const char* fStop;
    1.96 +};
    1.97 +
    1.98 +/** \class SkRBufferWithSizeCheck
    1.99 +
   1.100 +    Same as SkRBuffer, except that a size check is performed before the read operation and an
   1.101 +    error is set if the read operation is attempting to read past the end of the data.
   1.102 +*/
   1.103 +class SkRBufferWithSizeCheck : public SkRBuffer {
   1.104 +public:
   1.105 +    SkRBufferWithSizeCheck(const void* data, size_t size) : SkRBuffer(data, size), fError(false) {}
   1.106 +
   1.107 +    /** Read the specified number of bytes from the data pointer. If buffer is not
   1.108 +        null and the number of bytes to read does not overflow this object's data,
   1.109 +        copy those bytes into buffer.
   1.110 +    */
   1.111 +    virtual bool read(void* buffer, size_t size) SK_OVERRIDE;
   1.112 +
   1.113 +    /** Returns whether or not a read operation attempted to read past the end of the data.
   1.114 +    */
   1.115 +    bool isValid() const { return !fError; }
   1.116 +private:
   1.117 +    bool fError;
   1.118 +};
   1.119 +
   1.120 +/** \class SkWBuffer
   1.121 +
   1.122 +    Light weight class for writing data to a memory block.
   1.123 +    The WBuffer is given the buffer to write into, with either a specified size
   1.124 +    or no size, in which case no range checking is performed. An empty WBuffer
   1.125 +    is legal, in which case no data is ever written, but the relative pos()
   1.126 +    is updated.
   1.127 +*/
   1.128 +class SkWBuffer : SkNoncopyable {
   1.129 +public:
   1.130 +    SkWBuffer() : fData(0), fPos(0), fStop(0) {}
   1.131 +    SkWBuffer(void* data) { reset(data); }
   1.132 +    SkWBuffer(void* data, size_t size) { reset(data, size); }
   1.133 +
   1.134 +    void reset(void* data) {
   1.135 +        fData = (char*)data;
   1.136 +        fPos = (char*)data;
   1.137 +        fStop = 0;  // no bounds checking
   1.138 +    }
   1.139 +
   1.140 +    void reset(void* data, size_t size) {
   1.141 +        SkASSERT(data != 0 || size == 0);
   1.142 +        fData = (char*)data;
   1.143 +        fPos = (char*)data;
   1.144 +        fStop = (char*)data + size;
   1.145 +    }
   1.146 +
   1.147 +    size_t  pos() const { return fPos - fData; }
   1.148 +    void*   skip(size_t size); // return start of skipped data
   1.149 +
   1.150 +    void write(const void* buffer, size_t size) {
   1.151 +        if (size) {
   1.152 +            this->writeNoSizeCheck(buffer, size);
   1.153 +        }
   1.154 +    }
   1.155 +
   1.156 +    size_t  padToAlign4();
   1.157 +
   1.158 +    void    writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
   1.159 +    void    writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
   1.160 +    void    write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
   1.161 +    void    write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
   1.162 +    void    write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
   1.163 +    void    writeBool(bool x) { this->write8(x); }
   1.164 +
   1.165 +private:
   1.166 +    void    writeNoSizeCheck(const void* buffer, size_t size);
   1.167 +
   1.168 +    char* fData;
   1.169 +    char* fPos;
   1.170 +    char* fStop;
   1.171 +};
   1.172 +
   1.173 +#endif

mercurial