Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(554)

Side by Side Diff: Source/core/css/CSSValue.h

Issue 1233363002: CSSValue Immediates: Replace CSSPrimitiveValue usage with const references (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@cssvalue_patch_4_attempt_2
Patch Set: Rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/css/CSSShadowValue.cpp ('k') | Source/core/css/CSSValueList.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
9 * 9 *
10 * This library is distributed in the hope that it will be useful, 10 * This library is distributed in the hope that it will be useful,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 #if !ENABLE(OILPAN) 58 #if !ENABLE(OILPAN)
59 template<class T> CSSValue(RefPtrWillBeRawPtr<T> cssValueObject) 59 template<class T> CSSValue(RefPtrWillBeRawPtr<T> cssValueObject)
60 : m_data(cssValueObject.release().leakRef()) 60 : m_data(cssValueObject.release().leakRef())
61 { 61 {
62 static_assert(WTF::IsSubclass<T, CSSValueObject>::value, "Parameter must be a container for CSSValueObject"); 62 static_assert(WTF::IsSubclass<T, CSSValueObject>::value, "Parameter must be a container for CSSValueObject");
63 ASSERT(isObject()); 63 ASSERT(isObject());
64 ASSERT(m_data); 64 ASSERT(m_data);
65 } 65 }
66 #endif 66 #endif
67 67
68 CSSValue(const CSSValue& cssValue) 68 CSSValue(CSSValue const& cssValue)
69 : m_data(cssValue.m_data) 69 : m_data(cssValue.m_data)
70 { 70 {
71 ref(); 71 ref();
72 } 72 }
73 73
74
75 CSSValue(CSSValue&& cssValue)
76 : m_data(cssValue.m_data)
77 {
78 cssValue.m_data.clear();
79 }
80
81
74 // Implicit conversion from CSSPrimitiveValue. 82 // Implicit conversion from CSSPrimitiveValue.
75 CSSValue(const CSSPrimitiveValue& cssPrimitiveValue) 83 CSSValue(const CSSPrimitiveValue& cssPrimitiveValue)
76 : m_data(static_cast<CSSValueObject*>(cssPrimitiveValue.get())) 84 : m_data(reinterpret_cast<CSSValueObject*>(cssPrimitiveValue.get()))
77 { 85 {
78 ref(); 86 ref();
79 } 87 }
80 88
81 ~CSSValue() 89 ~CSSValue()
82 { 90 {
83 deref(); 91 // Could be null, because of move operations.
92 if (m_data)
93 deref();
84 } 94 }
85 95
96 /*
86 CSSValue& operator=(const CSSValue& rhs) 97 CSSValue& operator=(const CSSValue& rhs)
87 { 98 {
88 rhs.ref(); 99 rhs.ref();
89 deref(); 100 deref();
90 m_data = rhs.m_data; 101 m_data = rhs.m_data;
91 return *this; 102 return *this;
103 }*/
104
105 CSSValue& operator=(CSSValue&& rhs)
106 {
107 m_data = rhs.m_data.get();
108 rhs.m_data = nullptr;
109 return *this;
92 } 110 }
93 111
94 bool operator==(const CSSValue& other) const 112 bool operator==(const CSSValue& other) const
95 { 113 {
96 if (m_data == other.m_data) 114 if (m_data == other.m_data)
97 return true; 115 return true;
98 if (!isObject() || !other.isObject()) 116 if (!isObject() || !other.isObject())
99 return false; 117 return false;
100 return m_data->equals(*other.m_data); 118 return m_data->equals(*other.m_data);
101 } 119 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 // like WebNode. 188 // like WebNode.
171 CSSValueObject* get() const 189 CSSValueObject* get() const
172 { 190 {
173 return m_data.get(); 191 return m_data.get();
174 } 192 }
175 193
176 private: 194 private:
177 CSSValue() = delete; // compile-time guard 195 CSSValue() = delete; // compile-time guard
178 CSSValue(std::nullptr_t) = delete; // compile-time guard 196 CSSValue(std::nullptr_t) = delete; // compile-time guard
179 197
180 void ref() const 198 inline void ref() const
181 { 199 {
182 #if !ENABLE(OILPAN) 200 #if !ENABLE(OILPAN)
183 if (m_data && isObject()) 201 if (isObject())
184 m_data->ref(); 202 m_data->ref();
185 #endif 203 #endif
186 } 204 }
187 205
188 void deref() const 206 inline void deref() const
189 { 207 {
190 #if !ENABLE(OILPAN) 208 #if !ENABLE(OILPAN)
191 if (m_data && isObject()) 209 if (isObject())
192 m_data->deref(); 210 m_data->deref();
193 #endif 211 #endif
194 } 212 }
195 213
196 bool isObject() const 214 bool isObject() const
197 { 215 {
198 return !(*reinterpret_cast<const uintptr_t*>(&m_data) & 1); 216 return !(reinterpret_cast<const uintptr_t>(m_data.get()) & 1);
199 } 217 }
200 218
201 RawPtrWillBeMember<CSSValueObject> m_data; 219 RawPtrWillBeMember<CSSValueObject> m_data;
202 }; 220 };
203 221
204 // A nullable container for CSSValueObject. Contents are the same as CSSValue. 222 // A nullable container for CSSValueObject. Contents are the same as CSSValue.
205 // Behavior is similar to a CSSValue*. 223 // Behavior is similar to a CSSValue*.
206 class CORE_EXPORT NullableCSSValue { 224 class CORE_EXPORT NullableCSSValue {
207 ALLOW_ONLY_INLINE_ALLOCATION(); 225 ALLOW_ONLY_INLINE_ALLOCATION();
208 public: 226 public:
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 { 263 {
246 ref(); 264 ref();
247 } 265 }
248 266
249 NullableCSSValue(const CSSValue& cssValue) 267 NullableCSSValue(const CSSValue& cssValue)
250 : m_data(cssValue.m_data) 268 : m_data(cssValue.m_data)
251 { 269 {
252 ref(); 270 ref();
253 } 271 }
254 272
273 NullableCSSValue(CSSValue&& cssValue)
274 : m_data(cssValue.m_data)
275 {
276 cssValue.m_data.clear();
277 }
278
279 NullableCSSValue(NullableCSSValue&& cssValue)
280 : m_data(cssValue.m_data)
281 {
282 cssValue.m_data.clear();
283 }
284
255 // Implicit conversion from CSSPrimitiveValue. 285 // Implicit conversion from CSSPrimitiveValue.
256 NullableCSSValue(const CSSPrimitiveValue& cssPrimitiveValue) 286 NullableCSSValue(const CSSPrimitiveValue& cssPrimitiveValue)
257 : m_data(static_cast<CSSValueObject*>(cssPrimitiveValue.get())) 287 : m_data(static_cast<CSSValueObject*>(cssPrimitiveValue.get()))
258 { 288 {
259 ref(); 289 ref();
260 } 290 }
261 291
262 ~NullableCSSValue() 292 ~NullableCSSValue()
263 { 293 {
264 deref(); 294 deref();
265 }; 295 };
266 296
267 explicit operator bool() const 297 explicit operator bool() const
268 { 298 {
269 return m_data; 299 return m_data;
270 } 300 }
271 301
272 NullableCSSValue& operator=(const NullableCSSValue& rhs) 302 NullableCSSValue& operator=(NullableCSSValue const& rhs)
273 { 303 {
274 rhs.ref(); 304 rhs.ref();
275 deref(); 305 deref();
276 m_data = rhs.m_data; 306 m_data = rhs.m_data;
277 return *this; 307 return *this;
278 } 308 }
279 309
310 NullableCSSValue& operator=(NullableCSSValue&& rhs)
311 {
312 m_data = rhs.m_data.get();
313 rhs.m_data = nullptr;
314 return *this;
315 }
316
280 bool operator==(const NullableCSSValue& rhs) const 317 bool operator==(const NullableCSSValue& rhs) const
281 { 318 {
282 if (m_data == rhs.m_data) 319 if (m_data == rhs.m_data)
283 return true; 320 return true;
284 if (!isObject() || (rhs && !rhs.isObject())) 321 if (!isObject() || (rhs && !rhs.isObject()))
285 return false; 322 return false;
286 return m_data ? rhs.m_data && m_data->equals(*rhs.m_data) : !bool(rhs.m_ data); 323 return m_data ? rhs.m_data && m_data->equals(*rhs.m_data) : !bool(rhs.m_ data);
287 } 324 }
288 325
289 bool operator!=(const NullableCSSValue& rhs) const 326 bool operator!=(const NullableCSSValue& rhs) const
290 { 327 {
291 return !(*this == rhs); 328 return !(*this == rhs);
292 } 329 }
293 330
294 CSSValue& operator*()
295 {
296 ASSERT(m_data);
297 // reinterpret_casts used to avoid ref-churn.
298 return *reinterpret_cast<CSSValue*>(this);
299 }
300
301 const CSSValue& operator*() const 331 const CSSValue& operator*() const
302 { 332 {
303 ASSERT(m_data); 333 ASSERT(m_data);
304 return *reinterpret_cast<const CSSValue*>(this); 334 return *reinterpret_cast<const CSSValue*>(this);
305 } 335 }
306 336
307 CSSValue* operator->()
308 {
309 ASSERT(m_data);
310 return reinterpret_cast<CSSValue*>(this);
311 }
312
313 const CSSValue* operator->() const 337 const CSSValue* operator->() const
314 { 338 {
315 ASSERT(m_data); 339 ASSERT(m_data);
316 return reinterpret_cast<const CSSValue*>(this); 340 return reinterpret_cast<const CSSValue*>(this);
317 } 341 }
318 342
319 void swap(NullableCSSValue& rhs) 343 void swap(NullableCSSValue& rhs)
320 { 344 {
321 std::swap(this->m_data, rhs.m_data); 345 std::swap(this->m_data, rhs.m_data);
322 } 346 }
(...skipping 24 matching lines...) Expand all
347 void deref() const 371 void deref() const
348 { 372 {
349 #if !ENABLE(OILPAN) 373 #if !ENABLE(OILPAN)
350 if (m_data && isObject()) 374 if (m_data && isObject())
351 m_data->deref(); 375 m_data->deref();
352 #endif 376 #endif
353 } 377 }
354 378
355 bool isObject() const 379 bool isObject() const
356 { 380 {
357 return !(*reinterpret_cast<const uintptr_t*>(&m_data) & 1); 381 return !(reinterpret_cast<const uintptr_t>(m_data.get()) & 1);
358 } 382 }
359 383
360 RawPtrWillBeMember<CSSValueObject> m_data; 384 RawPtrWillBeMember<CSSValueObject> m_data;
361 }; 385 };
362 386
363 static_assert(sizeof(CSSValue) == sizeof(void*), "CSSValue should be pointer-siz ed"); 387 static_assert(sizeof(CSSValue) == sizeof(void*), "CSSValue should be pointer-siz ed");
364 static_assert(sizeof(NullableCSSValue) == sizeof(void*), "CSSValue should be poi nter-sized"); 388 static_assert(sizeof(NullableCSSValue) == sizeof(void*), "CSSValue should be poi nter-sized");
365 static_assert(sizeof(CSSValue) == sizeof(NullableCSSValue), "Both CSSValue conta iners must contain the same data"); 389 static_assert(sizeof(CSSValue) == sizeof(NullableCSSValue), "Both CSSValue conta iners must contain the same data");
366 static_assert(sizeof(CSSValue) == sizeof(CSSPrimitiveValue), "Both CSSValue and CSSPrimitiveValue must contain the same data"); 390 static_assert(sizeof(CSSValue) == sizeof(CSSPrimitiveValue), "Both CSSValue and CSSPrimitiveValue must contain the same data");
367 391
(...skipping 14 matching lines...) Expand all
382 return true; 406 return true;
383 } 407 }
384 408
385 #define DEFINE_CSS_VALUE_TYPE_CASTS(thisType, predicate) \ 409 #define DEFINE_CSS_VALUE_TYPE_CASTS(thisType, predicate) \
386 DEFINE_TYPE_CASTS(thisType, CSSValueObject, value, value->predicate, value.p redicate); \ 410 DEFINE_TYPE_CASTS(thisType, CSSValueObject, value, value->predicate, value.p redicate); \
387 inline thisType& to##thisType(const CSSValue& value) \ 411 inline thisType& to##thisType(const CSSValue& value) \
388 { \ 412 { \
389 ASSERT_WITH_SECURITY_IMPLICATION(value.predicate); \ 413 ASSERT_WITH_SECURITY_IMPLICATION(value.predicate); \
390 return *static_cast<thisType*>(value.get()); \ 414 return *static_cast<thisType*>(value.get()); \
391 } \ 415 } \
416 inline thisType& to##thisType(CSSValue&& value) \
417 { \
418 ASSERT_WITH_SECURITY_IMPLICATION(value.predicate); \
419 return *static_cast<thisType*>(value.get()); \
420 } \
421 inline thisType* to##thisType(NullableCSSValue&& value) \
422 { \
423 if (!value) \
424 return nullptr; \
425 ASSERT_WITH_SECURITY_IMPLICATION(value->predicate); \
426 return static_cast<thisType*>(value.get()); \
427 } \
392 inline thisType* to##thisType(const NullableCSSValue& value) \ 428 inline thisType* to##thisType(const NullableCSSValue& value) \
393 { \ 429 { \
394 if (!value) \ 430 if (!value) \
395 return nullptr; \ 431 return nullptr; \
396 ASSERT_WITH_SECURITY_IMPLICATION(value->predicate); \ 432 ASSERT_WITH_SECURITY_IMPLICATION(value->predicate); \
397 return static_cast<thisType*>(value.get()); \ 433 return static_cast<thisType*>(value.get()); \
398 } 434 }
399 435
400 // Returns by value on purpose.
401 inline const CSSPrimitiveValue& toCSSPrimitiveValue(const CSSValue& value) 436 inline const CSSPrimitiveValue& toCSSPrimitiveValue(const CSSValue& value)
402 { 437 {
403 ASSERT_WITH_SECURITY_IMPLICATION(value.isPrimitiveValue()); 438 ASSERT_WITH_SECURITY_IMPLICATION(value.isPrimitiveValue());
404 return *reinterpret_cast<const CSSPrimitiveValue*>(&value); 439 return *reinterpret_cast<const CSSPrimitiveValue*>(&value);
405 } 440 }
406 /* 441
407 inline CSSPrimitiveValue& toCSSPrimitiveValue(const CSSValue& value) 442 inline const CSSPrimitiveValue& toCSSPrimitiveValue(CSSValue&& value)
408 { 443 {
409 ASSERT_WITH_SECURITY_IMPLICATION(value.isPrimitiveValue()); 444 ASSERT_WITH_SECURITY_IMPLICATION(value.isPrimitiveValue());
410 return *const_cast<CSSPrimitiveValue*>(reinterpret_cast<const CSSPrimitiveVa lue*>(&value)); 445 return *reinterpret_cast<const CSSPrimitiveValue*>(&value);
411 } 446 }
412 */
413 447
414 } // namespace blink 448 } // namespace blink
415 449
416 WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::CSSValue); 450 WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::CSSValue);
417 WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::NullableCSSValue); 451 WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::NullableCSSValue);
418 452
419 #endif // CSSValue_h 453 #endif // CSSValue_h
OLDNEW
« no previous file with comments | « Source/core/css/CSSShadowValue.cpp ('k') | Source/core/css/CSSValueList.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698