gfx/skia/trunk/include/core/SkTRegistry.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkTRegistry.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,55 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2009 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkTRegistry_DEFINED
    1.14 +#define SkTRegistry_DEFINED
    1.15 +
    1.16 +#include "SkTypes.h"
    1.17 +
    1.18 +/** Template class that registers itself (in the constructor) into a linked-list
    1.19 +    and provides a function-pointer. This can be used to auto-register a set of
    1.20 +    services, e.g. a set of image codecs.
    1.21 + */
    1.22 +template <typename T> class SkTRegistry : SkNoncopyable {
    1.23 +public:
    1.24 +    typedef T Factory;
    1.25 +
    1.26 +    explicit SkTRegistry(T fact) : fFact(fact) {
    1.27 +#ifdef SK_BUILD_FOR_ANDROID
    1.28 +        // work-around for double-initialization bug
    1.29 +        {
    1.30 +            SkTRegistry* reg = gHead;
    1.31 +            while (reg) {
    1.32 +                if (reg == this) {
    1.33 +                    return;
    1.34 +                }
    1.35 +                reg = reg->fChain;
    1.36 +            }
    1.37 +        }
    1.38 +#endif
    1.39 +        fChain = gHead;
    1.40 +        gHead  = this;
    1.41 +    }
    1.42 +
    1.43 +    static const SkTRegistry* Head() { return gHead; }
    1.44 +
    1.45 +    const SkTRegistry* next() const { return fChain; }
    1.46 +    const Factory& factory() const { return fFact; }
    1.47 +
    1.48 +private:
    1.49 +    Factory      fFact;
    1.50 +    SkTRegistry* fChain;
    1.51 +
    1.52 +    static SkTRegistry* gHead;
    1.53 +};
    1.54 +
    1.55 +// The caller still needs to declare an instance of this somewhere
    1.56 +template <typename T> SkTRegistry<T>* SkTRegistry<T>::gHead;
    1.57 +
    1.58 +#endif

mercurial