epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2009 The Android Open Source Project |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 10 | #ifndef SkTRegistry_DEFINED |
| 11 | #define SkTRegistry_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | /** Template class that registers itself (in the constructor) into a linked-list |
| 16 | and provides a function-pointer. This can be used to auto-register a set of |
| 17 | services, e.g. a set of image codecs. |
| 18 | */ |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 19 | template <typename T> class SkTRegistry : SkNoncopyable { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 20 | public: |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 21 | typedef T Factory; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 22 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 23 | explicit SkTRegistry(T fact) : fFact(fact) { |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 24 | #ifdef SK_BUILD_FOR_ANDROID |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 25 | // work-around for double-initialization bug |
| 26 | { |
| 27 | SkTRegistry* reg = gHead; |
| 28 | while (reg) { |
| 29 | if (reg == this) { |
| 30 | return; |
| 31 | } |
| 32 | reg = reg->fChain; |
| 33 | } |
| 34 | } |
| 35 | #endif |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 36 | fChain = gHead; |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 37 | gHead = this; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static const SkTRegistry* Head() { return gHead; } |
| 41 | |
| 42 | const SkTRegistry* next() const { return fChain; } |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 43 | const Factory& factory() const { return fFact; } |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | Factory fFact; |
| 47 | SkTRegistry* fChain; |
| 48 | |
| 49 | static SkTRegistry* gHead; |
| 50 | }; |
| 51 | |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 52 | // The caller still needs to declare an instance of this somewhere |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 53 | template <typename T> SkTRegistry<T>* SkTRegistry<T>::gHead; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 54 | |
| 55 | #endif |