Vcsn  2.4
Be Rational
registry.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <iostream>
5 
6 #include <boost/range/algorithm/sort.hpp>
7 
8 #include <lib/vcsn/dyn/translate.hh> // compile
9 #include <vcsn/dyn/name.hh>
10 #include <vcsn/misc/raise.hh>
11 #include <vcsn/misc/signature.hh>
13 
14 namespace vcsn
15 {
16  namespace dyn
17  {
18  namespace detail
19  {
20  template <typename Fun>
21  class Registry
22  {
23  public:
26  Registry(const std::string& name)
27  : name_(name)
28  {}
29 
30  Registry() = delete;
31 
33  bool debug = getenv("VCSN_DYN");
34 
36  bool set(const signature& sig, Fun* fn)
37  {
38  if (debug)
39  std::cerr << "Register(" << name_ << ").set(" << sig << ")\n";
40  map_[sig] = fn;
41  return true;
42  }
43 
45  const Fun* get0(const signature& sig)
46  {
47  if (debug)
48  std::cerr << "Register(" << name_ << ").get(" << sig << ")\n";
49  auto i = map_.find(sig);
50  if (i == map_.end())
51  return nullptr;
52  else
53  return i->second;
54  }
55 
57  std::string signatures(const signature& sig) const
58  {
59  auto sigs = std::vector<std::string>();
60  sigs.reserve(map_.size());
61  for (auto p: map_)
62  sigs.emplace_back(p.first.to_string());
63  boost::sort(sigs);
64 
65  std::string res;
66  res += " failed signature:\n";
67  res += " " + sig.to_string() + "\n";
68  res += " available versions:\n";
69  for (auto s: sigs)
70  res += " " + s + "\n";
71  return res;
72  }
73 
75  const Fun* get(const signature& sig)
76  {
77  // Maybe already loaded.
78  if (auto res = get0(sig))
79  return res;
80  else
81  // No, try to compile it.
82  {
83  try
84  {
86  }
87  catch (const jit_error& e)
88  {
89  raise(e.assertions.empty()
90  ? name_ + ": no such implementation\n"
91  : e.assertions,
92  signatures(sig),
93  e.what());
94  }
95  res = get0(sig);
97  name_,
98  ": compilation succeeded, "
99  "but function is unavailable\n",
100  signatures(sig));
101  return res;
102  }
103  }
104 
108  template <typename... Args>
109  auto
110  call(const signature& sig, Args&&... args)
111  -> decltype(std::declval<Fun>()(args...))
112  {
113  return (get(sig))(std::forward<Args>(args)...);
114  }
115 
116  template <typename... Args>
117  auto
118  call(Args&&... args)
119  -> decltype(std::declval<Fun>()(args...))
120  {
121  return call(vsignature(std::forward<Args>(args)...),
122  std::forward<Args>(args)...);
123  }
124 
125  private:
127  std::string name_;
129  using map_t = std::unordered_map<signature, Fun*>;
131  };
132  }
133  }
134 }
135 
137 #define REGISTRY_DEFINE(Name) \
138  namespace detail \
139  { \
140  static \
141  Registry<Name ## _t>& \
142  Name ## _registry() \
143  { \
144  static Registry<Name ## _t> instance{#Name}; \
145  return instance; \
146  } \
147  \
148  bool \
149  Name ## _register(const signature& sig, Name ## _t fn) \
150  { \
151  return Name ## _registry().set(sig, fn); \
152  } \
153  }
auto call(const signature &sig, Args &&...args) -> decltype(std::declval< Fun >()(args...))
Call function for signature sig.
Definition: registry.hh:110
signature vsignature(Args &&...args)
The signature of (Args...).
Definition: name.hh:310
return res
Definition: multiply.hh:398
Registry(const std::string &name)
Create a register for an algorithm.
Definition: registry.hh:26
std::unordered_map< signature, Fun * > map_t
Signature -> pointer to implementation.
Definition: registry.hh:129
std::string name_
Function name (e.g., "determinize").
Definition: registry.hh:127
void compile(const std::string &ctx)
Compile, and load, a DSO with instantiations for ctx.
Definition: translate.cc:380
An exception suited for our compilation errors.
Definition: translate.hh:13
std::string to_string() const
Definition: signature.cc:9
Definition: a-star.hh:8
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:111
bool debug
Whether log messages should be issued.
Definition: registry.hh:33
bool set(const signature &sig, Fun *fn)
Register function fn for signature sig.
Definition: registry.hh:36
auto sort(const Aut &a) -> permutation_automaton< Aut >
Definition: sort.hh:161
std::string assertions
If defined, static assertions that failed (ends with a eol).
Definition: translate.hh:17
std::string signatures(const signature &sig) const
A message about a failed signature compilation.
Definition: registry.hh:57
const Fun * get0(const signature &sig)
Get function for signature sig.
Definition: registry.hh:45
auto call(Args &&...args) -> decltype(std::declval< Fun >()(args...))
Definition: registry.hh:118
Signature of a function call.
Definition: signature.hh:15