1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00
dragonflydb-dragonfly/core/op_status.h
Roman Gershman 0cf2f57bf2 Fix expiry bug in AddOrFind function
Also add more test coverage.
Fix a bug where iterators might get invalidated between Renamer hops.
Initial work on BLPOP transaction.
2022-01-12 08:48:41 +02:00

95 lines
1.5 KiB
C++

// Copyright 2021, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
//
#pragma once
#include <ostream>
namespace dfly {
enum class OpStatus : uint16_t {
OK,
KEY_EXISTS,
KEY_NOTFOUND,
SKIPPED,
WRONG_TYPE,
TIMED_OUT,
};
class OpResultBase {
public:
OpResultBase(OpStatus st = OpStatus::OK) : st_(st) {
}
constexpr explicit operator bool() const {
return st_ == OpStatus::OK;
}
OpStatus status() const {
return st_;
}
bool operator==(OpStatus st) const {
return st_ == st;
}
bool ok() const {
return st_ == OpStatus::OK;
}
private:
OpStatus st_;
};
template <typename V> class OpResult : public OpResultBase {
public:
OpResult(V v) : v_(std::move(v)) {
}
using OpResultBase::OpResultBase;
const V& value() const {
return v_;
}
V& value() {
return v_;
}
V value_or(V v) const {
return status() == OpStatus::OK ? v_ : v;
}
const V* operator->() const {
return &v_;
}
private:
V v_;
};
template <> class OpResult<void> : public OpResultBase {
public:
using OpResultBase::OpResultBase;
};
inline bool operator==(OpStatus st, const OpResultBase& ob) {
return ob.operator==(st);
}
} // namespace dfly
namespace std {
template <typename T> std::ostream& operator<<(std::ostream& os, const dfly::OpResult<T>& res) {
os << int(res.status());
return os;
}
inline std::ostream& operator<<(std::ostream& os, const dfly::OpStatus op) {
os << int(op);
return os;
}
} // namespace std