21 #ifndef FST_SCRIPT_WEIGHT_CLASS_H_ 22 #define FST_SCRIPT_WEIGHT_CLASS_H_ 33 #include <string_view> 41 virtual void Print(std::ostream *o)
const = 0;
42 virtual const std::string &
Type()
const = 0;
43 virtual std::string
ToString()
const = 0;
44 virtual bool Member()
const = 0;
63 const std::string &
Type() const final {
return W::Type(); }
65 void Print(std::ostream *ostrm)
const final { *ostrm << weight_; }
71 bool Member() const final {
return weight_.Member(); }
75 return weight_ == typed_other->weight_;
79 return !(*
this == other);
84 weight_ =
Plus(weight_, typed_other->weight_);
90 weight_ =
Times(weight_, typed_other->weight_);
96 weight_ =
Divide(weight_, typed_other->weight_);
101 weight_ =
Power(weight_, n);
123 WeightClass(std::string_view weight_type, std::string_view weight_str);
126 : impl_(other.impl_ ? other.impl_->
Copy() : nullptr) {}
129 impl_.reset(other.impl_ ? other.impl_->Copy() :
nullptr);
133 static constexpr std::string_view __ZERO__ =
"__ZERO__";
134 static constexpr std::string_view __ONE__ =
"__ONE__";
135 static constexpr std::string_view __NOWEIGHT__ =
"__NOWEIGHT__";
137 static WeightClass Zero(std::string_view weight_type);
139 static WeightClass One(std::string_view weight_type);
141 static WeightClass NoWeight(std::string_view weight_type);
145 if (W::Type() != impl_->Type()) {
153 std::string
ToString()
const {
return (impl_) ? impl_->ToString() :
"none"; }
155 const std::string &
Type()
const {
156 if (impl_)
return impl_->Type();
157 static const std::string *
const no_type =
new std::string(
"none");
161 bool Member()
const {
return impl_ && impl_->Member(); }
164 std::string_view op_name);
181 std::unique_ptr<WeightImplBase> impl_;
203 std::unique_ptr<WeightImplBase> (*)(std::string_view str);
208 return std::make_unique<WeightClassImpl<W>>(W::Zero());
210 return std::make_unique<WeightClassImpl<W>>(W::One());
212 return std::make_unique<WeightClassImpl<W>>(W::NoWeight());
214 return std::make_unique<WeightClassImpl<W>>(StrToWeight<W>(str));
219 WeightClassRegister> {
222 std::string legal_type(key);
224 legal_type.append(
".so");
233 #define REGISTER_FST_WEIGHT__(Weight, line) \ 234 static WeightClassRegisterer weight_registerer##_##line( \ 235 Weight::Type(), StrToWeightImplBase<Weight>) 238 #define REGISTER_FST_WEIGHT_EXPANDER(Weight, line) \ 239 REGISTER_FST_WEIGHT__(Weight, line) 242 #define REGISTER_FST_WEIGHT(Weight) \ 243 REGISTER_FST_WEIGHT_EXPANDER(Weight, __LINE__) 248 #endif // FST_SCRIPT_WEIGHT_CLASS_H_ WeightClass Divide(const WeightClass &lhs, const WeightClass &rhs)
WeightClassImpl< W > * Copy() const final
virtual bool operator!=(const WeightImplBase &other) const =0
void ConvertToLegalCSymbol(std::string *s)
WeightClassImpl< W > & DivideEq(const WeightImplBase &other) final
WeightClass(const WeightClass &other)
bool operator!=(const WeightImplBase &other) const final
WeightClass Plus(const WeightClass &lhs, const WeightClass &rhs)
static constexpr std::string_view __ONE__
virtual WeightImplBase & PowerEq(size_t n)=0
WeightClass & operator=(const WeightClass &other)
const std::string & Type() const
virtual bool Member() const =0
virtual WeightImplBase * Copy() const =0
WeightClass(const W &weight)
virtual ~WeightImplBase()=default
static constexpr std::string_view __NOWEIGHT__
std::string ConvertKeyToSoFilename(std::string_view key) const final
const std::string & Type() const final
virtual const std::string & Type() const =0
virtual WeightImplBase & DivideEq(const WeightImplBase &other)=0
std::unique_ptr< WeightImplBase > StrToWeightImplBase(std::string_view str)
WeightClassImpl< W > & TimesEq(const WeightImplBase &other) final
virtual std::string ToString() const =0
std::ostream & operator<<(std::ostream &o, const WeightClass &c)
virtual void Print(std::ostream *o) const =0
std::unique_ptr< WeightImplBase >(*)(std::string_view str) StrToWeightImplBaseT
std::string WeightToStr(Weight w)
virtual WeightImplBase & PlusEq(const WeightImplBase &other)=0
const W * GetWeight() const
bool Member() const final
WeightClassImpl< W > & PlusEq(const WeightImplBase &other) final
std::string ToString() const final
std::string ToString() const
WeightClassImpl(const W &weight)
virtual WeightImplBase & TimesEq(const WeightImplBase &other)=0
virtual bool operator==(const WeightImplBase &other) const =0
WeightClass(const WeightClassImpl< W > &impl)
void Print(std::ostream *ostrm) const final
WeightClassImpl< W > & PowerEq(size_t n) final
WeightClass Times(const WeightClass &lhs, const WeightClass &rhs)
bool operator==(const WeightImplBase &other) const final
WeightClass Power(const WeightClass &w, size_t n)
static constexpr std::string_view __ZERO__