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) { |
| 31 | fFact = fact; |
| 32 | fChain = gHead; |
| 33 | gHead = this; |
| 34 | } |
| 35 | |
| 36 | static const SkTRegistry* Head() { return gHead; } |
| 37 | |
| 38 | const SkTRegistry* next() const { return fChain; } |
| 39 | Factory factory() const { return fFact; } |
| 40 | |
| 41 | private: |
| 42 | Factory fFact; |
| 43 | SkTRegistry* fChain; |
| 44 | |
| 45 | static SkTRegistry* gHead; |
| 46 | }; |
| 47 | |
| 48 | template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead; |
| 49 | |
| 50 | #endif |