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/Compiler.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() LLVM_DELETED_FUNCTION;
45 typedef SimpleRegistryEntry<T> entry;
47 /// nameof/descof - Accessors for name and description of entries. These are
48 // used to generate help for command-line options.
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() LLVM_DELETED_FUNCTION;
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;
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:
126 /// class CollectorPrinter : public Registry<Collector>::listener {
128 /// void registered(const Registry<Collector>::entry &e) {
129 /// cerr << "collector now available: " << e->getName() << "\n";
133 /// CollectorPrinter() { init(); } // Print those already registered.
136 /// CollectorPrinter Printer;
139 listener *Prev, *Next;
141 friend void Registry::Announce(const entry &E);
144 /// Called when an entry is added to the registry.
146 virtual void registered(const entry &) = 0;
148 /// Calls 'registered' for each pre-existing entry.
151 for (iterator I = begin(), E = end(); I != E; ++I)
156 listener() : Prev(ListenerTail), Next(0) {
164 virtual ~listener() {
177 /// A static registration template. Use like such:
179 /// Registry<Collector>::Add<FancyGC>
180 /// X("fancy-gc", "Newfangled garbage collector.");
182 /// Use of this template requires that:
184 /// 1. The registered subclass has a default constructor.
186 /// 2. The registry entry type has a constructor compatible with this
189 /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
191 /// If you have more elaborate requirements, then copy and modify.
193 template <typename V>
198 static T *CtorFn() { return new V(); }
201 Add(const char *Name, const char *Desc)
202 : Entry(Name, Desc, CtorFn), Node(Entry) {}
205 /// Registry::Parser now lives in llvm/Support/RegistryParser.h.
209 // Since these are defined in a header file, plugins must be sure to export
212 template <typename T, typename U>
213 typename Registry<T,U>::node *Registry<T,U>::Head;
215 template <typename T, typename U>
216 typename Registry<T,U>::node *Registry<T,U>::Tail;
218 template <typename T, typename U>
219 typename Registry<T,U>::listener *Registry<T,U>::ListenerHead;
221 template <typename T, typename U>
222 typename Registry<T,U>::listener *Registry<T,U>::ListenerTail;