1 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 // 14 // Copyright 2005-2010 Google, Inc. 15 // Author: jpr@google.com (Jake Ratkiewicz) 16 17 #ifndef FST_SCRIPT_REGISTER_H_ 18 #define FST_SCRIPT_REGISTER_H_ 19 20 #include <string> 21 22 #include <fst/generic-register.h> 23 #include <fst/script/fst-class.h> 24 #include <fst/script/weight-class.h> 25 26 // Holds methods and classes responsible for maintaining 27 // the register for FstClass arc types. 28 29 namespace fst { 30 namespace script { 31 32 // 33 // Registers for reading and converting various kinds of FST classes. 34 // 35 36 // This class definition is to avoid a nested class definition inside 37 // the IORegistration struct. 38 template<class Reader, class Creator, class Converter> 39 struct FstClassRegEntry { 40 Reader reader; 41 Creator creator; 42 Converter converter; 43 FstClassRegEntryFstClassRegEntry44 FstClassRegEntry(Reader r, Creator cr, Converter co) : 45 reader(r), creator(cr), converter(co) { } FstClassRegEntryFstClassRegEntry46 FstClassRegEntry() : reader(0), creator(0), converter(0) { } 47 }; 48 49 template<class Reader, class Creator, class Converter> 50 class FstClassIORegister 51 : public GenericRegister<string, 52 FstClassRegEntry<Reader, Creator, Converter>, 53 FstClassIORegister<Reader, Creator, 54 Converter> > { 55 public: GetReader(const string & arc_type)56 Reader GetReader(const string &arc_type) const { 57 return this->GetEntry(arc_type).reader; 58 } 59 GetCreator(const string & arc_type)60 Creator GetCreator(const string &arc_type) const { 61 return this->GetEntry(arc_type).creator; 62 } 63 GetConverter(const string & arc_type)64 Converter GetConverter(const string &arc_type) const { 65 return this->GetEntry(arc_type).converter; 66 } 67 68 protected: ConvertKeyToSoFilename(const string & key)69 virtual string ConvertKeyToSoFilename( 70 const string& key) const { 71 string legal_type(key); 72 ConvertToLegalCSymbol(&legal_type); 73 74 return legal_type + "-arc.so"; 75 } 76 }; 77 78 // 79 // Struct containing everything needed to register a particular type 80 // of FST class (e.g. a plain FstClass, or a MutableFstClass, etc) 81 // 82 template<class FstClassType> 83 struct IORegistration { 84 typedef FstClassType *(*Reader)(istream &stream, 85 const FstReadOptions &opts); 86 87 typedef FstClassImplBase *(*Creator)(); 88 typedef FstClassImplBase *(*Converter)(const FstClass &other); 89 90 typedef FstClassRegEntry<Reader, Creator, Converter> Entry; 91 92 // FST class Register 93 typedef FstClassIORegister<Reader, Creator, Converter> Register; 94 95 // FST class Register-er 96 typedef GenericRegisterer<FstClassIORegister<Reader, Creator, Converter> > 97 Registerer; 98 }; 99 100 101 // 102 // REGISTRATION MACROS 103 // 104 105 #define REGISTER_FST_CLASS(Class, Arc) \ 106 static IORegistration<Class>::Registerer Class ## _ ## Arc ## _registerer( \ 107 Arc::Type(), \ 108 IORegistration<Class>::Entry(Class::Read<Arc>, \ 109 Class::Create<Arc>, \ 110 Class::Convert<Arc>)) 111 112 #define REGISTER_FST_CLASSES(Arc) \ 113 REGISTER_FST_CLASS(FstClass, Arc); \ 114 REGISTER_FST_CLASS(MutableFstClass, Arc); \ 115 REGISTER_FST_CLASS(VectorFstClass, Arc); 116 117 } // namespace script 118 } // namespace fst 119 120 #endif // FST_SCRIPT_REGISTER_H_ 121