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