Vcsn  2.3a
Be Rational
context.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <vcsn/ctx/fwd.hh> // vcsn::context
7 #include <vcsn/dyn/cast.hh>
8 #include <vcsn/misc/export.hh>
9 #include <vcsn/misc/symbol.hh>
10 
11 namespace vcsn
12 {
13  namespace dyn
14  {
17  {
18  public:
20  : self_(nullptr)
21  {}
22 
24  template <typename LabelSet, typename WeightSet>
26  : self_(std::make_shared<model<vcsn::context<LabelSet, WeightSet>>>(ctx))
27  {}
28 
29  symbol vname() const
30  {
31  return self_->vname();
32  }
33 
35  template <typename Ctx>
36  auto& as()
37  {
38  return detail::dyn_cast<model<Ctx>&>(*self_).context();
39  }
40 
42  template <typename Ctx>
43  auto& as() const
44  {
45  return detail::dyn_cast<const model<Ctx>&>(*self_).context();
46  }
47 
48  const auto* operator->() const
49  {
50  return this;
51  }
52 
53  operator bool() const
54  {
55  return bool(self_);
56  }
57 
58  private:
60  struct base
61  {
62  virtual ~base() = default;
63  virtual symbol vname() const = 0;
64  };
65 
67  template <typename Context>
68  struct model final : base
69  {
70  using context_t = Context;
71 
73  : context_(std::move(ctx))
74  {}
75 
76  symbol vname() const
77  {
78  return context().sname();
79  }
80 
81  const auto& context() const
82  {
83  return context_;
84  }
85 
88  };
89 
91  std::shared_ptr<base> self_;
92  };
93  }
94 }
context(const vcsn::context< LabelSet, WeightSet > &ctx)
A description of the context, sufficient to build it.
Definition: context.hh:25
const auto * operator->() const
Definition: context.hh:48
Abstract wrapped type.
Definition: context.hh:60
symbol vname() const
Definition: context.hh:76
Definition: a-star.hh:8
const auto & context() const
Definition: context.hh:81
const context_t context_
The context.
Definition: context.hh:87
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
model(context_t ctx)
Definition: context.hh:72
auto & as()
Downcast to the exact type.
Definition: context.hh:36
auto & as() const
Downcast to the exact type.
Definition: context.hh:43
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:14
Template-less root for contexts.
Definition: context.hh:16
#define LIBVCSN_API
Definition: export.hh:8
symbol vname(T &t)
Definition: name.hh:99
STL namespace.
A wrapped type.
Definition: context.hh:68
std::shared_ptr< base > self_
The wrapped context.
Definition: context.hh:91
symbol vname() const
Definition: context.hh:29