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/Support/CommandLine.h"
20 /// A simple registry entry which provides only a name, description, and
21 /// no-argument constructor.
23 class SimpleRegistryEntry {
24 const char *Name, *Desc;
28 SimpleRegistryEntry(const char *N, const char *D, T *(*C)())
29 : Name(N), Desc(D), Ctor(C)
32 const char *getName() const { return Name; }
33 const char *getDesc() const { return Desc; }
34 T *instantiate() const { return Ctor(); }
38 /// Traits for registry entries. If using other than SimpleRegistryEntry, it
39 /// is necessary to define an alternate traits class.
41 class RegistryTraits {
42 RegistryTraits(); // Do not implement.
45 typedef SimpleRegistryEntry<T> entry;
49 static const char *nameof(const entry &Entry) { return Entry.getName(); }
50 static const char *descof(const entry &Entry) { return Entry.getDesc(); }
54 /// A global registry used in conjunction with static constructors to make
55 /// pluggable components (like targets or garbage collectors) "just work" when
56 /// linked with an executable.
57 template <typename T, typename U = RegistryTraits<T> >
61 typedef typename U::entry entry;
68 Registry(); // Do not implement.
70 static void Announce(const entry &E) {
71 for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
76 static node *Head, *Tail;
78 friend class listener;
79 static listener *ListenerHead, *ListenerTail;
85 /// Node in linked list of entries.
88 friend class iterator;
94 node(const entry& V) : Next(0), 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(0); }
125 /// Abstract base class for registry listeners, which are informed when new
126 /// entries are added to the registry. Simply subclass and instantiate:
128 /// class CollectorPrinter : public Registry<Collector>::listener {
130 /// void registered(const Registry<Collector>::entry &e) {
131 /// cerr << "collector now available: " << e->getName() << "\n";
135 /// CollectorPrinter() { init(); } // Print those already registered.
138 /// CollectorPrinter Printer;
141 listener *Prev, *Next;
143 friend void Registry::Announce(const entry &E);
146 /// Called when an entry is added to the registry.
148 virtual void registered(const entry &) = 0;
150 /// Calls 'registered' for each pre-existing entry.
153 for (iterator I = begin(), E = end(); I != E; ++I)
158 listener() : Prev(ListenerTail), Next(0) {
166 virtual ~listener() {
179 /// A static registration template. Use like such:
181 /// Registry<Collector>::Add<FancyGC>
182 /// X("fancy-gc", "Newfangled garbage collector.");
184 /// Use of this template requires that:
186 /// 1. The registered subclass has a default constructor.
188 /// 2. The registry entry type has a constructor compatible with this
191 /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
193 /// If you have more elaborate requirements, then copy and modify.
195 template <typename V>
200 static T *CtorFn() { return new V(); }
203 Add(const char *Name, const char *Desc)
204 : Entry(Name, Desc, CtorFn), Node(Entry) {}
208 /// A command-line parser for a registry. Use like such:
210 /// static cl::opt<Registry<Collector>::entry, false,
211 /// Registry<Collector>::Parser>
212 /// GCOpt("gc", cl::desc("Garbage collector to use."),
213 /// cl::value_desc());
215 /// To make use of the value:
217 /// Collector *TheCollector = GCOpt->instantiate();
219 class Parser : public cl::parser<const typename U::entry*>, public listener{
221 typedef typename U::entry entry;
224 void registered(const entry &E) {
225 addLiteralOption(traits::nameof(E), &E, traits::descof(E));
229 void initialize(cl::Option &O) {
231 cl::parser<const typename U::entry*>::initialize(O);