作者: 谢瑞阳 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.

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