status.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // A Status encapsulates the result of an operation. It may indicate success,
  6. // or it may indicate an error with an associated error message.
  7. //
  8. // Multiple threads can invoke const methods on a Status without
  9. // external synchronization, but if any of the threads may call a
  10. // non-const method, all threads accessing the same Status must use
  11. // external synchronization.
  12. #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
  13. #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
  14. #include <algorithm>
  15. #include <string>
  16. #include "leveldb/export.h"
  17. #include "leveldb/slice.h"
  18. namespace leveldb {
  19. class LEVELDB_EXPORT Status {
  20. public:
  21. // Create a success status.
  22. Status() noexcept : state_(nullptr) {}
  23. ~Status() { delete[] state_; }
  24. Status(const Status& rhs);
  25. Status& operator=(const Status& rhs);
  26. Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
  27. Status& operator=(Status&& rhs) noexcept;
  28. // Return a success status.
  29. static Status OK() { return Status(); }
  30. // Return error status of an appropriate type.
  31. static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
  32. return Status(kNotFound, msg, msg2);
  33. }
  34. static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
  35. return Status(kCorruption, msg, msg2);
  36. }
  37. static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
  38. return Status(kNotSupported, msg, msg2);
  39. }
  40. static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
  41. return Status(kInvalidArgument, msg, msg2);
  42. }
  43. static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
  44. return Status(kIOError, msg, msg2);
  45. }
  46. // Returns true iff the status indicates success.
  47. bool ok() const { return (state_ == nullptr); }
  48. // Returns true iff the status indicates a NotFound error.
  49. bool IsNotFound() const { return code() == kNotFound; }
  50. // Returns true iff the status indicates a Corruption error.
  51. bool IsCorruption() const { return code() == kCorruption; }
  52. // Returns true iff the status indicates an IOError.
  53. bool IsIOError() const { return code() == kIOError; }
  54. // Returns true iff the status indicates a NotSupportedError.
  55. bool IsNotSupportedError() const { return code() == kNotSupported; }
  56. // Returns true iff the status indicates an InvalidArgument.
  57. bool IsInvalidArgument() const { return code() == kInvalidArgument; }
  58. // Return a string representation of this status suitable for printing.
  59. // Returns the string "OK" for success.
  60. std::string ToString() const;
  61. private:
  62. enum Code {
  63. kOk = 0,
  64. kNotFound = 1,
  65. kCorruption = 2,
  66. kNotSupported = 3,
  67. kInvalidArgument = 4,
  68. kIOError = 5
  69. };
  70. Code code() const {
  71. return (state_ == nullptr) ? kOk : static_cast<Code>(state_[4]);
  72. }
  73. Status(Code code, const Slice& msg, const Slice& msg2);
  74. static const char* CopyState(const char* s);
  75. // OK status has a null state_. Otherwise, state_ is a new[] array
  76. // of the following form:
  77. // state_[0..3] == length of message
  78. // state_[4] == code
  79. // state_[5..] == message
  80. const char* state_;
  81. };
  82. inline Status::Status(const Status& rhs) {
  83. state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
  84. }
  85. inline Status& Status::operator=(const Status& rhs) {
  86. // The following condition catches both aliasing (when this == &rhs),
  87. // and the common case where both rhs and *this are ok.
  88. if (state_ != rhs.state_) {
  89. delete[] state_;
  90. state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_);
  91. }
  92. return *this;
  93. }
  94. inline Status& Status::operator=(Status&& rhs) noexcept {
  95. std::swap(state_, rhs.state_);
  96. return *this;
  97. }
  98. } // namespace leveldb
  99. #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_