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