1 //=== Registry.h - Linker-supported plugin registries -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under the
6 // University of Illinois Open Source 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(node *);
73 static node *Head, *Tail;
75 friend class listener;
76 static listener *ListenerHead, *ListenerTail;
82 /// Node in linked list of entries.
85 friend class iterator;
91 node(const entry& V) : Next(0), Val(V) {
103 /// Iterators for registry entries.
109 explicit iterator(const node *N) : Cur(N) {}
111 bool operator==(const iterator &That) const { return Cur == That.Cur; }
112 bool operator!=(const iterator &That) const { return Cur != That.Cur; }
113 iterator &operator++() { Cur = Cur->Next; return *this; }
114 const entry &operator*() const { return Cur->Val; }
115 const entry *operator->() const { return &Cur->Val; }
118 static iterator begin() { return iterator(Head); }
119 static iterator end() { return iterator(0); }
122 /// Abstract base class for registry listeners, which are informed when new
123 /// entries are added to the registry. Simply subclass and instantiate:
125 /// class CollectorPrinter : public Registry<Collector>::listener {
127 /// void registered(const Registry<Collector>::entry &e) {
128 /// cerr << "collector now available: " << e->getName() << "\n";
132 /// CollectorPrinter() { init(); } // Print those already registered.
135 /// CollectorPrinter Printer;
138 listener *Prev, *Next;
140 friend void Registry::Announce(const entry &E);
143 /// Called when an entry is added to the registry.
145 virtual void registered(const entry &) = 0;
147 /// Calls 'registered' for each pre-existing entry.
150 for (iterator I = begin(), E = end(); I != E; ++I)
155 listener() : Prev(ListenerTail), Next(0) {
163 virtual ~listener() {
176 /// A static registration template. Use like such:
178 /// Registry<Collector>::Add<FancyGC>
179 /// X("fancy-gc", "Newfangled garbage collector.");
181 /// Use of this template requires that:
183 /// 1. The registered subclass has a default constructor.
185 /// 2. The registry entry type has a constructor compatible with this
188 /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
190 /// If you have more elaborate requirements, then copy and modify.
192 template <typename V>
197 static T *CtorFn() { return new V(); }
200 Add(const char *Name, const char *Desc)
201 : Entry(Name, Desc, CtorFn), Node(Entry) {}
205 /// A command-line parser for a registry. Use like such:
207 /// static cl::opt<Registry<Collector>::entry, false,
208 /// Registry<Collector>::Parser>
209 /// GCOpt("gc", cl::desc("Garbage collector to use."),
210 /// cl::value_desc());
212 /// To make use of the value:
214 /// Collector *TheCollector = GCOpt->instantiate();
216 class Parser : public cl::parser<const typename U::entry*>, public listener{
218 typedef typename U::entry entry;
221 void registered(const entry &E) {
222 addLiteralOption(traits::nameof(E), &E, traits::descof(E));
226 void initialize(cl::Option &O) {
228 cl::parser<const typename U::entry*>::initialize(O);
234 static void Announce(const entry &E) {
235 for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)