blob: 78c5a0734ef409c2c05def3cae1b2ad29c0aab99 [file] [log] [blame]
reed@android.com00bf85a2009-01-22 13:04:56 +00001/*
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 */
26template <typename T, typename P> class SkTRegistry : SkNoncopyable {
27public:
28 typedef T (*Factory)(P);
29
30 SkTRegistry(Factory fact) {
reed@android.com57b799e2009-04-01 20:26:42 +000031#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.com00bf85a2009-01-22 13:04:56 +000043 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
53private:
54 Factory fFact;
55 SkTRegistry* fChain;
56
57 static SkTRegistry* gHead;
58};
59
reed@android.comf523e252009-01-26 23:15:37 +000060// The caller still needs to declare an instance of this somewhere
reed@android.com00bf85a2009-01-22 13:04:56 +000061template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead;
62
63#endif