Vcsn  2.3a
Be Rational
automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm> // move
4 #include <memory> // shared_ptr
5 
6 #include <vcsn/concepts/automaton.hh> // Automaton
7 #include <vcsn/dyn/cast.hh>
8 #include <vcsn/dyn/fwd.hh>
9 #include <vcsn/misc/export.hh>
10 #include <vcsn/misc/symbol.hh>
11 
12 namespace vcsn
13 {
14  namespace dyn
15  {
18  {
19  public:
20  automaton(std::nullptr_t ptr)
21  : self_(ptr)
22  {}
23 
24  template <typename AutImpl>
25  automaton(const std::shared_ptr<AutImpl>& aut)
26  : self_(std::make_shared<model<std::shared_ptr<AutImpl>>>(aut))
27  {}
28 
30  symbol vname() const
31  {
32  return self_->vname();
33  }
34 
36  template <Automaton Aut>
37  auto& as()
38  {
39  return detail::dyn_cast<model<Aut>&>(*self_).automaton();
40  }
41 
43  template <Automaton Aut>
44  const auto& as() const
45  {
46  return detail::dyn_cast<const model<Aut>&>(*self_).automaton();
47  }
48 
49  auto* operator->()
50  {
51  return this;
52  }
53 
54  const auto* operator->() const
55  {
56  return this;
57  }
58 
59  bool operator!() const
60  {
61  return !self_;
62  }
63 
64  private:
66  struct base
67  {
68  virtual ~base() = default;
69  virtual symbol vname() const = 0;
70  };
71 
73  template <Automaton Aut>
74  struct model final : base
75  {
76  using automaton_t = Aut;
77 
79  : automaton_(std::move(aut))
80  {}
81 
82  symbol vname() const
83  {
84  return automaton()->sname();
85  }
86 
87  auto& automaton()
88  {
89  return automaton_;
90  }
91 
92  const auto& automaton() const
93  {
94  return automaton_;
95  }
96 
99  };
100 
102  std::shared_ptr<base> self_;
103  };
104  }
105 }
automaton(const std::shared_ptr< AutImpl > &aut)
Definition: automaton.hh:25
automaton_t automaton_
The automaton.
Definition: automaton.hh:98
Definition: a-star.hh:8
A wrapped typed automaton.
Definition: automaton.hh:74
const auto * operator->() const
Definition: automaton.hh:54
model(automaton_t aut)
Definition: automaton.hh:78
automaton(std::nullptr_t ptr)
Definition: automaton.hh:20
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
symbol vname() const
Definition: automaton.hh:82
Abstract wrapped typed automaton.
Definition: automaton.hh:66
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:14
bool operator!() const
Definition: automaton.hh:59
A dyn automaton.
Definition: automaton.hh:17
#define LIBVCSN_API
Definition: export.hh:8
symbol vname(T &t)
Definition: name.hh:99
symbol vname() const
A description of the automaton, sufficient to build it.
Definition: automaton.hh:30
const auto & as() const
Extract wrapped typed automaton.
Definition: automaton.hh:44
const auto & automaton() const
Definition: automaton.hh:92
STL namespace.
std::shared_ptr< base > self_
The wrapped automaton.
Definition: automaton.hh:102
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37