status.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFSPP_STATUS_H_
  19. #define LIBHDFSPP_STATUS_H_
  20. #include <string>
  21. #include <system_error>
  22. namespace hdfs {
  23. class StatusHelper;
  24. class Status {
  25. public:
  26. // Create a success status.
  27. Status() : state_(NULL) { }
  28. ~Status() { delete[] state_; }
  29. explicit Status(int code, const char *msg);
  30. // Copy the specified status.
  31. Status(const Status& s);
  32. void operator=(const Status& s);
  33. // Return a success status.
  34. static Status OK() { return Status(); }
  35. static Status InvalidArgument(const char *msg)
  36. { return Status(kInvalidArgument, msg); }
  37. static Status ResourceUnavailable(const char *msg)
  38. { return Status(kResourceUnavailable, msg); }
  39. static Status Unimplemented()
  40. { return Status(kUnimplemented, ""); }
  41. static Status Error(const char *msg)
  42. { return Status(kGenericError, msg); }
  43. static Status InvalidEncryptionKey(const char *msg)
  44. { return Status(kInvalidEncryptionKey, msg); }
  45. static Status Exception(const char *expception_class_name, const char *error_message)
  46. { return Status(kException, expception_class_name, error_message); }
  47. // Returns true iff the status indicates success.
  48. bool ok() const { return (state_ == NULL); }
  49. // Return a string representation of this status suitable for printing.
  50. // Returns the string "OK" for success.
  51. std::string ToString() const;
  52. int code() const {
  53. return (state_ == NULL) ? kOk : static_cast<int>(state_[4]);
  54. }
  55. private:
  56. // OK status has a NULL state_. Otherwise, state_ is a new[] array
  57. // of the following form:
  58. // state_[0..3] == length of message
  59. // state_[4] == code
  60. // state_[5..] == message
  61. const char* state_;
  62. friend class StatusHelper;
  63. enum Code {
  64. kOk = 0,
  65. kInvalidArgument = static_cast<unsigned>(std::errc::invalid_argument),
  66. kResourceUnavailable = static_cast<unsigned>(std::errc::resource_unavailable_try_again),
  67. kGenericError = 1,
  68. kInvalidEncryptionKey = 2,
  69. kUnimplemented = 3,
  70. kException = 256,
  71. };
  72. explicit Status(int code, const char *msg1, const char *msg2);
  73. static const char *CopyState(const char* s);
  74. static const char *ConstructState(int code, const char *msg1, const char *msg2);
  75. };
  76. inline Status::Status(const Status& s) {
  77. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  78. }
  79. inline void Status::operator=(const Status& s) {
  80. // The following condition catches both aliasing (when this == &s),
  81. // and the common case where both s and *this are ok.
  82. if (state_ != s.state_) {
  83. delete[] state_;
  84. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  85. }
  86. }
  87. }
  88. #endif