作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

112 rivejä
3.5 KiB

  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 <string>
  15. #include "leveldb/slice.h"
  16. namespace leveldb {
  17. class Status {
  18. public:
  19. // Create a success status.
  20. Status() : state_(NULL) { }
  21. ~Status() { delete[] state_; }
  22. // Copy the specified status.
  23. Status(const Status& s);
  24. void operator=(const Status& s);
  25. // Return a success status.
  26. static Status OK() { return Status(); }
  27. // Return error status of an appropriate type.
  28. static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
  29. return Status(kNotFound, msg, msg2);
  30. }
  31. static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
  32. return Status(kCorruption, msg, msg2);
  33. }
  34. static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
  35. return Status(kNotSupported, msg, msg2);
  36. }
  37. static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
  38. return Status(kInvalidArgument, msg, msg2);
  39. }
  40. static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
  41. return Status(kIOError, msg, msg2);
  42. }
  43. // Returns true iff the status indicates success.
  44. bool ok() const { return (state_ == NULL); }
  45. // Returns true iff the status indicates a NotFound error.
  46. bool IsNotFound() const { return code() == kNotFound; }
  47. // Returns true iff the status indicates a Corruption error.
  48. bool IsCorruption() const { return code() == kCorruption; }
  49. // Returns true iff the status indicates an IOError.
  50. bool IsIOError() const { return code() == kIOError; }
  51. // Returns true iff the status indicates a NotSupportedError.
  52. bool IsNotSupportedError() const { return code() == kNotSupported; }
  53. // Returns true iff the status indicates an InvalidArgument.
  54. bool IsInvalidArgument() const { return code() == kInvalidArgument; }
  55. // Return a string representation of this status suitable for printing.
  56. // Returns the string "OK" for success.
  57. std::string ToString() const;
  58. private:
  59. // OK status has a NULL state_. Otherwise, state_ is a new[] array
  60. // of the following form:
  61. // state_[0..3] == length of message
  62. // state_[4] == code
  63. // state_[5..] == message
  64. const char* state_;
  65. enum Code {
  66. kOk = 0,
  67. kNotFound = 1,
  68. kCorruption = 2,
  69. kNotSupported = 3,
  70. kInvalidArgument = 4,
  71. kIOError = 5
  72. };
  73. Code code() const {
  74. return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
  75. }
  76. Status(Code code, const Slice& msg, const Slice& msg2);
  77. static const char* CopyState(const char* s);
  78. };
  79. inline Status::Status(const Status& s) {
  80. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  81. }
  82. inline void Status::operator=(const Status& s) {
  83. // The following condition catches both aliasing (when this == &s),
  84. // and the common case where both s and *this are ok.
  85. if (state_ != s.state_) {
  86. delete[] state_;
  87. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  88. }
  89. }
  90. } // namespace leveldb
  91. #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_