FST  openfst-1.8.3
OpenFst Library
impl-to-fst.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Helper class template useful for attaching an FST interface to its
19 // implementation, handling reference counting.
20 
21 #ifndef FST_IMPL_TO_FST_H_
22 #define FST_IMPL_TO_FST_H_
23 
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 
28 #include <fst/fst.h>
29 #include <fst/impl-to-fst.h>
30 #include <fst/symbol-table.h>
31 #include <fst/test-properties.h>
32 
33 namespace fst {
34 
35 // This is a helper class template useful for attaching an FST interface to
36 // its implementation, handling reference counting.
37 // Thread-unsafe due to Properties (a const function) calling
38 // Impl::SetProperties. TODO(jrosenstock): Make thread-compatible.
39 // Impl's copy constructor must produce a thread-safe copy.
40 template <class Impl, class FST = Fst<typename Impl::Arc>>
41 class ImplToFst : public FST {
42  public:
43  using Arc = typename Impl::Arc;
44  using StateId = typename Arc::StateId;
45  using Weight = typename Arc::Weight;
46 
47  StateId Start() const override { return impl_->Start(); }
48 
49  Weight Final(StateId s) const override { return impl_->Final(s); }
50 
51  size_t NumArcs(StateId s) const override { return impl_->NumArcs(s); }
52 
53  size_t NumInputEpsilons(StateId s) const override {
54  return impl_->NumInputEpsilons(s);
55  }
56 
57  size_t NumOutputEpsilons(StateId s) const override {
58  return impl_->NumOutputEpsilons(s);
59  }
60 
61  // Note that this is a const function, but can set the Impl's properties
62  // when test is true.
63  uint64_t Properties(uint64_t mask, bool test) const override {
64  if (test) {
65  uint64_t knownprops,
66  testprops = internal::TestProperties(*this, mask, &knownprops);
67  // Properties is a const member function, but can set the cached
68  // properties. UpdateProperties does this thread-safely via atomics.
69  impl_->UpdateProperties(testprops, knownprops);
70  return testprops & mask;
71  } else {
72  return impl_->Properties(mask);
73  }
74  }
75 
76  const std::string &Type() const override { return impl_->Type(); }
77 
78  const SymbolTable *InputSymbols() const override {
79  return impl_->InputSymbols();
80  }
81 
82  const SymbolTable *OutputSymbols() const override {
83  return impl_->OutputSymbols();
84  }
85 
86  protected:
87  explicit ImplToFst(std::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
88 
89  // The object is thread-compatible if constructed with safe=true,
90  // otherwise thread-unsafe.
91  // This constructor presumes there is a copy constructor for the
92  // implementation that produces a thread-safe copy.
93  ImplToFst(const ImplToFst &fst, bool safe) {
94  if (safe) {
95  impl_ = std::make_shared<Impl>(*(fst.impl_));
96  } else {
97  impl_ = fst.impl_;
98  }
99  }
100 
101  ImplToFst() = delete;
102 
103  ImplToFst(const ImplToFst &fst) : impl_(fst.impl_) {}
104 
105  ImplToFst(ImplToFst &&fst) noexcept : impl_(std::move(fst.impl_)) {
106  fst.impl_ = std::make_shared<Impl>();
107  }
108 
110  impl_ = fst.impl_;
111  return *this;
112  }
113 
115  if (this != &fst) {
116  impl_ = std::move(fst.impl_);
117  fst.impl_ = std::make_shared<Impl>();
118  }
119  return *this;
120  }
121 
122  // Returns raw pointers to the shared object.
123  const Impl *GetImpl() const { return impl_.get(); }
124 
125  Impl *GetMutableImpl() const { return impl_.get(); }
126 
127  // Returns a ref-counted smart poiner to the implementation.
128  std::shared_ptr<Impl> GetSharedImpl() const { return impl_; }
129 
130  bool Unique() const { return impl_.unique(); }
131 
132  void SetImpl(std::shared_ptr<Impl> impl) { impl_ = std::move(impl); }
133 
134  private:
135  template <class IFST, class OFST>
136  friend void Cast(const IFST &ifst, OFST *ofst);
137 
138  std::shared_ptr<Impl> impl_;
139 };
140 
141 } // namespace fst
142 
143 #endif // FST_IMPL_TO_FST_H_
uint64_t TestProperties(const Fst< Arc > &fst, uint64_t mask, uint64_t *known)
const SymbolTable * OutputSymbols() const override
Definition: impl-to-fst.h:82
ImplToFst & operator=(const ImplToFst &fst)
Definition: impl-to-fst.h:109
std::shared_ptr< Impl > GetSharedImpl() const
Definition: impl-to-fst.h:128
StateId Start() const override
Definition: impl-to-fst.h:47
ImplToFst(const ImplToFst &fst)
Definition: impl-to-fst.h:103
size_t NumArcs(StateId s) const override
Definition: impl-to-fst.h:51
ImplToFst(const ImplToFst &fst, bool safe)
Definition: impl-to-fst.h:93
size_t NumInputEpsilons(StateId s) const override
Definition: impl-to-fst.h:53
friend void Cast(const IFST &ifst, OFST *ofst)
Definition: fst.h:942
const std::string & Type() const override
Definition: impl-to-fst.h:76
ImplToFst & operator=(ImplToFst &&fst) noexcept
Definition: impl-to-fst.h:114
size_t NumOutputEpsilons(StateId s) const override
Definition: impl-to-fst.h:57
const SymbolTable * InputSymbols() const override
Definition: impl-to-fst.h:78
void SetImpl(std::shared_ptr< Impl > impl)
Definition: impl-to-fst.h:132
ImplToFst(ImplToFst &&fst) noexcept
Definition: impl-to-fst.h:105
typename internal::DeterminizeFstImplBase< A >::Arc Arc
Definition: fst.h:205
Weight Final(StateId s) const override
Definition: impl-to-fst.h:49
ImplToFst(std::shared_ptr< Impl > impl)
Definition: impl-to-fst.h:87
Impl * GetMutableImpl() const
Definition: impl-to-fst.h:125
bool Unique() const
Definition: impl-to-fst.h:130
ImplToFst()=delete
uint64_t Properties(uint64_t mask, bool test) const override
Definition: impl-to-fst.h:63
const Impl * GetImpl() const
Definition: impl-to-fst.h:123