reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2009 The Android Open Source Project |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 8 | #ifndef sk_tools_Registry_DEFINED |
| 9 | #define sk_tools_Registry_DEFINED |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/private/SkNoncopyable.h" |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 13 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 14 | namespace sk_tools { |
| 15 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 16 | /** Template class that registers itself (in the constructor) into a linked-list |
| 17 | and provides a function-pointer. This can be used to auto-register a set of |
| 18 | services, e.g. a set of image codecs. |
| 19 | */ |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 20 | template <typename T> class Registry : SkNoncopyable { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 21 | public: |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 22 | explicit Registry(T value) : fValue(value) { |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 23 | #ifdef SK_BUILD_FOR_ANDROID |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 24 | // work-around for double-initialization bug |
| 25 | { |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 26 | Registry* reg = gHead; |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 27 | while (reg) { |
| 28 | if (reg == this) { |
| 29 | return; |
| 30 | } |
| 31 | reg = reg->fChain; |
| 32 | } |
| 33 | } |
| 34 | #endif |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 35 | fChain = gHead; |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 36 | gHead = this; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 39 | static const Registry* Head() { return gHead; } |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 40 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 41 | const Registry* next() const { return fChain; } |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 42 | const T& get() const { return fValue; } |
| 43 | |
| 44 | // for (const T& t : sk_tools::Registry<T>::Range()) { process(t); } |
| 45 | struct Range { |
| 46 | struct Iterator { |
| 47 | const Registry* fPtr; |
| 48 | const T& operator*() { return SkASSERT(fPtr), fPtr->get(); } |
| 49 | void operator++() { if (fPtr) { fPtr = fPtr->next(); } } |
| 50 | bool operator!=(const Iterator& other) const { return fPtr != other.fPtr; } |
| 51 | }; |
| 52 | Iterator begin() const { return Iterator{Registry::Head()}; } |
| 53 | Iterator end() const { return Iterator{nullptr}; } |
| 54 | }; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
Hal Canary | 972eba3 | 2018-07-30 17:07:07 -0400 | [diff] [blame] | 57 | T fValue; |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 58 | Registry* fChain; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 59 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 60 | static Registry* gHead; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 63 | // The caller still needs to declare an instance of this somewhere |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 64 | template <typename T> Registry<T>* Registry<T>::gHead; |
| 65 | |
| 66 | } // namespace sk_tools |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 67 | |
| 68 | #endif |