Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / ArrayPiece.cpp
blobf0cc5d42d241e8fcb85ac16ae48439413c06c23f
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.
5 #include "config.h"
6 #include "wtf/ArrayPiece.h"
8 #include "wtf/ArrayBuffer.h"
9 #include "wtf/ArrayBufferView.h"
10 #include "wtf/Assertions.h"
12 namespace WTF {
14 ArrayPiece::ArrayPiece()
16 initNull();
19 ArrayPiece::ArrayPiece(void* data, unsigned byteLength)
21 initWithData(data, byteLength);
24 ArrayPiece::ArrayPiece(ArrayBuffer* buffer)
26 if (buffer) {
27 initWithData(buffer->data(), buffer->byteLength());
28 } else {
29 initNull();
33 ArrayPiece::ArrayPiece(ArrayBufferView* buffer)
35 if (buffer) {
36 initWithData(buffer->baseAddress(), buffer->byteLength());
37 } else {
38 initNull();
42 bool ArrayPiece::isNull() const
44 return m_isNull;
47 void* ArrayPiece::data() const
49 ASSERT(!isNull());
50 return m_data;
53 unsigned char* ArrayPiece::bytes() const
55 return static_cast<unsigned char*>(data());
58 unsigned ArrayPiece::byteLength() const
60 ASSERT(!isNull());
61 return m_byteLength;
64 void ArrayPiece::initWithData(void* data, unsigned byteLength)
66 m_byteLength = byteLength;
67 m_data = data;
68 m_isNull = false;
71 void ArrayPiece::initNull()
73 m_byteLength = 0;
74 m_data = 0;
75 m_isNull = true;
78 } // namespace WTF