michael@0: michael@0: /* michael@0: * Copyright 2009 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkTRegistry_DEFINED michael@0: #define SkTRegistry_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: /** Template class that registers itself (in the constructor) into a linked-list michael@0: and provides a function-pointer. This can be used to auto-register a set of michael@0: services, e.g. a set of image codecs. michael@0: */ michael@0: template class SkTRegistry : SkNoncopyable { michael@0: public: michael@0: typedef T Factory; michael@0: michael@0: explicit SkTRegistry(T fact) : fFact(fact) { michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: // work-around for double-initialization bug michael@0: { michael@0: SkTRegistry* reg = gHead; michael@0: while (reg) { michael@0: if (reg == this) { michael@0: return; michael@0: } michael@0: reg = reg->fChain; michael@0: } michael@0: } michael@0: #endif michael@0: fChain = gHead; michael@0: gHead = this; michael@0: } michael@0: michael@0: static const SkTRegistry* Head() { return gHead; } michael@0: michael@0: const SkTRegistry* next() const { return fChain; } michael@0: const Factory& factory() const { return fFact; } michael@0: michael@0: private: michael@0: Factory fFact; michael@0: SkTRegistry* fChain; michael@0: michael@0: static SkTRegistry* gHead; michael@0: }; michael@0: michael@0: // The caller still needs to declare an instance of this somewhere michael@0: template SkTRegistry* SkTRegistry::gHead; michael@0: michael@0: #endif