Vcsn  2.2
Be Rational
name.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <initializer_list>
4 #include <iostream>
5 #include <memory>
6 #include <set>
7 #include <string>
8 #include <vector>
9 
10 #include <boost/optional.hpp>
11 
13 // FIXME: I don't like that misc depends on dyn. Actually, it is
14 // misc/name.hh which should be elsewhere.
15 #include <vcsn/dyn/automaton.hh>
16 #include <vcsn/dyn/expression.hh>
17 #include <vcsn/misc/direction.hh>
18 #include <vcsn/misc/signature.hh>
19 #include <vcsn/misc/symbol.hh>
20 
21 namespace vcsn
22 {
23  // This file provides the basic blocks needed by the implementation
24  // of dyn:: bridges.
25  //
26  // We need two types of signatures.
27  //
28  // The runtime signatures (vname and vsignature) are obtained during
29  // a call to a dyn:: routine: all its arguments are queried to get
30  // their runtime type name (e.g., `mutable_automaton<lal_char, b>`).
31  // These vnames are assembled into vsignatures, which are used to
32  // query the registry corresponding to the algorithm.
33  //
34  // The compile-time signatures (sname and ssignature) are built
35  // explicitly when registering an algorithm. This happens in
36  // vcsn/ctx/instantiate.hh, but also in the generated plugins. For
37  // instance:
38  //
39  // static bool conjunction_vector ATTRIBUTE_USED =
40  // vcsn::dyn::detail::conjunction_vector_register(
41  // vcsn::ssignature<t0_t, t1_t>(), // <============ Here.
42  // vcsn::dyn::detail::conjunction_vector<t0_t, t1_t>
43  // );
44  //
45  // So:
46  //
47  // - sname (built from types) is used at register time, it should
48  // be the precise type at hand,
49  //
50  // - vname (built from values) is used at dyn:: call time. It should
51  // return what sname will return.
52 
53  /*---------------.
54  | Static names. |
55  `---------------*/
56 
57  template <typename T>
58  struct snamer
59  {
60  static symbol name()
61  {
62  return T::sname();
63  }
64  };
65 
66  template <typename T>
68  {
69  return snamer<T>::name();
70  }
71 
72  template <typename T>
74  {
75  return sname<T>();
76  }
77 
79  template <typename... Args>
80  inline
81  signature
83  {
84  return {sname<Args>()...};
85  }
86 
87  /*----------------.
88  | Dynamic names. |
89  `----------------*/
90 
91  template <typename T>
92  struct vnamer
93  {
94  static symbol name(T& t)
95  {
96  return t->vname();
97  }
98  };
99 
100  template <typename T>
101  symbol vname(T& t)
102  {
103  return vnamer<T>::name(t);
104  }
105 
106  /*------------------.
107  | Specializations. |
108  `------------------*/
109 
110 
111  template <typename T>
112  struct snamer<std::shared_ptr<T>>: snamer<T>
113  {};
114 
115 #define DEFINE(...) \
116  template <> \
117  struct snamer<__VA_ARGS__> \
118  { \
119  static symbol name() \
120  { \
121  auto res = symbol{#__VA_ARGS__}; \
122  return res; \
123  } \
124  }; \
125  \
126  template <> \
127  struct vnamer<__VA_ARGS__> \
128  { \
129  static symbol name(__VA_ARGS__&) \
130  { \
131  auto res = symbol{#__VA_ARGS__}; \
132  return res; \
133  } \
134  };
135 
136 
137  DEFINE(bool);
138  DEFINE(float);
139  DEFINE(int);
140  DEFINE(unsigned);
141 
142  DEFINE(std::istream);
143  DEFINE(const std::string);
144  DEFINE(const std::vector<unsigned>);
145  DEFINE(const std::set<std::pair<std::string, std::string>>);
146  DEFINE(std::ostream);
147 
148  DEFINE(boost::optional<unsigned>);
149 
152 #undef DEFINE
153 
154 
155  /*--------------------.
156  | integral_constant. |
157  `--------------------*/
158 
159 
160  template <typename T, T Value>
161  struct snamer<std::integral_constant<T, Value>>
162  {
163  static symbol name()
164  {
165  symbol res("std::integral_constant<unsigned, "
166  + std::to_string(Value) + '>');
167  return res;
168  }
169  };
170 
171  template <typename T, T Value>
172  struct vnamer<std::integral_constant<T, Value>>
173  {
174  using type = std::integral_constant<T, Value>;
175  static symbol name(type)
176  {
177  return sname<type>();
178  }
179  };
180 
199  {
201  };
202 
203  template <>
205  {
207  {
208  return t.name;
209  }
210  };
211 
212 
213  /*--------------.
214  | std::tuple. |
215  `--------------*/
216 
222  template <typename Dyn>
224  {
225  using type = const std::vector<Dyn>;
226  static symbol name(const type& t)
227  {
228  std::string names;
229  for (const auto& a: t)
230  {
231  if (!names.empty())
232  names += ", ";
233  names += vname(a);
234  }
235  return symbol{"std::tuple<" + names + '>'};
236  }
237  };
238 
240  template <>
241  struct vnamer<const std::vector<dyn::automaton>>
242  : dyn_vector_vnamer<dyn::automaton>
243  {};
244 
246  template <>
247  struct vnamer<const std::vector<dyn::expansion>>
248  : dyn_vector_vnamer<dyn::expansion>
249  {};
250 
252  template <>
253  struct vnamer<const std::vector<dyn::expression>>
254  : dyn_vector_vnamer<dyn::expression>
255  {};
256 
258  template <typename... Args>
259  struct snamer<std::tuple<Args...>>
260  {
261  template <typename T1>
262  static std::string name()
263  {
264  return sname<T1>();
265  }
266 
267  template <typename T1, typename T2, typename... Ts>
268  static std::string name()
269  {
270  return sname<T1>() + ", " + name<T2, Ts...>();
271  }
272 
273  static symbol name()
274  {
275  return symbol{"std::tuple<" + name<Args...>() + '>'};
276  }
277  };
278 
279 
280  /*-------------.
281  | vsignature. |
282  `-------------*/
283 
285  template <typename... Args>
286  inline
287  signature
288  vsignature(Args&&... args)
289  {
290  return {vname(std::forward<Args>(args))...};
291  }
292 
293 } // namespace vcsn
ValueSet::value_t tuple(const ValueSet &vs, const typename ValueSets::value_t &...v)
Definition: tuple.hh:43
direction
Orientation.
Definition: direction.hh:9
static std::string name()
Definition: name.hh:268
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:21
#define DEFINE(...)
Definition: name.hh:115
Request the set implementation (bool weights).
A simple placeholder for integral constants.
Definition: name.hh:198
Definition: a-star.hh:8
STL namespace.
const std::vector< dyn::automaton > type
Definition: name.hh:225
The vname of a vector of dyn:: objects (e.g., automaton, expression, ...) is the tuple of their vname...
Definition: name.hh:223
static symbol name(T &t)
Definition: name.hh:94
symbol vname(T &t)
Definition: name.hh:101
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
signature vsignature(Args &&...args)
The signature of (Args...).
Definition: name.hh:288
static std::string name()
Definition: name.hh:262
symbol sname(T &)
Definition: name.hh:73
std::integral_constant< T, Value > type
Definition: name.hh:174
symbol sname()
Definition: name.hh:67
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:17
static symbol name(integral_constant t)
Definition: name.hh:206
static symbol name()
Definition: name.hh:60
static symbol name(const type &t)
Definition: name.hh:226
signature ssignature()
Static signature.
Definition: name.hh:82
Signature of a function call.
Definition: signature.hh:15