WebKit/chromium/public/WebVector.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2009 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 WebVector_h
       
    32 #define WebVector_h
       
    33 
       
    34 #include "WebCommon.h"
       
    35 
       
    36 #include <algorithm>
       
    37 
       
    38 namespace WebKit {
       
    39 
       
    40 // A simple vector class.
       
    41 //
       
    42 // Sample usage:
       
    43 //
       
    44 //   void Foo(WebVector<int>& result)
       
    45 //   {
       
    46 //       WebVector<int> data(10);
       
    47 //       for (size_t i = 0; i < data.size(); ++i)
       
    48 //           data[i] = ...
       
    49 //       result.swap(data);
       
    50 //   }
       
    51 //
       
    52 // It is also possible to assign from other types of random access
       
    53 // containers:
       
    54 //
       
    55 //   void Foo(const std::vector<std::string>& input)
       
    56 //   {
       
    57 //       WebVector<WebCString> cstrings = input;
       
    58 //       ...
       
    59 //   }
       
    60 //
       
    61 template <typename T>
       
    62 class WebVector {
       
    63 public:
       
    64     typedef T ValueType;
       
    65 
       
    66     ~WebVector()
       
    67     {
       
    68         destroy();
       
    69     }
       
    70 
       
    71     explicit WebVector(size_t size = 0)
       
    72     {
       
    73         initialize(size);
       
    74     }
       
    75 
       
    76     WebVector(const WebVector<T>& other)
       
    77     {
       
    78         initializeFrom(other.m_ptr, other.m_size);
       
    79     }
       
    80 
       
    81     template <typename C>
       
    82     WebVector(const C& other)
       
    83     {
       
    84         initializeFrom(other.size() ? &other[0] : 0, other.size());
       
    85     }
       
    86 
       
    87     WebVector& operator=(const WebVector& other)
       
    88     {
       
    89         if (this != &other)
       
    90             assign(other);
       
    91         return *this;
       
    92     }
       
    93 
       
    94     template <typename C>
       
    95     WebVector<T>& operator=(const C& other)
       
    96     {
       
    97         if (this != reinterpret_cast<const WebVector<T>*>(&other))
       
    98             assign(other);
       
    99         return *this;
       
   100     }
       
   101 
       
   102     template <typename C>
       
   103     void assign(const C& other)
       
   104     {
       
   105         assign(other.size() ? &other[0] : 0, other.size());
       
   106     }
       
   107 
       
   108     template <typename U>
       
   109     void assign(const U* values, size_t size)
       
   110     {
       
   111         destroy();
       
   112         initializeFrom(values, size);
       
   113     }
       
   114 
       
   115     size_t size() const { return m_size; }
       
   116     bool isEmpty() const { return !m_size; }
       
   117 
       
   118     T& operator[](size_t i) { return m_ptr[i]; }
       
   119     const T& operator[](size_t i) const { return m_ptr[i]; }
       
   120 
       
   121     T* data() { return m_ptr; }
       
   122     const T* data() const { return m_ptr; }
       
   123 
       
   124     void swap(WebVector<T>& other)
       
   125     {
       
   126         std::swap(m_ptr, other.m_ptr);
       
   127         std::swap(m_size, other.m_size);
       
   128     }
       
   129 
       
   130 private:
       
   131     void initialize(size_t size)
       
   132     {
       
   133         m_size = size;
       
   134         if (!m_size)
       
   135             m_ptr = 0;
       
   136         else {
       
   137             m_ptr = static_cast<T*>(::operator new(sizeof(T) * m_size));
       
   138             for (size_t i = 0; i < m_size; ++i)
       
   139                 new (&m_ptr[i]) T();
       
   140         }
       
   141     }
       
   142 
       
   143     template <typename U>
       
   144     void initializeFrom(const U* values, size_t size)
       
   145     {
       
   146         m_size = size;
       
   147         if (!m_size)
       
   148             m_ptr = 0;
       
   149         else {
       
   150             m_ptr = static_cast<T*>(::operator new(sizeof(T) * m_size));
       
   151             for (size_t i = 0; i < m_size; ++i)
       
   152                 new (&m_ptr[i]) T(values[i]);
       
   153         }
       
   154     }
       
   155 
       
   156     void destroy()
       
   157     {
       
   158         for (size_t i = 0; i < m_size; ++i)
       
   159             m_ptr[i].~T();
       
   160         ::operator delete(m_ptr);
       
   161     }
       
   162 
       
   163     T* m_ptr;
       
   164     size_t m_size;
       
   165 };
       
   166 
       
   167 }  // namespace WebKit
       
   168 
       
   169 #endif