Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2009 The Android Open Source Project | 3 * Copyright 2009 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkTRegistry_DEFINED | 10 #ifndef SkTRegistry_DEFINED |
| 11 #define SkTRegistry_DEFINED | 11 #define SkTRegistry_DEFINED |
| 12 | 12 |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 | 14 |
| 15 /** Template class that registers itself (in the constructor) into a linked-list | 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 | 16 and provides a user-defined type. This can be used to auto-register a set of |
| 17 services, e.g. a set of image codecs. | 17 services, e.g. a set of image codecs. |
| 18 */ | 18 */ |
| 19 template <typename T, typename P> class SkTRegistry : SkNoncopyable { | 19 template <typename T> class SkTRegistry : SkNoncopyable { |
| 20 public: | 20 public: |
| 21 typedef T (*Factory)(P); | 21 typedef T Data; |
|
scroggo
2013/09/04 15:40:21
Would it make sense to write a partial specializat
| |
| 22 | 22 explicit SkTRegistry(T data) { |
|
reed1
2013/09/04 15:15:23
(const T&) so we don't force the caller to make a
mtklein
2013/09/04 16:36:46
As we talked.
| |
| 23 SkTRegistry(Factory fact) { | |
| 24 #ifdef SK_BUILD_FOR_ANDROID | 23 #ifdef SK_BUILD_FOR_ANDROID |
| 25 // work-around for double-initialization bug | 24 // work-around for double-initialization bug |
| 26 { | 25 { |
| 27 SkTRegistry* reg = gHead; | 26 SkTRegistry* reg = gHead; |
| 28 while (reg) { | 27 while (reg) { |
| 29 if (reg == this) { | 28 if (reg == this) { |
| 30 return; | 29 return; |
| 31 } | 30 } |
| 32 reg = reg->fChain; | 31 reg = reg->fChain; |
| 33 } | 32 } |
| 34 } | 33 } |
| 35 #endif | 34 #endif |
| 36 fFact = fact; | 35 fData = data; |
| 37 fChain = gHead; | 36 fChain = gHead; |
| 38 gHead = this; | 37 gHead = this; |
| 39 } | 38 } |
| 40 | 39 |
| 41 static const SkTRegistry* Head() { return gHead; } | 40 static const SkTRegistry* Head() { return gHead; } |
| 42 | 41 |
| 43 const SkTRegistry* next() const { return fChain; } | 42 const SkTRegistry* next() const { return fChain; } |
| 44 Factory factory() const { return fFact; } | 43 Data data() const { return fData; } // fix |
|
reed1
2013/09/04 15:15:23
const Data& data() const { ... } ?
mtklein
2013/09/04 16:36:46
Done.
| |
| 45 | 44 |
| 46 private: | 45 private: |
| 47 Factory fFact; | 46 Data fData; |
| 48 SkTRegistry* fChain; | 47 SkTRegistry* fChain; |
| 49 | 48 |
| 50 static SkTRegistry* gHead; | 49 static SkTRegistry* gHead; |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 // The caller still needs to declare an instance of this somewhere | 52 // The caller still needs to declare an instance of this somewhere |
| 54 template <typename T, typename P> SkTRegistry<T, P>* SkTRegistry<T, P>::gHead; | 53 template <typename T> SkTRegistry<T>* SkTRegistry<T>::gHead; |
| 55 | 54 |
| 56 #endif | 55 #endif |
| OLD | NEW |