iostatus.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <util/generic/yexception.h>
  3. class TIOStatus {
  4. public:
  5. TIOStatus(int status) noexcept
  6. : Status_(status)
  7. {
  8. }
  9. static TIOStatus Error(int status) noexcept {
  10. return TIOStatus(status);
  11. }
  12. static TIOStatus Error() noexcept {
  13. return TIOStatus(LastSystemError());
  14. }
  15. static TIOStatus Success() noexcept {
  16. return TIOStatus(0);
  17. }
  18. void Check() const {
  19. if (Status_) {
  20. ythrow TSystemError(Status_) << "io error";
  21. }
  22. }
  23. bool Failed() const noexcept {
  24. return (bool)Status_;
  25. }
  26. bool Succeed() const noexcept {
  27. return !Failed();
  28. }
  29. int Status() const noexcept {
  30. return Status_;
  31. }
  32. private:
  33. int Status_;
  34. };
  35. class TContIOStatus {
  36. public:
  37. TContIOStatus(size_t processed, TIOStatus status) noexcept
  38. : Processed_(processed)
  39. , Status_(status)
  40. {
  41. }
  42. static TContIOStatus Error(TIOStatus status) noexcept {
  43. return TContIOStatus(0, status);
  44. }
  45. static TContIOStatus Error() noexcept {
  46. return TContIOStatus(0, TIOStatus::Error());
  47. }
  48. static TContIOStatus Success(size_t processed) noexcept {
  49. return TContIOStatus(processed, TIOStatus::Success());
  50. }
  51. static TContIOStatus Eof() noexcept {
  52. return Success(0);
  53. }
  54. ~TContIOStatus() {
  55. }
  56. size_t Processed() const noexcept {
  57. return Processed_;
  58. }
  59. int Status() const noexcept {
  60. return Status_.Status();
  61. }
  62. size_t Checked() const {
  63. Status_.Check();
  64. return Processed_;
  65. }
  66. private:
  67. size_t Processed_;
  68. TIOStatus Status_;
  69. };