作者: 谢瑞阳 10225101483 徐翔宇 10225101535
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.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. #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_
  8. #define STORAGE_LEVELDB_INCLUDE_STATUS_H_
  9. #include <string>
  10. #include <utility>
  11. #include "leveldb/slice.h"
  12. namespace leveldb {
  13. class Status {
  14. public:
  15. // Create a success status.
  16. Status() : state_(NULL) { }
  17. ~Status() { delete state_; }
  18. // Copy the specified status.
  19. Status(const Status& s);
  20. void operator=(const Status& s);
  21. // Return a success status.
  22. static Status OK() { return Status(); }
  23. // Return error status of an appropriate type.
  24. static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
  25. return Status(kNotFound, msg, Slice());
  26. }
  27. static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
  28. return Status(kCorruption, msg, msg2);
  29. }
  30. static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
  31. return Status(kNotSupported, msg, msg2);
  32. }
  33. static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
  34. return Status(kInvalidArgument, msg, msg2);
  35. }
  36. static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
  37. return Status(kIOError, msg, msg2);
  38. }
  39. // Returns true iff the status indicates success.
  40. bool ok() const { return (state_ == NULL); }
  41. // Returns true iff the status indicates a NotFound error.
  42. bool IsNotFound() const { return code() == kNotFound; }
  43. // Return a string representation of this status suitable for printing.
  44. // Returns the string "OK" for success.
  45. std::string ToString() const;
  46. private:
  47. enum Code {
  48. kOk = 0,
  49. kNotFound = 1,
  50. kCorruption = 2,
  51. kNotSupported = 3,
  52. kInvalidArgument = 4,
  53. kIOError = 5,
  54. };
  55. Code code() const { return (state_ == NULL) ? kOk : state_->first; }
  56. Status(Code code, const Slice& msg, const Slice& msg2);
  57. typedef std::pair<Code, std::string> State;
  58. State* state_;
  59. };
  60. inline Status::Status(const Status& s) {
  61. state_ = (s.state_ == NULL) ? NULL : new State(*s.state_);
  62. }
  63. inline void Status::operator=(const Status& s) {
  64. if (this != &s) {
  65. delete state_;
  66. state_ = (s.state_ == NULL) ? NULL : new State(*s.state_);
  67. }
  68. }
  69. }
  70. #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_