| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkMessageBus_DEFINED | 8 #ifndef SkMessageBus_DEFINED |
| 9 #define SkMessageBus_DEFINED | 9 #define SkMessageBus_DEFINED |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 SkTDArray<Message> fMessages; | 31 SkTDArray<Message> fMessages; |
| 32 SkMutex fMessagesMutex; | 32 SkMutex fMessagesMutex; |
| 33 | 33 |
| 34 friend class SkMessageBus; | 34 friend class SkMessageBus; |
| 35 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. | 35 void receive(const Message& m); // SkMessageBus is a friend only to cal
l this. |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 SkMessageBus(); | 39 SkMessageBus(); |
| 40 static SkMessageBus* Get(); | 40 static SkMessageBus* Get(); |
| 41 static SkMessageBus* New(); | 41 |
| 42 // Allow SkLazyPtr to call SkMessageBus::SkMessageBus(). |
| 43 template <typename T> friend T* Private::sk_new(); |
| 42 | 44 |
| 43 SkTDArray<Inbox*> fInboxes; | 45 SkTDArray<Inbox*> fInboxes; |
| 44 SkMutex fInboxesMutex; | 46 SkMutex fInboxesMutex; |
| 45 }; | 47 }; |
| 46 | 48 |
| 47 // This must go in a single .cpp file, not some .h, or we risk creating more tha
n one global | 49 // This must go in a single .cpp file, not some .h, or we risk creating more tha
n one global |
| 48 // SkMessageBus per type when using shared libraries. | 50 // SkMessageBus per type when using shared libraries. NOTE: at most one per fil
e will compile. |
| 49 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ | 51 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ |
| 50 template <> \ | 52 SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus); \ |
| 51 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ | 53 template <> \ |
| 52 SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus, New); \ | 54 SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ |
| 53 return bus.get(); \ | 55 return bus.get(); \ |
| 54 } | 56 } |
| 55 | 57 |
| 56 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- | 58 // ----------------------- Implementation of SkMessageBus::Inbox -------------
---------- |
| 57 | 59 |
| 58 template<typename Message> | 60 template<typename Message> |
| 59 SkMessageBus<Message>::Inbox::Inbox() { | 61 SkMessageBus<Message>::Inbox::Inbox() { |
| 60 // Register ourselves with the corresponding message bus. | 62 // Register ourselves with the corresponding message bus. |
| 61 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 63 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 62 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 64 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 63 bus->fInboxes.push(this); | 65 bus->fInboxes.push(this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 90 SkAutoMutexAcquire lock(fMessagesMutex); | 92 SkAutoMutexAcquire lock(fMessagesMutex); |
| 91 messages->swap(fMessages); | 93 messages->swap(fMessages); |
| 92 } | 94 } |
| 93 | 95 |
| 94 // ----------------------- Implementation of SkMessageBus --------------------
--- | 96 // ----------------------- Implementation of SkMessageBus --------------------
--- |
| 95 | 97 |
| 96 template <typename Message> | 98 template <typename Message> |
| 97 SkMessageBus<Message>::SkMessageBus() {} | 99 SkMessageBus<Message>::SkMessageBus() {} |
| 98 | 100 |
| 99 template <typename Message> | 101 template <typename Message> |
| 100 /*static*/ SkMessageBus<Message>* SkMessageBus<Message>::New() { | |
| 101 return SkNEW(SkMessageBus<Message>); | |
| 102 } | |
| 103 | |
| 104 template <typename Message> | |
| 105 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { | 102 /*static*/ void SkMessageBus<Message>::Post(const Message& m) { |
| 106 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); | 103 SkMessageBus<Message>* bus = SkMessageBus<Message>::Get(); |
| 107 SkAutoMutexAcquire lock(bus->fInboxesMutex); | 104 SkAutoMutexAcquire lock(bus->fInboxesMutex); |
| 108 for (int i = 0; i < bus->fInboxes.count(); i++) { | 105 for (int i = 0; i < bus->fInboxes.count(); i++) { |
| 109 bus->fInboxes[i]->receive(m); | 106 bus->fInboxes[i]->receive(m); |
| 110 } | 107 } |
| 111 } | 108 } |
| 112 | 109 |
| 113 #endif // SkMessageBus_DEFINED | 110 #endif // SkMessageBus_DEFINED |
| OLD | NEW |