Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DisplayItemClient_h | 5 #ifndef DisplayItemClient_h |
| 6 #define DisplayItemClient_h | 6 #define DisplayItemClient_h |
| 7 | 7 |
| 8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/heap/Heap.h" | 9 #include "platform/heap/Heap.h" |
| 10 #include "wtf/text/WTFString.h" | 10 #include "wtf/text/WTFString.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 class DisplayItemClientInternalVoid; | 14 class DisplayItemClientInternalVoid; |
| 15 using DisplayItemClient = const DisplayItemClientInternalVoid*; | 15 using DisplayItemClient = const DisplayItemClientInternalVoid*; |
| 16 | 16 |
| 17 inline DisplayItemClient toDisplayItemClient(const void* object) { return static _cast<DisplayItemClient>(object); } | 17 inline DisplayItemClient toDisplayItemClient(const void* object) { return static _cast<DisplayItemClient>(object); } |
| 18 | 18 |
| 19 // Allows one object to create multiple sub display item clients, each identifie d by subClientId. | |
| 20 template <size_t subClientId, typename T> | |
| 21 inline DisplayItemClient toSubDisplayItemClient(const T* object) | |
| 22 { | |
| 23 static_assert(subClientId > 0 && subClientId < sizeof(T), "subClientId must be non-zero and within the size of the object"); | |
| 24 return reinterpret_cast<DisplayItemClient>(reinterpret_cast<const char*>(obj ect) + subClientId); | |
|
chrishtr
2015/11/04 23:37:11
This is kind of hacky. Why do it this way?
Xianzhu
2015/11/05 00:30:28
To allow one object to have multiple DisplayItemCl
| |
| 25 } | |
| 26 | |
| 19 // Used to pass DisplayItemClient and debugName() (called only when needed) from | 27 // Used to pass DisplayItemClient and debugName() (called only when needed) from |
| 20 // core/layout module etc. to platform/paint module. | 28 // core/layout module etc. to platform/paint module. |
| 21 // The instance must not out-live the object. Long-time reference to a client mu st | 29 // The instance must not out-live the object. Long-time reference to a client mu st |
| 22 // use DisplayItemClient. | 30 // use DisplayItemClient. |
| 23 class PLATFORM_EXPORT DisplayItemClientWrapper { | 31 class PLATFORM_EXPORT DisplayItemClientWrapper { |
| 24 DISALLOW_NEW(); // Allow allocated in stack or in another object only. | 32 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); // Allow allocated in stack or in anoth er object only. |
| 25 public: | 33 public: |
| 26 template <typename T> | 34 template <typename T> |
| 27 DisplayItemClientWrapper(const T& object) | 35 DisplayItemClientWrapper(const T& object) |
| 28 : m_displayItemClient(object.displayItemClient()) | 36 : m_displayItemClient(object.displayItemClient()) |
| 29 , m_object(reinterpret_cast<const GenericClass&>(object)) | 37 , m_object(reinterpret_cast<const GenericClass&>(object)) |
| 30 , m_debugNameInvoker(&invokeDebugName<T>) | 38 , m_debugNameInvoker(&invokeDebugName<T>) |
| 31 { } | 39 { } |
| 32 | 40 |
| 41 // Allows one object to create multiple sub display item clients. Use like: | |
| 42 // DisplayItemClientWrapper(object, toSubDisplayItemClient<SubClientId>(&obj ect), functionToGetDebugNameOfSubClient) | |
| 43 template <typename T> | |
| 44 DisplayItemClientWrapper(const T& object, DisplayItemClient displayItemClien t, String (*debugNameInvoker)(const T&)) | |
| 45 : m_displayItemClient(displayItemClient) | |
| 46 , m_object(reinterpret_cast<const GenericClass&>(object)) | |
| 47 , m_debugNameInvoker(reinterpret_cast<DebugNameInvoker>(debugNameInvoker )) | |
| 48 { } | |
| 49 | |
| 33 DisplayItemClientWrapper(const DisplayItemClientWrapper& other) | 50 DisplayItemClientWrapper(const DisplayItemClientWrapper& other) |
| 34 : m_displayItemClient(other.m_displayItemClient) | 51 : m_displayItemClient(other.m_displayItemClient) |
| 35 , m_object(other.m_object) | 52 , m_object(other.m_object) |
| 36 , m_debugNameInvoker(other.m_debugNameInvoker) | 53 , m_debugNameInvoker(other.m_debugNameInvoker) |
| 37 { } | 54 { } |
| 38 | 55 |
| 39 DisplayItemClient displayItemClient() const { return m_displayItemClient; } | 56 DisplayItemClient displayItemClient() const { return m_displayItemClient; } |
| 40 String debugName() const { return m_debugNameInvoker(m_object); } | 57 String debugName() const { return m_debugNameInvoker(m_object); } |
| 41 | 58 |
| 42 private: | 59 private: |
| 43 DisplayItemClientWrapper& operator=(const DisplayItemClientWrapper&) = delet e; | 60 DisplayItemClientWrapper& operator=(const DisplayItemClientWrapper&) = delet e; |
| 44 | 61 |
| 45 class GenericClass; | 62 class GenericClass; |
| 46 template <typename T> | 63 template <typename T> |
| 47 static String invokeDebugName(const GenericClass& object) { return reinterpr et_cast<const T&>(object).debugName(); } | 64 static String invokeDebugName(const GenericClass& object) { return reinterpr et_cast<const T&>(object).debugName(); } |
| 48 | 65 |
| 66 using DebugNameInvoker = String (*)(const GenericClass&); | |
| 49 DisplayItemClient m_displayItemClient; | 67 DisplayItemClient m_displayItemClient; |
| 50 const GenericClass& m_object; | 68 const GenericClass& m_object; |
| 51 using DebugNameInvoker = String(*)(const GenericClass&); | |
| 52 DebugNameInvoker m_debugNameInvoker; | 69 DebugNameInvoker m_debugNameInvoker; |
| 53 }; | 70 }; |
| 54 | 71 |
| 55 } | 72 } |
| 56 | 73 |
| 57 #endif // DisplayItemClient_h | 74 #endif // DisplayItemClient_h |
| OLD | NEW |