1/*
2 * Copyright 2009 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef sk_tools_Registry_DEFINED
9#define sk_tools_Registry_DEFINED
10
11#include "SkTypes.h"
12
13namespace sk_tools {
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> class Registry : SkNoncopyable {
20public:
21    typedef T Factory;
22
23    explicit Registry(T fact) : fFact(fact) {
24#ifdef SK_BUILD_FOR_ANDROID
25        // work-around for double-initialization bug
26        {
27            Registry* reg = gHead;
28            while (reg) {
29                if (reg == this) {
30                    return;
31                }
32                reg = reg->fChain;
33            }
34        }
35#endif
36        fChain = gHead;
37        gHead  = this;
38    }
39
40    static const Registry* Head() { return gHead; }
41
42    const Registry* next() const { return fChain; }
43    const Factory& factory() const { return fFact; }
44
45private:
46    Factory   fFact;
47    Registry* fChain;
48
49    static Registry* gHead;
50};
51
52// The caller still needs to declare an instance of this somewhere
53template <typename T> Registry<T>* Registry<T>::gHead;
54
55}  // namespace sk_tools
56
57#endif
58