Vcsn  2.2a
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/core/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  {
16  namespace detail
17  {
20  {
21  public:
22  automaton(std::nullptr_t ptr)
23  : self_(ptr)
24  {}
25 
26  template <typename AutImpl>
27  automaton(const std::shared_ptr<AutImpl>& aut)
28  : self_(std::make_shared<model<std::shared_ptr<AutImpl>>>(aut))
29  {}
30 
32  symbol vname() const
33  {
34  return self_->vname();
35  }
36 
38  template <Automaton Aut>
39  auto& as()
40  {
41  return dyn_cast<model<Aut>&>(*self_).automaton();
42  }
43 
45  template <Automaton Aut>
46  const auto& as() const
47  {
48  return dyn_cast<const model<Aut>&>(*self_).automaton();
49  }
50 
51  auto* operator->()
52  {
53  return this;
54  }
55 
56  const auto* operator->() const
57  {
58  return this;
59  }
60 
61  bool operator!() const
62  {
63  return !self_;
64  }
65 
66  private:
68  struct base
69  {
70  virtual ~base() = default;
71  virtual symbol vname() const = 0;
72  };
73 
75  template <Automaton Aut>
76  struct model final : base
77  {
78  using automaton_t = Aut;
79 
81  : automaton_(std::move(aut))
82  {}
83 
84  symbol vname() const
85  {
86  return automaton()->sname();
87  }
88 
89  auto& automaton()
90  {
91  return automaton_;
92  }
93 
94  const auto& automaton() const
95  {
96  return automaton_;
97  }
98 
101  };
102 
104  std::shared_ptr<base> self_;
105  };
106  }
107 
109 
111  template <Automaton Aut>
112  automaton
113  make_automaton(const Aut& aut)
114  {
115  return automaton(aut);
116  }
117  }
118 }
Abstract wrapped typed automaton.
Definition: automaton.hh:68
#define LIBVCSN_API
Definition: export.hh:8
symbol vname() const
A description of the automaton, sufficient to build it.
Definition: automaton.hh:32
automaton(const std::shared_ptr< AutImpl > &aut)
Definition: automaton.hh:27
A dyn automaton.
Definition: automaton.hh:19
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
A wrapped typed automaton.
Definition: automaton.hh:76
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:12
const auto & as() const
Extract wrapped typed automaton.
Definition: automaton.hh:46
detail::automaton automaton
Definition: automaton.hh:108
const auto * operator->() const
Definition: automaton.hh:56
automaton(std::nullptr_t ptr)
Definition: automaton.hh:22
STL namespace.
automaton_t automaton_
The automaton.
Definition: automaton.hh:100
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:113
symbol vname(T &t)
Definition: name.hh:101
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:39
const auto & automaton() const
Definition: automaton.hh:94
std::shared_ptr< base > self_
The wrapped automaton.
Definition: automaton.hh:104
Definition: a-star.hh:8