10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

100 lines
3.0 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. // Return a string representation of this status suitable for printing.
  48. // Returns the string "OK" for success.
  49. std::string ToString() const;
  50. private:
  51. // OK status has a NULL state_. Otherwise, state_ is a new[] array
  52. // of the following form:
  53. // state_[0..3] == length of message
  54. // state_[4] == code
  55. // state_[5..] == message
  56. const char* state_;
  57. enum Code {
  58. kOk = 0,
  59. kNotFound = 1,
  60. kCorruption = 2,
  61. kNotSupported = 3,
  62. kInvalidArgument = 4,
  63. kIOError = 5
  64. };
  65. Code code() const {
  66. return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
  67. }
  68. Status(Code code, const Slice& msg, const Slice& msg2);
  69. static const char* CopyState(const char* s);
  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 // STORAGE_LEVELDB_INCLUDE_STATUS_H_