X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FRegistry.h;h=d0375bedd9f25b658acf586612cf630a589b0013;hb=55907d1274ce715b92d584e305e0708e333a33c0;hp=7488773bb46353dc2d7effbc1952daea50db39c2;hpb=efd4a5144b03f61ebfd53d0245176f95e1170fb8;p=oota-llvm.git diff --git a/include/llvm/Support/Registry.h b/include/llvm/Support/Registry.h index 7488773bb46..d0375bedd9f 100644 --- a/include/llvm/Support/Registry.h +++ b/include/llvm/Support/Registry.h @@ -2,20 +2,18 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by Gordon Henriksen and is distributed under the -// University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // Defines a registry template for discovering pluggable modules. -// +// //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_REGISTRY_H #define LLVM_SUPPORT_REGISTRY_H -#include "llvm/Support/CommandLine.h" - namespace llvm { /// A simple registry entry which provides only a name, description, and /// no-argument constructor. @@ -23,34 +21,34 @@ namespace llvm { class SimpleRegistryEntry { const char *Name, *Desc; T *(*Ctor)(); - + public: SimpleRegistryEntry(const char *N, const char *D, T *(*C)()) : Name(N), Desc(D), Ctor(C) {} - + const char *getName() const { return Name; } const char *getDesc() const { return Desc; } T *instantiate() const { return Ctor(); } }; - - + + /// Traits for registry entries. If using other than SimpleRegistryEntry, it /// is necessary to define an alternate traits class. template class RegistryTraits { RegistryTraits(); // Do not implement. - + public: typedef SimpleRegistryEntry entry; - - /// Accessors for . - /// + + /// nameof/descof - Accessors for name and description of entries. These are + // used to generate help for command-line options. static const char *nameof(const entry &Entry) { return Entry.getName(); } static const char *descof(const entry &Entry) { return Entry.getDesc(); } }; - - + + /// A global registry used in conjunction with static constructors to make /// pluggable components (like targets or garbage collectors) "just work" when /// linked with an executable. @@ -59,37 +57,34 @@ namespace llvm { public: typedef U traits; typedef typename U::entry entry; - + class node; class listener; class iterator; - + private: Registry(); // Do not implement. - + static void Announce(const entry &E) { for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next) Cur->registered(E); } - + friend class node; static node *Head, *Tail; - + friend class listener; static listener *ListenerHead, *ListenerTail; - + public: - class iterator; - - /// Node in linked list of entries. - /// + /// class node { friend class iterator; - + node *Next; const entry& Val; - + public: node(const entry& V) : Next(0), Val(V) { if (Tail) @@ -97,63 +92,63 @@ namespace llvm { else Head = this; Tail = this; - + Announce(V); } }; - - + + /// Iterators for registry entries. - /// + /// class iterator { const node *Cur; - + public: explicit iterator(const node *N) : Cur(N) {} - + bool operator==(const iterator &That) const { return Cur == That.Cur; } bool operator!=(const iterator &That) const { return Cur != That.Cur; } iterator &operator++() { Cur = Cur->Next; return *this; } const entry &operator*() const { return Cur->Val; } const entry *operator->() const { return &Cur->Val; } }; - + static iterator begin() { return iterator(Head); } static iterator end() { return iterator(0); } - - + + /// Abstract base class for registry listeners, which are informed when new /// entries are added to the registry. Simply subclass and instantiate: - /// + /// /// class CollectorPrinter : public Registry::listener { /// protected: /// void registered(const Registry::entry &e) { /// cerr << "collector now available: " << e->getName() << "\n"; /// } - /// + /// /// public: /// CollectorPrinter() { init(); } // Print those already registered. /// }; - /// + /// /// CollectorPrinter Printer; - /// + /// class listener { listener *Prev, *Next; - + friend void Registry::Announce(const entry &E); - + protected: /// Called when an entry is added to the registry. - /// + /// virtual void registered(const entry &) = 0; - + /// Calls 'registered' for each pre-existing entry. - /// + /// void init() { for (iterator I = begin(), E = end(); I != E; ++I) registered(*I); } - + public: listener() : Prev(ListenerTail), Next(0) { if (Prev) @@ -162,7 +157,7 @@ namespace llvm { ListenerHead = this; ListenerTail = this; } - + virtual ~listener() { if (Next) Next->Prev = Prev; @@ -174,66 +169,55 @@ namespace llvm { ListenerHead = Next; } }; - - + + /// A static registration template. Use like such: - /// + /// /// Registry::Add /// X("fancy-gc", "Newfangled garbage collector."); - /// + /// /// Use of this template requires that: - /// + /// /// 1. The registered subclass has a default constructor. - // + // /// 2. The registry entry type has a constructor compatible with this /// signature: - /// + /// /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)()); - /// + /// /// If you have more elaborate requirements, then copy and modify. - /// + /// template class Add { entry Entry; node Node; - + static T *CtorFn() { return new V(); } - + public: Add(const char *Name, const char *Desc) : Entry(Name, Desc, CtorFn), Node(Entry) {} }; - - - /// A command-line parser for a registry. Use like such: - /// - /// static cl::opt::entry, false, - /// Registry::Parser> - /// GCOpt("gc", cl::desc("Garbage collector to use."), - /// cl::value_desc()); - /// - /// To make use of the value: - /// - /// Collector *TheCollector = GCOpt->instantiate(); - /// - class Parser : public cl::parser, public listener{ - typedef U traits; - typedef typename U::entry entry; - - protected: - void registered(const entry &E) { - addLiteralOption(traits::nameof(E), &E, traits::descof(E)); - } - - public: - void initialize(cl::Option &O) { - listener::init(); - cl::parser::initialize(O); - } - }; - + + /// Registry::Parser now lives in llvm/Support/RegistryParser.h. + }; - + + // Since these are defined in a header file, plugins must be sure to export + // these symbols. + + template + typename Registry::node *Registry::Head; + + template + typename Registry::node *Registry::Tail; + + template + typename Registry::listener *Registry::ListenerHead; + + template + typename Registry::listener *Registry::ListenerTail; + } #endif