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/iterator_range.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Compiler.h"
23 /// A simple registry entry which provides only a name, description, and
24 /// no-argument constructor.
26 class SimpleRegistryEntry {
27 const char *Name, *Desc;
28 std::unique_ptr<T> (*Ctor)();
31 SimpleRegistryEntry(const char *N, const char *D, std::unique_ptr<T> (*C)())
32 : Name(N), Desc(D), Ctor(C)
35 const char *getName() const { return Name; }
36 const char *getDesc() const { return Desc; }
37 std::unique_ptr<T> instantiate() const { return Ctor(); }
41 /// Traits for registry entries. If using other than SimpleRegistryEntry, it
42 /// is necessary to define an alternate traits class.
44 class RegistryTraits {
45 RegistryTraits() = delete;
48 typedef SimpleRegistryEntry<T> entry;
50 /// nameof/descof - Accessors for name and description of entries. These are
51 // used to generate help for command-line options.
52 static const char *nameof(const entry &Entry) { return Entry.getName(); }
53 static const char *descof(const entry &Entry) { return Entry.getDesc(); }
57 /// A global registry used in conjunction with static constructors to make
58 /// pluggable components (like targets or garbage collectors) "just work" when
59 /// linked with an executable.
60 template <typename T, typename U = RegistryTraits<T> >
64 typedef typename U::entry entry;
73 static void Announce(const entry &E) {
74 for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
79 static node *Head, *Tail;
81 friend class listener;
82 static listener *ListenerHead, *ListenerTail;
85 /// Node in linked list of entries.
88 friend class iterator;
94 node(const entry& V) : Next(nullptr), Val(V) {
106 /// Iterators for registry entries.
112 explicit iterator(const node *N) : Cur(N) {}
114 bool operator==(const iterator &That) const { return Cur == That.Cur; }
115 bool operator!=(const iterator &That) const { return Cur != That.Cur; }
116 iterator &operator++() { Cur = Cur->Next; return *this; }
117 const entry &operator*() const { return Cur->Val; }
118 const entry *operator->() const { return &Cur->Val; }
121 static iterator begin() { return iterator(Head); }
122 static iterator end() { return iterator(nullptr); }
124 static iterator_range<iterator> entries() {
125 return iterator_range<iterator>(begin(), end());
129 /// Abstract base class for registry listeners, which are informed when new
130 /// entries are added to the registry. Simply subclass and instantiate:
133 /// class CollectorPrinter : public Registry<Collector>::listener {
135 /// void registered(const Registry<Collector>::entry &e) {
136 /// cerr << "collector now available: " << e->getName() << "\n";
140 /// CollectorPrinter() { init(); } // Print those already registered.
143 /// CollectorPrinter Printer;
146 listener *Prev, *Next;
148 friend void Registry::Announce(const entry &E);
151 /// Called when an entry is added to the registry.
153 virtual void registered(const entry &) = 0;
155 /// Calls 'registered' for each pre-existing entry.
158 for (iterator I = begin(), E = end(); I != E; ++I)
163 listener() : Prev(ListenerTail), Next(0) {
171 virtual ~listener() {
184 /// A static registration template. Use like such:
186 /// Registry<Collector>::Add<FancyGC>
187 /// X("fancy-gc", "Newfangled garbage collector.");
189 /// Use of this template requires that:
191 /// 1. The registered subclass has a default constructor.
193 /// 2. The registry entry type has a constructor compatible with this
196 /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
198 /// If you have more elaborate requirements, then copy and modify.
200 template <typename V>
205 static std::unique_ptr<T> CtorFn() { return make_unique<V>(); }
208 Add(const char *Name, const char *Desc)
209 : Entry(Name, Desc, CtorFn), Node(Entry) {}
212 /// Registry::Parser now lives in llvm/Support/RegistryParser.h.
216 // Since these are defined in a header file, plugins must be sure to export
219 template <typename T, typename U>
220 typename Registry<T,U>::node *Registry<T,U>::Head;
222 template <typename T, typename U>
223 typename Registry<T,U>::node *Registry<T,U>::Tail;
225 template <typename T, typename U>
226 typename Registry<T,U>::listener *Registry<T,U>::ListenerHead;
228 template <typename T, typename U>
229 typename Registry<T,U>::listener *Registry<T,U>::ListenerTail;