Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebVector.h
blob9ae55a2204ddd568af1a24e2c22894f18f93cbe7
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #ifndef WebVector_h
32 #define WebVector_h
34 #include "WebCommon.h"
36 #include <algorithm>
37 #include <limits>
38 #include <stdlib.h>
40 namespace blink {
42 // A simple vector class.
44 // Sample usage:
46 // void Foo(WebVector<int>& result)
47 // {
48 // WebVector<int> data(10);
49 // for (size_t i = 0; i < data.size(); ++i)
50 // data[i] = ...
51 // result.swap(data);
52 // }
54 // It is also possible to assign from other types of random access
55 // containers:
57 // void Foo(const std::vector<std::string>& input)
58 // {
59 // WebVector<WebCString> cstrings = input;
60 // ...
61 // }
63 template <typename T>
64 class WebVector {
65 public:
66 using ValueType = T;
67 using iterator = T*;
68 using const_iterator = const T*;
70 ~WebVector()
72 destroy();
75 explicit WebVector(size_t size = 0)
77 initialize(size);
80 template <typename U>
81 WebVector(const U* values, size_t size)
83 initializeFrom(values, size);
86 WebVector(const WebVector<T>& other)
88 initializeFrom(other.m_ptr, other.m_size);
91 template <typename C>
92 WebVector(const C& other)
94 initializeFrom(other.size() ? &other[0] : 0, other.size());
97 WebVector& operator=(const WebVector& other)
99 if (this != &other)
100 assign(other);
101 return *this;
104 template <typename C>
105 WebVector<T>& operator=(const C& other)
107 if (this != reinterpret_cast<const WebVector<T>*>(&other))
108 assign(other);
109 return *this;
112 template <typename C>
113 void assign(const C& other)
115 assign(other.size() ? &other[0] : 0, other.size());
118 template <typename U>
119 void assign(const U* values, size_t size)
121 destroy();
122 initializeFrom(values, size);
125 size_t size() const { return m_size; }
126 bool isEmpty() const { return !m_size; }
128 T& operator[](size_t i)
130 BLINK_ASSERT(i < m_size);
131 return m_ptr[i];
133 const T& operator[](size_t i) const
135 BLINK_ASSERT(i < m_size);
136 return m_ptr[i];
139 bool contains(const T& value) const
141 for (size_t i = 0; i < m_size; i++) {
142 if (m_ptr[i] == value)
143 return true;
145 return false;
148 T* data() { return m_ptr; }
149 const T* data() const { return m_ptr; }
151 iterator begin() { return data(); }
152 iterator end() { return begin() + m_size; }
153 const_iterator begin() const { return data(); }
154 const_iterator end() const { return begin() + m_size; }
156 void swap(WebVector<T>& other)
158 std::swap(m_ptr, other.m_ptr);
159 std::swap(m_size, other.m_size);
162 private:
163 void initialize(size_t size)
165 validateSize(size);
166 m_size = size;
167 if (!m_size)
168 m_ptr = 0;
169 else {
170 char* cptr = static_cast<char*>(::operator new(sizeof(T) * m_size));
171 for (size_t i = 0; i < m_size; ++i)
172 new (&cptr[sizeof(T) * i]) T();
173 m_ptr = reinterpret_cast<T*>(cptr);
177 template <typename U>
178 void initializeFrom(const U* values, size_t size)
180 validateSize(size);
181 m_size = size;
182 if (!m_size)
183 m_ptr = 0;
184 else {
185 char* cptr = static_cast<char*>(::operator new(sizeof(T) * m_size));
186 for (size_t i = 0; i < m_size; ++i)
187 new (&cptr[sizeof(T) * i]) T(values[i]);
188 m_ptr = reinterpret_cast<T*>(cptr);
192 void validateSize(size_t size)
194 if (std::numeric_limits<size_t>::max() / sizeof(T) < size)
195 abort();
198 void destroy()
200 for (size_t i = 0; i < m_size; ++i)
201 m_ptr[i].~T();
202 ::operator delete(m_ptr);
205 T* m_ptr;
206 size_t m_size;
209 } // namespace blink
211 #endif