• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2009, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SkTRegistry_DEFINED
18 #define SkTRegistry_DEFINED
19 
20 #include "SkTypes.h"
21 
22 /** Template class that registers itself (in the constructor) into a linked-list
23     and provides a function-pointer. This can be used to auto-register a set of
24     services, e.g. a set of image codecs.
25  */
26 template <typename T, typename P> class SkTRegistry : SkNoncopyable {
27 public:
28     typedef T (*Factory)(P);
29 
SkTRegistry(Factory fact)30     SkTRegistry(Factory fact) {
31 #ifdef ANDROID
32         // work-around for double-initialization bug
33         {
34             SkTRegistry* reg = gHead;
35             while (reg) {
36                 if (reg == this) {
37                     return;
38                 }
39                 reg = reg->fChain;
40             }
41         }
42 #endif
43         fFact = fact;
44         fChain = gHead;
45         gHead = this;
46     }
47 
Head()48     static const SkTRegistry* Head() { return gHead; }
49 
next()50     const SkTRegistry* next() const { return fChain; }
factory()51     Factory factory() const { return fFact; }
52 
53 private:
54     Factory      fFact;
55     SkTRegistry* fChain;
56 
57     static SkTRegistry* gHead;
58 };
59 
60 // The caller still needs to declare an instance of this somewhere
61 template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead;
62 
63 #endif
64