1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//=== RegistryParser.h - Linker-supported plugin registries -----*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines a command-line parser for a registry.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_SUPPORT_REGISTRY_PARSER_H
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_SUPPORT_REGISTRY_PARSER_H
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Registry.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// A command-line parser for a registry. Use like such:
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///   static cl::opt<Registry<Collector>::entry, false,
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///                  RegistryParser<Collector> >
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///   GCOpt("gc", cl::desc("Garbage collector to use."),
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///               cl::value_desc());
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// To make use of the value:
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///   Collector *TheCollector = GCOpt->instantiate();
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template <typename T, typename U = RegistryTraits<T> >
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class RegistryParser :
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  public cl::parser<const typename U::entry*>,
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    public Registry<T, U>::listener {
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    typedef U traits;
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    typedef typename U::entry entry;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    typedef typename Registry<T, U>::listener listener;
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  protected:
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void registered(const entry &E) {
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      addLiteralOption(traits::nameof(E), &E, traits::descof(E));
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  public:
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void initialize(cl::Option &O) {
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      listener::init();
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cl::parser<const typename U::entry*>::initialize(O);
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // LLVM_SUPPORT_REGISTRY_PARSER_H
56