blob: 6a1e24a303b64f8d2ce1e1cbf2b29092a620942f [file] [log] [blame]
reed@android.com00bf85a2009-01-22 13:04:56 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2009 The Android Open Source Project
reed@android.com00bf85a2009-01-22 13:04:56 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com00bf85a2009-01-22 13:04:56 +00006 */
7
Mike Reedab273fa2017-01-11 13:58:55 -05008#ifndef sk_tools_Registry_DEFINED
9#define sk_tools_Registry_DEFINED
reed@android.com00bf85a2009-01-22 13:04:56 +000010
11#include "SkTypes.h"
12
Mike Reedab273fa2017-01-11 13:58:55 -050013namespace sk_tools {
14
reed@android.com00bf85a2009-01-22 13:04:56 +000015/** 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 */
Mike Reedab273fa2017-01-11 13:58:55 -050019template <typename T> class Registry : SkNoncopyable {
reed@android.com00bf85a2009-01-22 13:04:56 +000020public:
mtklein@google.combd6343b2013-09-04 17:20:18 +000021 typedef T Factory;
reed@android.com00bf85a2009-01-22 13:04:56 +000022
Mike Reedab273fa2017-01-11 13:58:55 -050023 explicit Registry(T fact) : fFact(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 {
Mike Reedab273fa2017-01-11 13:58:55 -050027 Registry* reg = gHead;
reed@android.com57b799e2009-04-01 20:26:42 +000028 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 fChain = gHead;
mtklein@google.combd6343b2013-09-04 17:20:18 +000037 gHead = this;
reed@android.com00bf85a2009-01-22 13:04:56 +000038 }
39
Mike Reedab273fa2017-01-11 13:58:55 -050040 static const Registry* Head() { return gHead; }
reed@android.com00bf85a2009-01-22 13:04:56 +000041
Mike Reedab273fa2017-01-11 13:58:55 -050042 const Registry* next() const { return fChain; }
mtklein@google.combd6343b2013-09-04 17:20:18 +000043 const Factory& factory() const { return fFact; }
reed@android.com00bf85a2009-01-22 13:04:56 +000044
45private:
Mike Reedab273fa2017-01-11 13:58:55 -050046 Factory fFact;
47 Registry* fChain;
reed@android.com00bf85a2009-01-22 13:04:56 +000048
Mike Reedab273fa2017-01-11 13:58:55 -050049 static Registry* gHead;
reed@android.com00bf85a2009-01-22 13:04:56 +000050};
51
reed@android.comf523e252009-01-26 23:15:37 +000052// The caller still needs to declare an instance of this somewhere
Mike Reedab273fa2017-01-11 13:58:55 -050053template <typename T> Registry<T>* Registry<T>::gHead;
54
55} // namespace sk_tools
reed@android.com00bf85a2009-01-22 13:04:56 +000056
57#endif