Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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