src/CompactBuffer.h
author David Anderson <dvander@alliedmods.net>
Tue Dec 25 16:32:06 2012 -0800 (2012-12-25)
changeset 238 e9bec7549c28
parent 86 32ad0523b309
child 263 ba85a47ee414
permissions -rw-r--r--
Preparatory refactoring for const types.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jc_compact_buffer_h_
    20 #define _include_jc_compact_buffer_h_
    21 
    22 #include "AllocatorPolicies.h"
    23 #include "Utility.h"
    24 #include "Vector.h"
    25 
    26 namespace ke {
    27 
    28 class CompactBufferReader
    29 {
    30     const uint8 *pos_;
    31     const uint8 *end_;
    32 
    33   public:
    34     CompactBufferReader(const uint8 *start, const uint8 *end)
    35       : pos_(start),
    36         end_(end)
    37     {
    38     }
    39 
    40     uint8 readUint8() {
    41         assert(pos_ < end_);
    42         return *pos_++;
    43     }
    44 
    45     uint32 readUint32() {
    46         uint32 value = 0;
    47         uint32 shift = 0;
    48 
    49         uint8 byte;
    50         do {
    51             byte = readUint8();
    52             value |= (byte >> 1) << shift;
    53             shift += 7;
    54         } while (byte & 1);
    55 
    56         assert(shift < 32);
    57         return value;
    58     }
    59 
    60     bool done() const {
    61         return pos_ == end_;
    62     }
    63 };
    64 
    65 class CompactBufferWriter
    66 {
    67     Vector<uint8, ZoneAllocatorPolicy> buffer_;
    68     bool enoughMemory_;
    69 
    70   public:
    71     CompactBufferWriter()
    72       : enoughMemory_(true)
    73     {
    74     }
    75 
    76     void writeUint8(unsigned value) {
    77         enoughMemory_ &= buffer_.append(value);
    78     }
    79     void writeUint32(unsigned value) {
    80         do {
    81             writeUint8(((value & 0x7F) << 1) | (value > 0x7F));
    82             value >>= 7;
    83         } while (value);
    84     }
    85 
    86     uint8 *buffer() const {
    87         return buffer_.buffer();
    88     }
    89     size_t length() const {
    90         return buffer_.length();
    91     }
    92 
    93     bool oom() const {
    94         return !enoughMemory_;
    95     }
    96 };
    97 
    98 }
    99 
   100 #endif // _include_jc_compact_buffer_h_