blob: 34fcffd5c51374cd422395fa7e547c287666a3b7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com00bf85a2009-01-22 13:04:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2009 The Android Open Source Project
reed@android.com00bf85a2009-01-22 13:04:56 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com00bf85a2009-01-22 13:04:56 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com00bf85a2009-01-22 13:04:56 +000010#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 */
19template <typename T, typename P> class SkTRegistry : SkNoncopyable {
20public:
21 typedef T (*Factory)(P);
22
23 SkTRegistry(Factory fact) {
djsollen@google.com56c69772011-11-08 19:00:26 +000024#ifdef SK_BUILD_FOR_ANDROID
reed@android.com57b799e2009-04-01 20:26:42 +000025 // 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.com00bf85a2009-01-22 13:04:56 +000036 fFact = fact;
37 fChain = gHead;
38 gHead = this;
39 }
40
41 static const SkTRegistry* Head() { return gHead; }
42
43 const SkTRegistry* next() const { return fChain; }
44 Factory factory() const { return fFact; }
45
46private:
47 Factory fFact;
48 SkTRegistry* fChain;
49
50 static SkTRegistry* gHead;
51};
52
reed@android.comf523e252009-01-26 23:15:37 +000053// The caller still needs to declare an instance of this somewhere
reed@android.com00bf85a2009-01-22 13:04:56 +000054template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead;
55
56#endif