FST  openfst-1.7.3
OpenFst Library
register.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 #ifndef FST_SCRIPT_REGISTER_H_
5 #define FST_SCRIPT_REGISTER_H_
6 
7 #include <istream>
8 #include <string>
9 
10 #include <fst/generic-register.h>
11 #include <fst/script/fst-class.h>
13 
14 // Holds methods and classes responsible for maintaining
15 // the register for FstClass arc types.
16 
17 namespace fst {
18 namespace script {
19 
20 // Registers for reading and converting various kinds of FST classes.
21 
22 // This class definition is to avoid a nested class definition inside the
23 // IORegistration struct.
24 
25 template <class Reader, class Creator, class Converter>
27  Reader reader;
28  Creator creator;
29  Converter converter;
30 
31  FstClassRegEntry(Reader r, Creator cr, Converter co)
32  : reader(r), creator(cr), converter(co) {}
33 
35  : reader(nullptr), creator(nullptr), converter(nullptr) {}
36 };
37 
38 template <class Reader, class Creator, class Converter>
40  : public GenericRegister<std::string,
41  FstClassRegEntry<Reader, Creator, Converter>,
42  FstClassIORegister<Reader, Creator, Converter>> {
43  public:
44  Reader GetReader(const std::string &arc_type) const {
45  return this->GetEntry(arc_type).reader;
46  }
47 
48  Creator GetCreator(const std::string &arc_type) const {
49  return this->GetEntry(arc_type).creator;
50  }
51 
52  Converter GetConverter(const std::string &arc_type) const {
53  return this->GetEntry(arc_type).converter;
54  }
55 
56  protected:
57  std::string ConvertKeyToSoFilename(const std::string &key) const final {
58  std::string legal_type(key);
59  ConvertToLegalCSymbol(&legal_type);
60  return legal_type + "-arc.so";
61  }
62 };
63 
64 // Struct containing everything needed to register a particular type
65 // of FST class (e.g., a plain FstClass, or a MutableFstClass, etc.).
66 template <class FstClassType>
68  using Reader = FstClassType *(*)(std::istream &stream,
69  const FstReadOptions &opts);
70 
71  using Creator = FstClassImplBase *(*)();
72 
73  using Converter = FstClassImplBase *(*)(const FstClass &other);
74 
76 
77  // FST class Register.
79 
80  // FST class Register-er.
81  using Registerer =
83 };
84 
85 #define REGISTER_FST_CLASS(Class, Arc) \
86  static IORegistration<Class>::Registerer Class##_##Arc##_registerer( \
87  Arc::Type(), \
88  IORegistration<Class>::Entry(Class::Read<Arc>, Class::Create<Arc>, \
89  Class::Convert<Arc>))
90 
91 #define REGISTER_FST_CLASSES(Arc) \
92  REGISTER_FST_CLASS(FstClass, Arc); \
93  REGISTER_FST_CLASS(MutableFstClass, Arc); \
94  REGISTER_FST_CLASS(VectorFstClass, Arc);
95 
96 } // namespace script
97 } // namespace fst
98 
99 #endif // FST_SCRIPT_REGISTER_H_
Creator GetCreator(const std::string &arc_type) const
Definition: register.h:48
void ConvertToLegalCSymbol(std::string *s)
Definition: util.cc:52
FstClassImplBase *(*)(const FstClass &other) Converter
Definition: register.h:73
Reader GetReader(const std::string &arc_type) const
Definition: register.h:44
FstClassRegEntry(Reader r, Creator cr, Converter co)
Definition: register.h:31
FstClassType *(*)(std::istream &stream, const FstReadOptions &opts) Reader
Definition: register.h:69
std::string ConvertKeyToSoFilename(const std::string &key) const final
Definition: register.h:57
Converter GetConverter(const std::string &arc_type) const
Definition: register.h:52