Flutter Engine
The Flutter Engine
Registry.h
Go to the documentation of this file.
1/*
2 * Copyright 2009 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef sk_tools_Registry_DEFINED
9#define sk_tools_Registry_DEFINED
10
13
14namespace sk_tools {
15
16/**
17 * Template class that registers itself (in the constructor) into a linked-list
18 * and provides a function-pointer. This can be used to auto-register a set of
19 * services, e.g. a set of image codecs.
20 */
21template <typename T> class Registry : SkNoncopyable {
22public:
23 explicit Registry(T value, bool condition = true) : fValue(value) {
24 if (condition) {
25 this->linkToRegistryHead();
26 }
27 }
28
29 static const Registry* Head() { return gHead; }
30
31 const Registry* next() const { return fChain; }
32 const T& get() const { return fValue; }
33
34 // for (const T& t : sk_tools::Registry<T>::Range()) { process(t); }
35 struct Range {
36 struct Iterator {
37 const Registry* fPtr;
38 const T& operator*() { return SkASSERT(fPtr), fPtr->get(); }
39 void operator++() { if (fPtr) { fPtr = fPtr->next(); } }
40 bool operator!=(const Iterator& other) const { return fPtr != other.fPtr; }
41 };
42 Iterator begin() const { return Iterator{Registry::Head()}; }
43 Iterator end() const { return Iterator{nullptr}; }
44 };
45
46private:
47 void linkToRegistryHead() {
48 fChain = gHead;
49 gHead = this;
50 }
51
52 T fValue;
53 Registry* fChain;
54
55 static Registry* gHead;
56};
57
58// The caller still needs to declare an instance of this somewhere
59template <typename T> Registry<T>* Registry<T>::gHead;
60
61} // namespace sk_tools
62
63#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
static const Registry * Head()
Definition: Registry.h:29
const T & get() const
Definition: Registry.h:32
const Registry * next() const
Definition: Registry.h:31
Registry(T value, bool condition=true)
Definition: Registry.h:23
uint8_t value
#define T
Definition: precompiler.cc:65
bool operator!=(const Iterator &other) const
Definition: Registry.h:40
Iterator begin() const
Definition: Registry.h:42
Iterator end() const
Definition: Registry.h:43