Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef ArrayBufferBuilder_h
     32 #define ArrayBufferBuilder_h
     33 
     34 #include "wtf/ArrayBuffer.h"
     35 #include "wtf/Noncopyable.h"
     36 #include "wtf/RefPtr.h"
     37 #include "wtf/text/WTFString.h"
     38 
     39 namespace WTF {
     40 
     41 // A utility class to build an ArrayBuffer instance.
     42 class WTF_EXPORT ArrayBufferBuilder {
     43     // Disallow copying since it's expensive and we don't want code to do it by
     44     // accident.
     45     WTF_MAKE_NONCOPYABLE(ArrayBufferBuilder);
     46 
     47 public:
     48     // Creates an ArrayBufferBuilder using the default capacity.
     49     ArrayBufferBuilder();
     50 
     51     ArrayBufferBuilder(unsigned capacity)
     52         : m_bytesUsed(0)
     53         , m_variableCapacity(true)
     54     {
     55         m_buffer = ArrayBuffer::create(capacity, 1);
     56     }
     57 
     58     unsigned append(const char* data, unsigned length);
     59 
     60     // Returns the accumulated data as an ArrayBuffer instance. If needed,
     61     // creates a new ArrayBuffer instance and copies contents from the internal
     62     // buffer to it. Otherwise, returns a PassRefPtr pointing to the internal
     63     // buffer.
     64     PassRefPtr<ArrayBuffer> toArrayBuffer();
     65 
     66     // Converts the accumulated data into a String using the default encoding.
     67     String toString();
     68 
     69     // Number of bytes currently accumulated.
     70     unsigned byteLength() const
     71     {
     72         return m_bytesUsed;
     73     }
     74 
     75     // Number of bytes allocated.
     76     unsigned capacity() const
     77     {
     78         return m_buffer->byteLength();
     79     }
     80 
     81     void shrinkToFit();
     82 
     83     const void* data() const
     84     {
     85         return m_buffer->data();
     86     }
     87 
     88     // If set to false, the capacity won't be expanded and when appended data
     89     // overflows, the overflowed part will be dropped.
     90     void setVariableCapacity(bool value)
     91     {
     92         m_variableCapacity = value;
     93     }
     94 
     95 private:
     96     // Expands the size of m_buffer to size + m_bytesUsed bytes. Returns true
     97     // iff successful. If reallocation is needed, copies only data in
     98     // [0, m_bytesUsed) range.
     99     bool expandCapacity(unsigned size);
    100 
    101     unsigned m_bytesUsed;
    102     bool m_variableCapacity;
    103     RefPtr<ArrayBuffer> m_buffer;
    104 };
    105 
    106 } // namespace WTF
    107 
    108 using WTF::ArrayBufferBuilder;
    109 
    110 #endif // ArrayBufferBuilder_h
    111