FST  openfst-1.8.3
OpenFst Library
stateiterator-class.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 #ifndef FST_SCRIPT_STATEITERATOR_CLASS_H_
19 #define FST_SCRIPT_STATEITERATOR_CLASS_H_
20 
21 #include <cstdint>
22 #include <memory>
23 #include <utility>
24 
25 #include <fst/fst.h>
26 #include <fst/fstlib.h>
27 #include <fst/script/fst-class.h>
28 
29 // Scripting API support for StateIterator.
30 
31 namespace fst {
32 namespace script {
33 
34 // Virtual interface implemented by each concrete StateIteratorImpl<F>.
36  public:
37  virtual bool Done() const = 0;
38  virtual int64_t Value() const = 0;
39  virtual void Next() = 0;
40  virtual void Reset() = 0;
41  virtual ~StateIteratorImplBase() = default;
42 };
43 
44 // Templated implementation.
45 template <class Arc>
47  public:
48  explicit StateIteratorClassImpl(const Fst<Arc> &fst) : siter_(fst) {}
49 
50  bool Done() const final { return siter_.Done(); }
51 
52  int64_t Value() const final { return siter_.Value(); }
53 
54  void Next() final { siter_.Next(); }
55 
56  void Reset() final { siter_.Reset(); }
57 
58  ~StateIteratorClassImpl() override = default;
59 
60  private:
61  StateIterator<Fst<Arc>> siter_;
62 };
63 
64 class StateIteratorClass;
65 
67  std::pair<const FstClass &, StateIteratorClass *>;
68 
69 // Untemplated user-facing class holding a templated pimpl.
71  public:
72  explicit StateIteratorClass(const FstClass &fst);
73 
74  template <class Arc>
75  explicit StateIteratorClass(const Fst<Arc> &fst)
76  : impl_(std::make_unique<StateIteratorClassImpl<Arc>>(fst)) {}
77 
78  bool Done() const { return impl_->Done(); }
79 
80  int64_t Value() const { return impl_->Value(); }
81 
82  void Next() { impl_->Next(); }
83 
84  void Reset() { impl_->Reset(); }
85 
86  template <class Arc>
88 
89  private:
90  std::unique_ptr<StateIteratorImplBase> impl_;
91 };
92 
93 template <class Arc>
95  const Fst<Arc> &fst = *std::get<0>(*args).GetFst<Arc>();
96  std::get<1>(*args)->impl_ =
97  std::make_unique<StateIteratorClassImpl<Arc>>(fst);
98 }
99 
100 } // namespace script
101 } // namespace fst
102 
103 #endif // FST_SCRIPT_STATEITERATOR_CLASS_H_
StateIteratorClassImpl(const Fst< Arc > &fst)
StateIteratorClass(const Fst< Arc > &fst)
std::pair< const FstClass &, StateIteratorClass * > InitStateIteratorClassArgs
virtual bool Done() const =0
void InitStateIteratorClass(InitStateIteratorClassArgs *args)
virtual ~StateIteratorImplBase()=default
virtual int64_t Value() const =0