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