Vcsn  2.1
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 
220  template <>
221  struct vnamer<const std::vector<dyn::expression>>
222  {
223  using type = const std::vector<dyn::expression>;
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  // FIXME: Code duplication.
239  template <>
240  struct vnamer<const std::vector<dyn::automaton>>
241  {
242  using type = const std::vector<dyn::automaton>;
243  static symbol name(const type& t)
244  {
245  std::string names;
246  for (const auto& a: t)
247  {
248  if (!names.empty())
249  names += ", ";
250  names += vname(a);
251  }
252  return symbol{"std::tuple<" + names + '>'};
253  }
254  };
255 
257  template <typename... Args>
258  struct snamer<std::tuple<Args...>>
259  {
260  template <typename T1>
261  static std::string name()
262  {
263  return sname<T1>();
264  }
265 
266  template <typename T1, typename T2, typename... Ts>
267  static std::string name()
268  {
269  return sname<T1>() + ", " + name<T2, Ts...>();
270  }
271 
272  static symbol name()
273  {
274  return symbol{"std::tuple<" + name<Args...>() + '>'};
275  }
276  };
277 
278 
279  /*-------------.
280  | vsignature. |
281  `-------------*/
282 
284  template <typename... Args>
285  inline
286  signature
287  vsignature(Args&&... args)
288  {
289  return {vname(std::forward<Args>(args))...};
290  }
291 
292 } // namespace vcsn
STL namespace.
const std::vector< dyn::automaton > type
Definition: name.hh:242
#define DEFINE(...)
Definition: name.hh:115
static symbol name(T &t)
Definition: name.hh:94
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:17
static symbol name()
Definition: name.hh:60
static std::string name()
Definition: name.hh:261
symbol vname(T &t)
Definition: name.hh:101
A simple placeholder for integral constants.
Definition: name.hh:198
direction
Orientation.
Definition: direction.hh:9
symbol sname(T &)
Definition: name.hh:73
ValueSet::value_t tuple(const ValueSet &vs, const typename ValueSets::value_t &...v)
Definition: tuple.hh:28
std::integral_constant< T, Value > type
Definition: name.hh:174
signature ssignature()
Static signature.
Definition: name.hh:82
symbol sname()
Definition: name.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:21
Signature of a function call.
Definition: signature.hh:15
static std::string name()
Definition: name.hh:267
signature vsignature(Args &&...args)
The signature of (Args...).
Definition: name.hh:287
const std::vector< dyn::expression > type
Definition: name.hh:223
static symbol name(integral_constant t)
Definition: name.hh:206