status.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Exception(const char *expception_class_name, const char *error_message)
  42. { return Status(kException, expception_class_name, error_message); }
  43. static Status Error(const char *error_message)
  44. { return Exception("Exception", error_message); }
  45. // Returns true iff the status indicates success.
  46. bool ok() const { return (state_ == NULL); }
  47. // Return a string representation of this status suitable for printing.
  48. // Returns the string "OK" for success.
  49. std::string ToString() const;
  50. int code() const {
  51. return (state_ == NULL) ? kOk : static_cast<int>(state_[4]);
  52. }
  53. enum Code {
  54. kOk = 0,
  55. kInvalidArgument = static_cast<unsigned>(std::errc::invalid_argument),
  56. kResourceUnavailable = static_cast<unsigned>(std::errc::resource_unavailable_try_again),
  57. kUnimplemented = static_cast<unsigned>(std::errc::function_not_supported),
  58. kException = 255,
  59. };
  60. private:
  61. // OK status has a NULL state_. Otherwise, state_ is a new[] array
  62. // of the following form:
  63. // state_[0..3] == length of message
  64. // state_[4] == code
  65. // state_[5..] == message
  66. const char* state_;
  67. explicit Status(int code, const char *msg1, const char *msg2);
  68. static const char *CopyState(const char* s);
  69. static const char *ConstructState(int code, const char *msg1, const char *msg2);
  70. };
  71. inline Status::Status(const Status& s) {
  72. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  73. }
  74. inline void Status::operator=(const Status& s) {
  75. // The following condition catches both aliasing (when this == &s),
  76. // and the common case where both s and *this are ok.
  77. if (state_ != s.state_) {
  78. delete[] state_;
  79. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  80. }
  81. }
  82. }
  83. #endif