FST  openfst-1.8.2
OpenFst Library
register.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 // Classes for registering derived FST for generic reading.
19 
20 #ifndef FST_REGISTER_H_
21 #define FST_REGISTER_H_
22 
23 #include <string>
24 #include <type_traits>
25 
26 
27 #include <fst/compat.h>
28 #include <fst/generic-register.h>
29 #include <fst/util.h>
30 
31 
32 #include <fst/log.h>
33 #include <string_view>
34 
35 namespace fst {
36 
37 template <class Arc>
38 class Fst;
39 
40 struct FstReadOptions;
41 
42 // This class represents a single entry in a FstRegister
43 template <class Arc>
45  using Reader = Fst<Arc> *(*)(std::istream &istrm, const FstReadOptions &opts);
46  using Converter = Fst<Arc> *(*)(const Fst<Arc> &fst);
47 
50 
51  explicit FstRegisterEntry(Reader reader = nullptr,
52  Converter converter = nullptr)
53  : reader(reader), converter(converter) {}
54 };
55 
56 // This class maintains the correspondence between a string describing
57 // an FST type, and its reader and converter.
58 template <class Arc>
59 class FstRegister : public GenericRegister<std::string, FstRegisterEntry<Arc>,
60  FstRegister<Arc>> {
61  public:
64 
65  const Reader GetReader(std::string_view type) const {
66  return this->GetEntry(type).reader;
67  }
68 
69  const Converter GetConverter(std::string_view type) const {
70  return this->GetEntry(type).converter;
71  }
72 
73  protected:
74  std::string ConvertKeyToSoFilename(std::string_view key) const override {
75  std::string legal_type(key);
76  ConvertToLegalCSymbol(&legal_type);
77  legal_type.append("-fst.so");
78  return legal_type;
79  }
80 };
81 
82 // This class registers an FST type for generic reading and creating.
83 // The type must have a default constructor and a copy constructor from
84 // Fst<Arc>.
85 template <class FST>
86 class FstRegisterer : public GenericRegisterer<FstRegister<typename FST::Arc>> {
87  public:
88  using Arc = typename FST::Arc;
89  using Entry = typename FstRegister<Arc>::Entry;
90  using Reader = typename FstRegister<Arc>::Reader;
91 
93  : GenericRegisterer<FstRegister<typename FST::Arc>>(FST().Type(),
94  BuildEntry()) {}
95 
96  private:
97  static Fst<Arc> *ReadGeneric(std::istream &strm, const FstReadOptions &opts) {
98  static_assert(std::is_base_of_v<Fst<Arc>, FST>,
99  "FST class does not inherit from Fst<Arc>");
100  return FST::Read(strm, opts);
101  }
102 
103  static Entry BuildEntry() {
104  return Entry(&ReadGeneric, &FstRegisterer<FST>::Convert);
105  }
106 
107  static Fst<Arc> *Convert(const Fst<Arc> &fst) { return new FST(fst); }
108 };
109 
110 // Convenience macro to generate a static FstRegisterer instance.
111 // `FST` and `Arc` must be identifiers (that is, not a qualified type).
112 // Users SHOULD NOT register within the fst namespace. To register an
113 // FST for StdArc, for example, use:
114 // namespace example {
115 // using fst::StdArc;
116 // REGISTER_FST(MyFst, StdArc);
117 // } // namespace example
118 #define REGISTER_FST(FST, Arc) \
119  static fst::FstRegisterer<FST<Arc>> FST##_##Arc##_registerer
120 
121 // Converts an FST to the specified type.
122 template <class Arc>
123 Fst<Arc> *Convert(const Fst<Arc> &fst, std::string_view fst_type) {
124  auto *reg = FstRegister<Arc>::GetRegister();
125  const auto converter = reg->GetConverter(fst_type);
126  if (!converter) {
127  FSTERROR() << "Fst::Convert: Unknown FST type " << fst_type << " (arc type "
128  << Arc::Type() << ")";
129  return nullptr;
130  }
131  return converter(fst);
132 }
133 
134 } // namespace fst
135 
136 #endif // FST_REGISTER_H_
FstRegisterEntry(Reader reader=nullptr, Converter converter=nullptr)
Definition: register.h:51
void Convert(FarReader< Arc > &reader, FarWriter< Arc > &writer, std::string_view fst_type)
Definition: convert.h:27
const Reader GetReader(std::string_view type) const
Definition: register.h:65
void ConvertToLegalCSymbol(std::string *s)
Definition: util.cc:71
Converter converter
Definition: register.h:49
Fst< Arc > *(*)(std::istream &istrm, const FstReadOptions &opts) Reader
Definition: register.h:45
#define FSTERROR()
Definition: util.h:53
const Converter GetConverter(std::string_view type) const
Definition: register.h:69
std::string ConvertKeyToSoFilename(std::string_view key) const override
Definition: register.h:74
typename FstRegisterEntry< Arc >::Converter Converter
Definition: register.h:63
typename FstRegister< Arc >::Entry Entry
Definition: register.h:89
typename FstRegister< Arc >::Reader Reader
Definition: register.h:90
typename FST::Arc Arc
Definition: register.h:88
typename FstRegisterEntry< Arc >::Reader Reader
Definition: register.h:62