1 //=== Registry.h - Linker-supported plugin registries -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines a registry template for discovering pluggable modules.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_REGISTRY_H
15 #define LLVM_SUPPORT_REGISTRY_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Compiler.h"
22 /// A simple registry entry which provides only a name, description, and
23 /// no-argument constructor.
25 class SimpleRegistryEntry {
26 const char *Name, *Desc;
27 std::unique_ptr<T> (*Ctor)();
30 SimpleRegistryEntry(const char *N, const char *D, std::unique_ptr<T> (*C)())
31 : Name(N), Desc(D), Ctor(C)
34 const char *getName() const { return Name; }
35 const char *getDesc() const { return Desc; }
36 std::unique_ptr<T> instantiate() const { return Ctor(); }
40 /// Traits for registry entries. If using other than SimpleRegistryEntry, it
41 /// is necessary to define an alternate traits class.
43 class RegistryTraits {
44 RegistryTraits() = delete;
47 typedef SimpleRegistryEntry<T> entry;
49 /// nameof/descof - Accessors for name and description of entries. These are
50 // used to generate help for command-line options.
51 static const char *nameof(const entry &Entry) { return Entry.getName(); }
52 static const char *descof(const entry &Entry) { return Entry.getDesc(); }
56 /// A global registry used in conjunction with static constructors to make
57 /// pluggable components (like targets or garbage collectors) "just work" when
58 /// linked with an executable.
59 template <typename T, typename U = RegistryTraits<T> >
63 typedef typename U::entry entry;
72 static void Announce(const entry &E) {
73 for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
78 static node *Head, *Tail;
80 friend class listener;
81 static listener *ListenerHead, *ListenerTail;
84 /// Node in linked list of entries.
87 friend class iterator;
93 node(const entry& V) : Next(nullptr), Val(V) {
105 /// Iterators for registry entries.
111 explicit iterator(const node *N) : Cur(N) {}
113 bool operator==(const iterator &That) const { return Cur == That.Cur; }
114 bool operator!=(const iterator &That) const { return Cur != That.Cur; }
115 iterator &operator++() { Cur = Cur->Next; return *this; }
116 const entry &operator*() const { return Cur->Val; }
117 const entry *operator->() const { return &Cur->Val; }
120 static iterator begin() { return iterator(Head); }
121 static iterator end() { return iterator(nullptr); }
123 static iterator_range<iterator> entries() {
124 return iterator_range<iterator>(begin(), end());
128 /// Abstract base class for registry listeners, which are informed when new
129 /// entries are added to the registry. Simply subclass and instantiate:
132 /// class CollectorPrinter : public Registry<Collector>::listener {
134 /// void registered(const Registry<Collector>::entry &e) {
135 /// cerr << "collector now available: " << e->getName() << "\n";
139 /// CollectorPrinter() { init(); } // Print those already registered.
142 /// CollectorPrinter Printer;
145 listener *Prev, *Next;
147 friend void Registry::Announce(const entry &E);
150 /// Called when an entry is added to the registry.
152 virtual void registered(const entry &) = 0;
154 /// Calls 'registered' for each pre-existing entry.
157 for (iterator I = begin(), E = end(); I != E; ++I)
162 listener() : Prev(ListenerTail), Next(0) {
170 virtual ~listener() {
183 /// A static registration template. Use like such:
185 /// Registry<Collector>::Add<FancyGC>
186 /// X("fancy-gc", "Newfangled garbage collector.");
188 /// Use of this template requires that:
190 /// 1. The registered subclass has a default constructor.
192 /// 2. The registry entry type has a constructor compatible with this
195 /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
197 /// If you have more elaborate requirements, then copy and modify.
199 template <typename V>
204 static std::unique_ptr<T> CtorFn() { return make_unique<V>(); }
207 Add(const char *Name, const char *Desc)
208 : Entry(Name, Desc, CtorFn), Node(Entry) {}
211 /// Registry::Parser now lives in llvm/Support/RegistryParser.h.
215 // Since these are defined in a header file, plugins must be sure to export
218 template <typename T, typename U>
219 typename Registry<T,U>::node *Registry<T,U>::Head;
221 template <typename T, typename U>
222 typename Registry<T,U>::node *Registry<T,U>::Tail;
224 template <typename T, typename U>
225 typename Registry<T,U>::listener *Registry<T,U>::ListenerHead;
227 template <typename T, typename U>
228 typename Registry<T,U>::listener *Registry<T,U>::ListenerTail;