FST  openfst-1.8.2
OpenFst Library
stateiterator-class.h
Go to the documentation of this file.
1 // Copyright 2005-2020 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 
24 #include <fst/fstlib.h>
25 #include <fst/script/fst-class.h>
26 
27 // Scripting API support for StateIterator.
28 
29 namespace fst {
30 namespace script {
31 
32 // Virtual interface implemented by each concrete StateIteratorImpl<F>.
34  public:
35  virtual bool Done() const = 0;
36  virtual int64_t Value() const = 0;
37  virtual void Next() = 0;
38  virtual void Reset() = 0;
40 };
41 
42 // Templated implementation.
43 template <class Arc>
45  public:
46  explicit StateIteratorClassImpl(const Fst<Arc> &fst) : siter_(fst) {}
47 
48  bool Done() const final { return siter_.Done(); }
49 
50  int64_t Value() const final { return siter_.Value(); }
51 
52  void Next() final { siter_.Next(); }
53 
54  void Reset() final { siter_.Reset(); }
55 
57 
58  private:
59  StateIterator<Fst<Arc>> siter_;
60 };
61 
62 class StateIteratorClass;
63 
65  std::pair<const FstClass &, StateIteratorClass *>;
66 
67 // Untemplated user-facing class holding a templated pimpl.
69  public:
70  explicit StateIteratorClass(const FstClass &fst);
71 
72  template <class Arc>
73  explicit StateIteratorClass(const Fst<Arc> &fst)
74  : impl_(std::make_unique<StateIteratorClassImpl<Arc>>(fst)) {}
75 
76  bool Done() const { return impl_->Done(); }
77 
78  int64_t Value() const { return impl_->Value(); }
79 
80  void Next() { impl_->Next(); }
81 
82  void Reset() { impl_->Reset(); }
83 
84  template <class Arc>
86 
87  private:
88  std::unique_ptr<StateIteratorImplBase> impl_;
89 };
90 
91 template <class Arc>
93  const Fst<Arc> &fst = *std::get<0>(*args).GetFst<Arc>();
94  std::get<1>(*args)->impl_ =
95  std::make_unique<StateIteratorClassImpl<Arc>>(fst);
96 }
97 
98 } // namespace script
99 } // namespace fst
100 
101 #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 int64_t Value() const =0