小组成员:姚凯文(kevinyao0901),姜嘉琪
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.

109 lines
3.4 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. // Return a string representation of this status suitable for printing.
  54. // Returns the string "OK" for success.
  55. std::string ToString() const;
  56. private:
  57. // OK status has a NULL state_. Otherwise, state_ is a new[] array
  58. // of the following form:
  59. // state_[0..3] == length of message
  60. // state_[4] == code
  61. // state_[5..] == message
  62. const char* state_;
  63. enum Code {
  64. kOk = 0,
  65. kNotFound = 1,
  66. kCorruption = 2,
  67. kNotSupported = 3,
  68. kInvalidArgument = 4,
  69. kIOError = 5
  70. };
  71. Code code() const {
  72. return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
  73. }
  74. Status(Code code, const Slice& msg, const Slice& msg2);
  75. static const char* CopyState(const char* s);
  76. };
  77. inline Status::Status(const Status& s) {
  78. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  79. }
  80. inline void Status::operator=(const Status& s) {
  81. // The following condition catches both aliasing (when this == &s),
  82. // and the common case where both s and *this are ok.
  83. if (state_ != s.state_) {
  84. delete[] state_;
  85. state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
  86. }
  87. }
  88. } // namespace leveldb
  89. #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_