FST  openfst-1.7.2
OpenFst Library
far-class.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Scripting API support for FarReader and FarWriter.
5 
6 #ifndef FST_EXTENSIONS_FAR_FAR_CLASS_H_
7 #define FST_EXTENSIONS_FAR_FAR_CLASS_H_
8 
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include <fst/extensions/far/far.h>
14 #include <fst/script/arg-packs.h>
15 #include <fst/script/fstscript.h>
16 
17 namespace fst {
18 namespace script {
19 
20 
21 // FarReader API.
22 
23 // Virtual interface implemented by each concrete FarReaderImpl<A>.
24 // See the FarReader interface in far.h for the exact semantics.
26  public:
27  virtual const string &ArcType() const = 0;
28  virtual bool Done() const = 0;
29  virtual bool Error() const = 0;
30  virtual const string &GetKey() const = 0;
31  virtual const FstClass *GetFstClass() const = 0;
32  virtual bool Find(const string &key) = 0;
33  virtual void Next() = 0;
34  virtual void Reset() = 0;
35  virtual FarType Type() const = 0;
36  virtual ~FarReaderImplBase() {}
37 };
38 
39 // Templated implementation.
40 template <class Arc>
42  public:
43  explicit FarReaderClassImpl(const string &filename)
44  : impl_(FarReader<Arc>::Open(filename)) {}
45 
46  explicit FarReaderClassImpl(const std::vector<string> &filenames)
47  : impl_(FarReader<Arc>::Open(filenames)) {}
48 
49  const string &ArcType() const final { return Arc::Type(); }
50 
51  bool Done() const final { return impl_->Done(); }
52 
53  bool Error() const final { return impl_->Error(); }
54 
55  bool Find(const string &key) final { return impl_->Find(key); }
56 
57  const FstClass *GetFstClass() const final {
58  fstc_.reset(new FstClass(*impl_->GetFst()));
59  return fstc_.get();
60  }
61 
62  const string &GetKey() const final { return impl_->GetKey(); }
63 
64  void Next() final { return impl_->Next(); }
65 
66  void Reset() final { impl_->Reset(); }
67 
68  FarType Type() const final { return impl_->Type(); }
69 
70  const FarReader<Arc> *GetImpl() const { return impl_.get(); }
71 
72  FarReader<Arc> *GetImpl() { return impl_.get(); }
73 
74  private:
75  std::unique_ptr<FarReader<Arc>> impl_;
76  mutable std::unique_ptr<FstClass> fstc_;
77 };
78 
79 
80 class FarReaderClass;
81 
84 
85 // Untemplated user-facing class holding a templated pimpl.
87  public:
88  const string &ArcType() const { return impl_->ArcType(); }
89 
90  bool Done() const { return impl_->Done(); }
91 
92  // Returns True if the impl is null (i.e., due to read failure).
93  // Attempting to call any other function will result in null dereference.
94  bool Error() const { return (impl_) ? impl_->Error() : true; }
95 
96  bool Find(const string &key) { return impl_->Find(key); }
97 
98  const FstClass *GetFstClass() const { return impl_->GetFstClass(); }
99 
100  const string &GetKey() const { return impl_->GetKey(); }
101 
102  void Next() { impl_->Next(); }
103 
104  void Reset() { impl_->Reset(); }
105 
106  FarType Type() const { return impl_->Type(); }
107 
108  template <class Arc>
109  const FarReader<Arc> *GetFarReader() const {
110  if (Arc::Type() != ArcType()) return nullptr;
111  const FarReaderClassImpl<Arc> *typed_impl =
112  static_cast<FarReaderClassImpl<Arc> *>(impl_.get());
113  return typed_impl->GetImpl();
114  }
115 
116  template <class Arc>
118  if (Arc::Type() != ArcType()) return nullptr;
119  FarReaderClassImpl<Arc> *typed_impl =
120  static_cast<FarReaderClassImpl<Arc> *>(impl_.get());
121  return typed_impl->GetImpl();
122  }
123 
124  template <class Arc>
125  friend void OpenFarReaderClass(OpenFarReaderClassArgs *args);
126 
127  // Defined in the CC.
128 
129  static FarReaderClass *Open(const string &filename);
130 
131  static FarReaderClass *Open(const std::vector<string> &filenames);
132 
133  private:
134  template <class Arc>
135  explicit FarReaderClass(FarReaderClassImpl<Arc> *impl) : impl_(impl) {}
136 
137  std::unique_ptr<FarReaderImplBase> impl_;
138 };
139 
140 // These exist solely for registration purposes; users should call the
141 // static method FarReaderClass::Open instead.
142 
143 template <class Arc>
145  args->retval = new FarReaderClass(new FarReaderClassImpl<Arc>(args->args));
146 }
147 
148 // FarWriter API.
149 
150 // Virtual interface implemented by each concrete FarWriterImpl<A>.
152  public:
153  // Unlike the lower-level library, this returns a boolean to signal failure
154  // due to non-conformant arc types.
155  virtual bool Add(const string &key, const FstClass &fst) = 0;
156  virtual const string &ArcType() const = 0;
157  virtual bool Error() const = 0;
158  virtual FarType Type() const = 0;
159  virtual ~FarWriterImplBase() {}
160 };
161 
162 
163 // Templated implementation.
164 template <class Arc>
166  public:
167  explicit FarWriterClassImpl(const string &filename,
168  FarType type = FAR_DEFAULT)
169  : impl_(FarWriter<Arc>::Create(filename, type)) {}
170 
171  bool Add(const string &key, const FstClass &fst) final {
172  if (ArcType() != fst.ArcType()) {
173  FSTERROR() << "Cannot write FST with " << fst.ArcType() << " arcs to "
174  << "FAR with " << ArcType() << " arcs";
175  return false;
176  }
177  impl_->Add(key, *(fst.GetFst<Arc>()));
178  return true;
179  }
180 
181  const string &ArcType() const final { return Arc::Type(); }
182 
183  bool Error() const final { return impl_->Error(); }
184 
185  FarType Type() const final { return impl_->Type(); }
186 
187  const FarWriter<Arc> *GetImpl() const { return impl_.get(); }
188 
189  FarWriter<Arc> *GetImpl() { return impl_.get(); }
190 
191  private:
192  std::unique_ptr<FarWriter<Arc>> impl_;
193 };
194 
195 
196 class FarWriterClass;
197 
198 using CreateFarWriterClassInnerArgs = std::pair<const string &, FarType>;
199 
202 
203 // Untemplated user-facing class holding a templated pimpl.
205  public:
206  static FarWriterClass *Create(const string &filename, const string &arc_type,
207  FarType type = FAR_DEFAULT);
208 
209  bool Add(const string &key, const FstClass &fst) {
210  return impl_->Add(key, fst);
211  }
212 
213  // Returns True if the impl is null (i.e., due to construction failure).
214  // Attempting to call any other function will result in null dereference.
215  bool Error() const { return (impl_) ? impl_->Error() : true; }
216 
217  const string &ArcType() const { return impl_->ArcType(); }
218 
219  FarType Type() const { return impl_->Type(); }
220 
221  template <class Arc>
222  const FarWriter<Arc> *GetFarWriter() const {
223  if (Arc::Type() != ArcType()) return nullptr;
224  const FarWriterClassImpl<Arc> *typed_impl =
225  static_cast<FarWriterClassImpl<Arc> *>(impl_.get());
226  return typed_impl->GetImpl();
227  }
228 
229  template <class Arc>
231  if (Arc::Type() != ArcType()) return nullptr;
232  FarWriterClassImpl<Arc> *typed_impl =
233  static_cast<FarWriterClassImpl<Arc> *>(impl_.get());
234  return typed_impl->GetImpl();
235  }
236 
237  template <class Arc>
239 
240  private:
241  template <class Arc>
242  explicit FarWriterClass(FarWriterClassImpl<Arc> *impl) : impl_(impl) {}
243 
244  std::unique_ptr<FarWriterImplBase> impl_;
245 };
246 
247 // This exists solely for registration purposes; users should call the
248 // static method FarWriterClass::Create instead.
249 template <class Arc>
252  std::get<0>(args->args), std::get<1>(args->args)));
253 }
254 
255 } // namespace script
256 } // namespace fst
257 
258 #endif // FST_EXTENSIONS_FAR_FAR_CLASS_H_
FarType Type() const final
Definition: far-class.h:68
const FarReader< Arc > * GetFarReader() const
Definition: far-class.h:109
virtual const string & ArcType() const =0
bool Find(const string &key) final
Definition: far-class.h:55
FarWriterClassImpl(const string &filename, FarType type=FAR_DEFAULT)
Definition: far-class.h:167
FarReaderClassImpl(const string &filename)
Definition: far-class.h:43
virtual const string & GetKey() const =0
bool Done() const final
Definition: far-class.h:51
const string & ArcType() const final
Definition: far-class.h:49
virtual bool Error() const =0
const FstClass * GetFstClass() const
Definition: far-class.h:98
FarWriter< Arc > * GetImpl()
Definition: far-class.h:189
FarReader< Arc > * GetFarReader()
Definition: far-class.h:117
const string & ArcType() const
Definition: far-class.h:217
#define FSTERROR()
Definition: util.h:35
FarType Type() const
Definition: far-class.h:219
bool Error() const final
Definition: far-class.h:183
const string & GetKey() const
Definition: far-class.h:100
bool Add(const string &key, const FstClass &fst) final
Definition: far-class.h:171
FarType
Definition: far.h:71
void OpenFarReaderClass(OpenFarReaderClassArgs *args)
Definition: far-class.h:144
virtual bool Find(const string &key)=0
FarReaderClassImpl(const std::vector< string > &filenames)
Definition: far-class.h:46
virtual const FstClass * GetFstClass() const =0
virtual FarType Type() const =0
FarType Type() const
Definition: far-class.h:106
FarType Type() const final
Definition: far-class.h:185
bool Error() const final
Definition: far-class.h:53
void CreateFarWriterClass(CreateFarWriterClassArgs *args)
Definition: far-class.h:250
bool Find(const string &key)
Definition: far-class.h:96
std::pair< const string &, FarType > CreateFarWriterClassInnerArgs
Definition: far-class.h:198
const FarWriter< Arc > * GetFarWriter() const
Definition: far-class.h:222
FarReader< Arc > * GetImpl()
Definition: far-class.h:72
FarWriter< Arc > * GetFarWriter()
Definition: far-class.h:230
const string & ArcType() const final
Definition: far-class.h:181
const string & GetKey() const final
Definition: far-class.h:62
const FarWriter< Arc > * GetImpl() const
Definition: far-class.h:187
virtual bool Done() const =0
const string & ArcType() const
Definition: far-class.h:88
const FarReader< Arc > * GetImpl() const
Definition: far-class.h:70
bool Add(const string &key, const FstClass &fst)
Definition: far-class.h:209
const FstClass * GetFstClass() const final
Definition: far-class.h:57