Vcsn  2.3
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  const genset_t& generators() const
42  {
43  return *gs_;
44  }
45 
46  static symbol sname()
47  {
48  static auto res = symbol{genset_t::sname()};
49  return res;
50  }
51 
63  template <typename Fun>
64  void
65  convs_(std::istream& i, Fun fun) const
66  {
67  eat(i, '[');
68  // Working via a set looks weird, instead of just calling
69  // `conv_label_class_(*this, i, fun)`, but it allows to ensure
70  // that letter classes never count letters several times.
71  // Otherwise [aa] would be <2>a, instead of a.
72  boost::for_each(conv_label_class_(*this, i), fun);
73  eat(i, ']');
74  }
75 
77  letter_t
78  get_letter(std::istream& i, bool quoted = true) const
79  {
80  letter_t res = this->genset()->get_letter(i, quoted);
81  VCSN_REQUIRE(this->has(res),
82  "invalid label: unexpected ", str_escape(res));
83  return res;
84  }
85 
87 # define DEFINE(Name, Attribute) \
88  template <typename... Args> \
89  Attribute \
90  auto \
91  Name(Args&&... args) const \
92  -> decltype(this->genset()->Name(std::forward<Args>(args)...)) \
93  { \
94  return this->genset()->Name(std::forward<Args>(args)...); \
95  }
96 
100  DEFINE(equal, ATTRIBUTE_PURE);
102  DEFINE(has, ATTRIBUTE_PURE);
103  DEFINE(is_letter, ATTRIBUTE_PURE);
104  DEFINE(less, ATTRIBUTE_PURE);
106  DEFINE(transpose, ATTRIBUTE_PURE);
108 
109 # undef DEFINE
110 
111  private:
112  genset_ptr gs_;
113  };
114  }
115 }
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
auto undelimit(Args &&...args) const -> decltype(this->genset() -> undelimit(std::forward< Args >(args)...))
auto mul(Args &&...args) const -> decltype(this->genset() -> mul(std::forward< Args >(args)...))
std::shared_ptr< const genset_t > genset_ptr
genset_labelset(const genset_ptr &gs)
symbol sname()
Definition: name.hh:65
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
genset_labelset(const genset_t &gs={})
letter_t get_letter(std::istream &i, bool quoted=true) const
Read one letter from i.
void convs_(std::istream &i, Fun fun) const
Read and process a class of letters.
return res
Definition: multiply.hh:398
ATTRIBUTE_PURE auto equal(Args &&...args) const -> decltype(this->genset() -> equal(std::forward< Args >(args)...))
This class has no modeling purpose, it only serves to factor code common to letterset and wordset...
typename genset_t::letters_t letters_t
auto delimit(Args &&...args) const -> decltype(this->genset() -> delimit(std::forward< Args >(args)...))
Definition: a-star.hh:8
const genset_t & generators() const
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:111
auto begin(Args &&...args) const -> decltype(this->genset() -> begin(std::forward< Args >(args)...))
ATTRIBUTE_PURE auto has(Args &&...args) const -> decltype(this->genset() -> has(std::forward< Args >(args)...))
Functor to compare Values of ValueSets.
Definition: functional.hh:76
typename genset_t::word_t word_t
auto get_word(Args &&...args) const -> decltype(this->genset() -> get_word(std::forward< Args >(args)...))
auto end(Args &&...args) const -> decltype(this->genset() -> end(std::forward< Args >(args)...))
typename genset_t::letter_t letter_t
ATTRIBUTE_PURE auto transpose(Args &&...args) const -> decltype(this->genset() -> transpose(std::forward< Args >(args)...))
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
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:440
ATTRIBUTE_PURE auto is_letter(Args &&...args) const -> decltype(this->genset() -> is_letter(std::forward< Args >(args)...))
#define DEFINE(Name, Attribute)
Use the implementation from genset.