1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "wtf/ArrayPiece.h" 7 8 #include "wtf/ArrayBuffer.h" 9 #include "wtf/ArrayBufferView.h" 10 #include "wtf/Assertions.h" 11 12 namespace WTF { 13 14 ArrayPiece::ArrayPiece() 15 { 16 initNull(); 17 } 18 19 ArrayPiece::ArrayPiece(void* data, unsigned byteLength) 20 { 21 initWithData(data, byteLength); 22 } 23 24 ArrayPiece::ArrayPiece(ArrayBuffer* buffer) 25 { 26 if (buffer) { 27 initWithData(buffer->data(), buffer->byteLength()); 28 } else { 29 initNull(); 30 } 31 } 32 33 ArrayPiece::ArrayPiece(ArrayBufferView* buffer) 34 { 35 if (buffer) { 36 initWithData(buffer->baseAddress(), buffer->byteLength()); 37 } else { 38 initNull(); 39 } 40 } 41 42 bool ArrayPiece::isNull() const 43 { 44 return m_isNull; 45 } 46 47 void* ArrayPiece::data() const 48 { 49 ASSERT(!isNull()); 50 return m_data; 51 } 52 53 unsigned char* ArrayPiece::bytes() const 54 { 55 return static_cast<unsigned char*>(data()); 56 } 57 58 unsigned ArrayPiece::byteLength() const 59 { 60 ASSERT(!isNull()); 61 return m_byteLength; 62 } 63 64 void ArrayPiece::initWithData(void* data, unsigned byteLength) 65 { 66 m_byteLength = byteLength; 67 m_data = data; 68 m_isNull = false; 69 } 70 71 void ArrayPiece::initNull() 72 { 73 m_byteLength = 0; 74 m_data = 0; 75 m_isNull = true; 76 } 77 78 } // namespace WTF 79