2007-09-27 19:34:27 +00:00
|
|
|
//=== Registry.h - Linker-supported plugin registries -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-27 19:34:27 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines a registry template for discovering pluggable modules.
|
2009-01-16 06:53:46 +00:00
|
|
|
//
|
2007-09-27 19:34:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_REGISTRY_H
|
|
|
|
#define LLVM_SUPPORT_REGISTRY_H
|
|
|
|
|
2013-06-27 17:57:40 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
namespace llvm {
|
|
|
|
/// A simple registry entry which provides only a name, description, and
|
|
|
|
/// no-argument constructor.
|
|
|
|
template <typename T>
|
|
|
|
class SimpleRegistryEntry {
|
|
|
|
const char *Name, *Desc;
|
|
|
|
T *(*Ctor)();
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
SimpleRegistryEntry(const char *N, const char *D, T *(*C)())
|
|
|
|
: Name(N), Desc(D), Ctor(C)
|
|
|
|
{}
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
const char *getName() const { return Name; }
|
|
|
|
const char *getDesc() const { return Desc; }
|
|
|
|
T *instantiate() const { return Ctor(); }
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Traits for registry entries. If using other than SimpleRegistryEntry, it
|
|
|
|
/// is necessary to define an alternate traits class.
|
|
|
|
template <typename T>
|
|
|
|
class RegistryTraits {
|
2012-09-17 06:31:17 +00:00
|
|
|
RegistryTraits() LLVM_DELETED_FUNCTION;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
typedef SimpleRegistryEntry<T> entry;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2008-08-17 19:08:34 +00:00
|
|
|
/// nameof/descof - Accessors for name and description of entries. These are
|
|
|
|
// used to generate help for command-line options.
|
2007-09-27 19:34:27 +00:00
|
|
|
static const char *nameof(const entry &Entry) { return Entry.getName(); }
|
|
|
|
static const char *descof(const entry &Entry) { return Entry.getDesc(); }
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// 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.
|
|
|
|
template <typename T, typename U = RegistryTraits<T> >
|
|
|
|
class Registry {
|
|
|
|
public:
|
|
|
|
typedef U traits;
|
|
|
|
typedef typename U::entry entry;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
class node;
|
|
|
|
class listener;
|
|
|
|
class iterator;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
private:
|
2012-09-17 06:31:17 +00:00
|
|
|
Registry() LLVM_DELETED_FUNCTION;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-10-17 14:56:40 +00:00
|
|
|
static void Announce(const entry &E) {
|
|
|
|
for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
|
|
|
|
Cur->registered(E);
|
|
|
|
}
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
friend class node;
|
|
|
|
static node *Head, *Tail;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
friend class listener;
|
|
|
|
static listener *ListenerHead, *ListenerTail;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
/// Node in linked list of entries.
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
class node {
|
|
|
|
friend class iterator;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
node *Next;
|
|
|
|
const entry& Val;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
node(const entry& V) : Next(0), Val(V) {
|
|
|
|
if (Tail)
|
|
|
|
Tail->Next = this;
|
|
|
|
else
|
|
|
|
Head = this;
|
|
|
|
Tail = this;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
Announce(V);
|
|
|
|
}
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Iterators for registry entries.
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
class iterator {
|
|
|
|
const node *Cur;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
explicit iterator(const node *N) : Cur(N) {}
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
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; }
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
static iterator begin() { return iterator(Head); }
|
|
|
|
static iterator end() { return iterator(0); }
|
2009-01-16 06:53:46 +00:00
|
|
|
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Abstract base class for registry listeners, which are informed when new
|
|
|
|
/// entries are added to the registry. Simply subclass and instantiate:
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2012-09-15 20:22:05 +00:00
|
|
|
/// \code
|
2007-09-27 19:34:27 +00:00
|
|
|
/// class CollectorPrinter : public Registry<Collector>::listener {
|
|
|
|
/// protected:
|
|
|
|
/// void registered(const Registry<Collector>::entry &e) {
|
|
|
|
/// cerr << "collector now available: " << e->getName() << "\n";
|
|
|
|
/// }
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// public:
|
|
|
|
/// CollectorPrinter() { init(); } // Print those already registered.
|
|
|
|
/// };
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// CollectorPrinter Printer;
|
2012-09-15 20:22:05 +00:00
|
|
|
/// \endcode
|
2007-09-27 19:34:27 +00:00
|
|
|
class listener {
|
|
|
|
listener *Prev, *Next;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
friend void Registry::Announce(const entry &E);
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
protected:
|
|
|
|
/// Called when an entry is added to the registry.
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
virtual void registered(const entry &) = 0;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Calls 'registered' for each pre-existing entry.
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
void init() {
|
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
registered(*I);
|
|
|
|
}
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
listener() : Prev(ListenerTail), Next(0) {
|
|
|
|
if (Prev)
|
|
|
|
Prev->Next = this;
|
|
|
|
else
|
|
|
|
ListenerHead = this;
|
|
|
|
ListenerTail = this;
|
|
|
|
}
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
virtual ~listener() {
|
|
|
|
if (Next)
|
|
|
|
Next->Prev = Prev;
|
|
|
|
else
|
|
|
|
ListenerTail = Prev;
|
|
|
|
if (Prev)
|
|
|
|
Prev->Next = Next;
|
|
|
|
else
|
|
|
|
ListenerHead = Next;
|
|
|
|
}
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
/// A static registration template. Use like such:
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Registry<Collector>::Add<FancyGC>
|
|
|
|
/// X("fancy-gc", "Newfangled garbage collector.");
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// Use of this template requires that:
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// 1. The registered subclass has a default constructor.
|
2009-01-16 06:53:46 +00:00
|
|
|
//
|
2007-09-27 19:34:27 +00:00
|
|
|
/// 2. The registry entry type has a constructor compatible with this
|
|
|
|
/// signature:
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
/// If you have more elaborate requirements, then copy and modify.
|
2009-01-16 06:53:46 +00:00
|
|
|
///
|
2007-09-27 19:34:27 +00:00
|
|
|
template <typename V>
|
|
|
|
class Add {
|
|
|
|
entry Entry;
|
|
|
|
node Node;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
static T *CtorFn() { return new V(); }
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
public:
|
|
|
|
Add(const char *Name, const char *Desc)
|
|
|
|
: Entry(Name, Desc, CtorFn), Node(Entry) {}
|
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2009-01-16 07:02:28 +00:00
|
|
|
/// Registry::Parser now lives in llvm/Support/RegistryParser.h.
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
};
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2010-07-26 21:05:37 +00:00
|
|
|
// Since these are defined in a header file, plugins must be sure to export
|
|
|
|
// these symbols.
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2008-08-17 19:08:34 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename Registry<T,U>::node *Registry<T,U>::Head;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2008-08-17 19:08:34 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename Registry<T,U>::node *Registry<T,U>::Tail;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2008-08-17 19:08:34 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename Registry<T,U>::listener *Registry<T,U>::ListenerHead;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2008-08-17 19:08:34 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
typename Registry<T,U>::listener *Registry<T,U>::ListenerTail;
|
2009-01-16 06:53:46 +00:00
|
|
|
|
2007-09-27 19:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|