Vcsn  2.8
Be Rational
genset-labelset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <set>
5 
6 #include <boost/optional.hpp>
7 #include <boost/range/algorithm/for_each.hpp>
8 
9 #include <vcsn/core/kind.hh>
10 #include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
14  namespace detail
15  {
18  template <typename GenSet>
20  {
21  using genset_t = GenSet;
22  using genset_ptr = std::shared_ptr<const genset_t>;
23 
24  using letter_t = typename genset_t::letter_t;
25  using word_t = typename genset_t::word_t;
26  using letters_t = typename genset_t::letters_t;
27 
29  : gs_{gs}
30  {}
31 
32  genset_labelset(const genset_t& gs = {})
33  : genset_labelset{std::make_shared<const genset_t>(gs)}
34  {}
35 
37  {
38  return gs_;
39  }
40 
41  static symbol sname()
42  {
43  static auto res = symbol{genset_t::sname()};
44  return res;
45  }
46 
58  template <typename Fun>
59  void
60  convs_(std::istream& i, Fun fun) const
61  {
62  eat(i, '[');
63  // Working via a set looks weird, instead of just calling
64  // `conv_label_class_(*this, i, fun)`, but it allows to ensure
65  // that letter classes never count letters several times.
66  // Otherwise [aa] would be <2>a, instead of a.
67  boost::for_each(conv_label_class_(*this, i), fun);
68  eat(i, ']');
69  }
70 
72  letter_t
73  get_letter(std::istream& i, bool quoted = true) const
74  {
75  letter_t res = this->genset()->get_letter(i, quoted);
76  VCSN_REQUIRE(this->has(res),
77  "invalid label: unexpected ", str_escape(res));
78  return res;
79  }
80 
82 # define DEFINE(Name, Attribute) \
83  template <typename... Args> \
84  Attribute \
85  auto \
86  Name(Args&&... args) const \
87  -> decltype(this->genset()->Name(std::forward<Args>(args)...)) \
88  { \
89  return this->genset()->Name(std::forward<Args>(args)...); \
90  }
91 
93  DEFINE(compare, ATTRIBUTE_PURE);
96  DEFINE(equal, ATTRIBUTE_PURE);
99  DEFINE(has, ATTRIBUTE_PURE);
100  DEFINE(is_letter, ATTRIBUTE_PURE);
101  DEFINE(less, ATTRIBUTE_PURE);
104  DEFINE(transpose, ATTRIBUTE_PURE);
106 
107 # undef DEFINE
108 
109  private:
110  genset_ptr gs_;
111  };
112  }
113 }
auto generators(Args &&... args) const -> decltype(this->genset() -> generators(std::forward< Args >(args)...))
auto end(Args &&... args) const -> decltype(this->genset() -> end(std::forward< Args >(args)...))
auto begin(Args &&... args) const -> decltype(this->genset() -> begin(std::forward< Args >(args)...))
auto mul(Args &&... args) const -> decltype(this->genset() -> mul(std::forward< Args >(args)...))
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:147
typename genset_t::word_t word_t
#define DEFINE(Name, Attribute)
Use the implementation from genset.
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:51
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
auto delimit(Args &&... args) const -> decltype(this->genset() -> delimit(std::forward< Args >(args)...))
genset_labelset(const genset_t &gs={})
LabelSet::letters_t conv_label_class_(const LabelSet &ls, std::istream &i)
Read a set of letters (hence, guaranteed in order, and unique).
Definition: labelset.hh:444
auto get_word(Args &&... args) const -> decltype(this->genset() -> get_word(std::forward< Args >(args)...))
This class has no modeling purpose, it only serves to factor code common to letterset and wordset...
ATTRIBUTE_PURE auto has(Args &&... args) const -> decltype(this->genset() -> has(std::forward< Args >(args)...))
Definition: a-star.hh:8
genset_labelset(const genset_ptr &gs)
typename genset_t::letter_t letter_t
ATTRIBUTE_PURE auto equal(Args &&... args) const -> decltype(this->genset() -> equal(std::forward< Args >(args)...))
letter_t get_letter(std::istream &i, bool quoted=true) const
Read one letter from i.
ATTRIBUTE_PURE auto transpose(Args &&... args) const -> decltype(this->genset() -> transpose(std::forward< Args >(args)...))
symbol sname()
Definition: name.hh:65
void convs_(std::istream &i, Fun fun) const
Read and process a class of letters.
Functor to compare Values of ValueSets.
Definition: functional.hh:91
auto undelimit(Args &&... args) const -> decltype(this->genset() -> undelimit(std::forward< Args >(args)...))
typename genset_t::letters_t letters_t
Functor to three-way comparison Values of ValueSets.
Definition: functional.hh:11
ATTRIBUTE_PURE auto is_letter(Args &&... args) const -> decltype(this->genset() -> is_letter(std::forward< Args >(args)...))
std::shared_ptr< const genset_t > genset_ptr
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:98
auto pregenerators(Args &&... args) const -> decltype(this->genset() -> pregenerators(std::forward< Args >(args)...))
return res
Definition: multiply.hh:399