1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00
dragonflydb-dragonfly/core/resp_expr.h

53 lines
1.2 KiB
C
Raw Normal View History

// Copyright 2021, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
2021-11-16 09:39:02 +00:00
//
#pragma once
#include <absl/strings/ascii.h>
#include <absl/types/span.h>
#include <variant>
#include <vector>
namespace dfly {
class RespExpr {
public:
using Buffer = absl::Span<uint8_t>;
enum Type : uint8_t { STRING, ARRAY, INT64, NIL, NIL_ARRAY, ERROR };
using Vec = std::vector<RespExpr>;
Type type;
bool has_support; // whether pointers in this item are supported by external storage.
std::variant<int64_t, Buffer, Vec*> u;
RespExpr(Type t = NIL) : type(t), has_support(false) {
}
static Buffer buffer(std::string* s) {
return Buffer{reinterpret_cast<uint8_t*>(s->data()), s->size()};
}
Buffer GetBuf() const { return std::get<Buffer>(u); }
static const char* TypeName(Type t);
};
using RespVec = RespExpr::Vec;
using RespSpan = absl::Span<const RespExpr>;
inline std::string_view ToSV(const absl::Span<uint8_t>& s) {
2021-11-16 09:39:02 +00:00
return std::string_view{reinterpret_cast<char*>(s.data()), s.size()};
}
} // namespace dfly
namespace std {
2021-11-16 09:39:02 +00:00
ostream& operator<<(ostream& os, const dfly::RespExpr& e);
ostream& operator<<(ostream& os, dfly::RespSpan rspan);
} // namespace std