1 /* 2 * The Doomsday Engine Project -- libcore 3 * 4 * Copyright © 2010-2017 Jaakko Keränen <jaakko.keranen@iki.fi> 5 * 6 * @par License 7 * LGPL: http://www.gnu.org/licenses/lgpl.html 8 * 9 * <small>This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 3 of the License, or (at your 12 * option) any later version. This program is distributed in the hope that it 13 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. You should have received a copy of 16 * the GNU Lesser General Public License along with this program; if not, see: 17 * http://www.gnu.org/licenses</small> 18 */ 19 20 #ifndef LIBDENG2_BYTEREFARRAY_H 21 #define LIBDENG2_BYTEREFARRAY_H 22 23 #include "../IByteArray" 24 25 namespace de { 26 27 /** 28 * Byte array that operates on a pointer-based array. Instances of 29 * ByteRefArray are fixed size: one cannot write past the end of 30 * the array. @ingroup data 31 */ 32 class DENG2_PUBLIC ByteRefArray : public IByteArray 33 { 34 public: 35 /// set() is attempted on a nonmodifiable array. @ingroup errors 36 DENG2_ERROR(NonModifiableError); 37 38 public: 39 /** 40 * Constructs a reference array to NULL with zero size. 41 */ 42 ByteRefArray(); 43 44 /** 45 * Constructs a new byte reference array. 46 * 47 * @param base Pointer to the start of the array. 48 * @param size Total size of the array. 49 */ 50 ByteRefArray(void *base, Size size); 51 52 /** 53 * Constructs a new non-modifiable byte reference array. 54 * 55 * @param base Pointer to the start of the array. 56 * @param size Total size of the array. 57 */ 58 ByteRefArray(void const *base, Size size); 59 60 /** 61 * Constructs a non-modifiable byte reference array from a null terminated C 62 * string. 63 * 64 * @param nullTerminatedCStr Pointer to the start of the string. 65 */ 66 static ByteRefArray fromCStr(char const *nullTerminatedCStr); 67 68 /** 69 * Returns a pointer to the start of the array. 70 */ 71 void *base(); 72 73 template <typename Type> baseAs()74 Type const *baseAs() const { return reinterpret_cast<Type const *>(base()); } 75 76 /** 77 * Returns a non-modifiable pointer to the start of the array. 78 */ 79 void const *base() const; 80 81 /** 82 * Returns a non-modifiable pointer to the start of the array. 83 */ readBase()84 void const *readBase() const { return base(); } 85 86 /** 87 * Sets the contents of the array to zero. 88 */ 89 void clear(); 90 91 /** 92 * Sets the contents of the array to a specific value. 93 * 94 * @param value Value to write to all bytes. 95 */ 96 void fill(Byte value); 97 98 // Implements IByteArray. 99 Size size() const; 100 void get(Offset at, Byte *values, Size count) const; 101 void set(Offset at, Byte const *values, Size count); 102 103 private: 104 Byte *_writeBase; 105 Byte const *_readBase; 106 Size _size; 107 }; 108 109 } // namespace de 110 111 #endif // LIBDENG2_BYTEREFARRAY_H 112