reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkTRegistry_DEFINED |
| 18 | #define SkTRegistry_DEFINED |
| 19 | |
| 20 | #include "SkTypes.h" |
| 21 | |
| 22 | /** Template class that registers itself (in the constructor) into a linked-list |
| 23 | and provides a function-pointer. This can be used to auto-register a set of |
| 24 | services, e.g. a set of image codecs. |
| 25 | */ |
| 26 | template <typename T, typename P> class SkTRegistry : SkNoncopyable { |
| 27 | public: |
| 28 | typedef T (*Factory)(P); |
| 29 | |
| 30 | SkTRegistry(Factory fact) { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame^] | 31 | #ifdef ANDROID |
| 32 | // work-around for double-initialization bug |
| 33 | { |
| 34 | SkTRegistry* reg = gHead; |
| 35 | while (reg) { |
| 36 | if (reg == this) { |
| 37 | return; |
| 38 | } |
| 39 | reg = reg->fChain; |
| 40 | } |
| 41 | } |
| 42 | #endif |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 43 | fFact = fact; |
| 44 | fChain = gHead; |
| 45 | gHead = this; |
| 46 | } |
| 47 | |
| 48 | static const SkTRegistry* Head() { return gHead; } |
| 49 | |
| 50 | const SkTRegistry* next() const { return fChain; } |
| 51 | Factory factory() const { return fFact; } |
| 52 | |
| 53 | private: |
| 54 | Factory fFact; |
| 55 | SkTRegistry* fChain; |
| 56 | |
| 57 | static SkTRegistry* gHead; |
| 58 | }; |
| 59 | |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 60 | // The caller still needs to declare an instance of this somewhere |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 61 | template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead; |
| 62 | |
| 63 | #endif |