You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

  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. #ifndef FST_SCRIPT_STATEITERATOR_CLASS_H_
  18. #define FST_SCRIPT_STATEITERATOR_CLASS_H_
  19. #include <cstdint>
  20. #include <memory>
  21. #include <utility>
  22. #include <fst/fst.h>
  23. #include <fst/fstlib.h>
  24. #include <fst/script/fst-class.h>
  25. // Scripting API support for StateIterator.
  26. namespace fst {
  27. namespace script {
  28. // Virtual interface implemented by each concrete StateIteratorImpl<F>.
  29. class StateIteratorImplBase {
  30. public:
  31. virtual bool Done() const = 0;
  32. virtual int64_t Value() const = 0;
  33. virtual void Next() = 0;
  34. virtual void Reset() = 0;
  35. virtual ~StateIteratorImplBase() = default;
  36. };
  37. // Templated implementation.
  38. template <class Arc>
  39. class StateIteratorClassImpl : public StateIteratorImplBase {
  40. public:
  41. explicit StateIteratorClassImpl(const Fst<Arc> &fst) : siter_(fst) {}
  42. bool Done() const final { return siter_.Done(); }
  43. int64_t Value() const final { return siter_.Value(); }
  44. void Next() final { siter_.Next(); }
  45. void Reset() final { siter_.Reset(); }
  46. ~StateIteratorClassImpl() override = default;
  47. private:
  48. StateIterator<Fst<Arc>> siter_;
  49. };
  50. class StateIteratorClass;
  51. using InitStateIteratorClassArgs =
  52. std::pair<const FstClass &, StateIteratorClass *>;
  53. // Untemplated user-facing class holding a templated pimpl.
  54. class StateIteratorClass {
  55. public:
  56. explicit StateIteratorClass(const FstClass &fst);
  57. template <class Arc>
  58. explicit StateIteratorClass(const Fst<Arc> &fst)
  59. : impl_(std::make_unique<StateIteratorClassImpl<Arc>>(fst)) {}
  60. bool Done() const { return impl_->Done(); }
  61. int64_t Value() const { return impl_->Value(); }
  62. void Next() { impl_->Next(); }
  63. void Reset() { impl_->Reset(); }
  64. template <class Arc>
  65. friend void InitStateIteratorClass(InitStateIteratorClassArgs *args);
  66. private:
  67. std::unique_ptr<StateIteratorImplBase> impl_;
  68. };
  69. template <class Arc>
  70. void InitStateIteratorClass(InitStateIteratorClassArgs *args) {
  71. const Fst<Arc> &fst = *std::get<0>(*args).GetFst<Arc>();
  72. std::get<1>(*args)->impl_ =
  73. std::make_unique<StateIteratorClassImpl<Arc>>(fst);
  74. }
  75. } // namespace script
  76. } // namespace fst
  77. #endif // FST_SCRIPT_STATEITERATOR_CLASS_H_