Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.0 KiB

  1. //=== Registry.h - Linker-supported plugin registries -----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Defines a registry template for discovering pluggable modules.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_REGISTRY_H
  14. #define LLVM_SUPPORT_REGISTRY_H
  15. namespace llvm {
  16. /// A simple registry entry which provides only a name, description, and
  17. /// no-argument constructor.
  18. template <typename T>
  19. class SimpleRegistryEntry {
  20. const char *Name, *Desc;
  21. T *(*Ctor)();
  22. public:
  23. SimpleRegistryEntry(const char *N, const char *D, T *(*C)())
  24. : Name(N), Desc(D), Ctor(C)
  25. {}
  26. const char *getName() const { return Name; }
  27. const char *getDesc() const { return Desc; }
  28. T *instantiate() const { return Ctor(); }
  29. };
  30. /// Traits for registry entries. If using other than SimpleRegistryEntry, it
  31. /// is necessary to define an alternate traits class.
  32. template <typename T>
  33. class RegistryTraits {
  34. RegistryTraits() LLVM_DELETED_FUNCTION;
  35. public:
  36. typedef SimpleRegistryEntry<T> entry;
  37. /// nameof/descof - Accessors for name and description of entries. These are
  38. // used to generate help for command-line options.
  39. static const char *nameof(const entry &Entry) { return Entry.getName(); }
  40. static const char *descof(const entry &Entry) { return Entry.getDesc(); }
  41. };
  42. /// A global registry used in conjunction with static constructors to make
  43. /// pluggable components (like targets or garbage collectors) "just work" when
  44. /// linked with an executable.
  45. template <typename T, typename U = RegistryTraits<T> >
  46. class Registry {
  47. public:
  48. typedef U traits;
  49. typedef typename U::entry entry;
  50. class node;
  51. class listener;
  52. class iterator;
  53. private:
  54. Registry() LLVM_DELETED_FUNCTION;
  55. static void Announce(const entry &E) {
  56. for (listener *Cur = ListenerHead; Cur; Cur = Cur->Next)
  57. Cur->registered(E);
  58. }
  59. friend class node;
  60. static node *Head, *Tail;
  61. friend class listener;
  62. static listener *ListenerHead, *ListenerTail;
  63. public:
  64. /// Node in linked list of entries.
  65. ///
  66. class node {
  67. friend class iterator;
  68. node *Next;
  69. const entry& Val;
  70. public:
  71. node(const entry& V) : Next(0), Val(V) {
  72. if (Tail)
  73. Tail->Next = this;
  74. else
  75. Head = this;
  76. Tail = this;
  77. Announce(V);
  78. }
  79. };
  80. /// Iterators for registry entries.
  81. ///
  82. class iterator {
  83. const node *Cur;
  84. public:
  85. explicit iterator(const node *N) : Cur(N) {}
  86. bool operator==(const iterator &That) const { return Cur == That.Cur; }
  87. bool operator!=(const iterator &That) const { return Cur != That.Cur; }
  88. iterator &operator++() { Cur = Cur->Next; return *this; }
  89. const entry &operator*() const { return Cur->Val; }
  90. const entry *operator->() const { return &Cur->Val; }
  91. };
  92. static iterator begin() { return iterator(Head); }
  93. static iterator end() { return iterator(0); }
  94. /// Abstract base class for registry listeners, which are informed when new
  95. /// entries are added to the registry. Simply subclass and instantiate:
  96. ///
  97. /// \code
  98. /// class CollectorPrinter : public Registry<Collector>::listener {
  99. /// protected:
  100. /// void registered(const Registry<Collector>::entry &e) {
  101. /// cerr << "collector now available: " << e->getName() << "\n";
  102. /// }
  103. ///
  104. /// public:
  105. /// CollectorPrinter() { init(); } // Print those already registered.
  106. /// };
  107. ///
  108. /// CollectorPrinter Printer;
  109. /// \endcode
  110. class listener {
  111. listener *Prev, *Next;
  112. friend void Registry::Announce(const entry &E);
  113. protected:
  114. /// Called when an entry is added to the registry.
  115. ///
  116. virtual void registered(const entry &) = 0;
  117. /// Calls 'registered' for each pre-existing entry.
  118. ///
  119. void init() {
  120. for (iterator I = begin(), E = end(); I != E; ++I)
  121. registered(*I);
  122. }
  123. public:
  124. listener() : Prev(ListenerTail), Next(0) {
  125. if (Prev)
  126. Prev->Next = this;
  127. else
  128. ListenerHead = this;
  129. ListenerTail = this;
  130. }
  131. virtual ~listener() {
  132. if (Next)
  133. Next->Prev = Prev;
  134. else
  135. ListenerTail = Prev;
  136. if (Prev)
  137. Prev->Next = Next;
  138. else
  139. ListenerHead = Next;
  140. }
  141. };
  142. /// A static registration template. Use like such:
  143. ///
  144. /// Registry<Collector>::Add<FancyGC>
  145. /// X("fancy-gc", "Newfangled garbage collector.");
  146. ///
  147. /// Use of this template requires that:
  148. ///
  149. /// 1. The registered subclass has a default constructor.
  150. //
  151. /// 2. The registry entry type has a constructor compatible with this
  152. /// signature:
  153. ///
  154. /// entry(const char *Name, const char *ShortDesc, T *(*Ctor)());
  155. ///
  156. /// If you have more elaborate requirements, then copy and modify.
  157. ///
  158. template <typename V>
  159. class Add {
  160. entry Entry;
  161. node Node;
  162. static T *CtorFn() { return new V(); }
  163. public:
  164. Add(const char *Name, const char *Desc)
  165. : Entry(Name, Desc, CtorFn), Node(Entry) {}
  166. };
  167. /// Registry::Parser now lives in llvm/Support/RegistryParser.h.
  168. };
  169. // Since these are defined in a header file, plugins must be sure to export
  170. // these symbols.
  171. template <typename T, typename U>
  172. typename Registry<T,U>::node *Registry<T,U>::Head;
  173. template <typename T, typename U>
  174. typename Registry<T,U>::node *Registry<T,U>::Tail;
  175. template <typename T, typename U>
  176. typename Registry<T,U>::listener *Registry<T,U>::ListenerHead;
  177. template <typename T, typename U>
  178. typename Registry<T,U>::listener *Registry<T,U>::ListenerTail;
  179. }
  180. #endif