20#include "cbl/CBLBase.h"
21#include "cbl/CBLQueryTypes.h"
22#include "fleece/slice.hh"
31#include "cbl/CBLLog.h"
60 RefCounted() noexcept :_ref(
nullptr) { }
62 RefCounted(
const RefCounted &other) noexcept :_ref(
CBL_Retain(other._ref)) { }
63 RefCounted(RefCounted &&other) noexcept :_ref(other._ref) {other._ref =
nullptr;}
66 RefCounted& operator= (
const RefCounted &other)
noexcept {
73 RefCounted& operator= (RefCounted &&other)
noexcept {
74 if (other._ref != _ref) {
83 bool valid()
const {
return _ref !=
nullptr;}
84 explicit operator bool()
const {
return valid();}
97 struct Error: std::runtime_error {
103 : std::runtime_error(what)
108 : std::runtime_error(
"")
113 std::runtime_error::operator=(other);
123 inline std::string asString(
FLSlice s) {
return slice(s).asString();}
126 inline void check(
bool ok, CBLError &error) {
130 CBL_Log(kCBLLogDomainDatabase, kCBLLogError,
"API returning error %d/%d: %.*s",
131 error.
domain, error.
code, (
int)message.size, (
char*)message.buf);
133 throw cbl::Error{error.
domain, error.
code, message.asString()};
140#define CBL_REFCOUNTED_WITHOUT_COPY_MOVE_BOILERPLATE(CLASS, SUPER, C_TYPE) \
143 CLASS() noexcept :SUPER() { } \
145 CLASS& operator=(std::nullptr_t) {clear(); return *this;} \
147 bool valid() const {return RefCounted::valid();} \
149 explicit operator bool() const {return valid();} \
151 bool operator==(const CLASS &other) const {return _ref == other._ref;} \
153 bool operator!=(const CLASS &other) const {return _ref != other._ref;} \
155 C_TYPE* _cbl_nullable ref() const {return (C_TYPE*)_ref;}\
158 explicit CLASS(C_TYPE* _cbl_nullable ref) :SUPER((CBLRefCounted*)ref) { }
160#define CBL_REFCOUNTED_BOILERPLATE(CLASS, SUPER, C_TYPE) \
161CBL_REFCOUNTED_WITHOUT_COPY_MOVE_BOILERPLATE(CLASS, SUPER, C_TYPE) \
164 CLASS(const CLASS &other) noexcept :SUPER(other) { } \
166 CLASS(CLASS &&other) noexcept :SUPER((SUPER&&)other) { } \
168 CLASS& operator=(const CLASS &other) noexcept {SUPER::operator=(other); return *this;} \
170 CLASS& operator=(CLASS &&other) noexcept {SUPER::operator=((SUPER&&)other); return *this;}
176 template <
class... Args>
196 :_token(other._token),
197 _callback(std::move(other._callback))
198 {other._token =
nullptr;}
204 _token = other._token;
205 other._token =
nullptr;
206 _callback = std::move(other._callback);
230 (*listener)(args...);
235 std::shared_ptr<Callback> _callback;
bool operator==(const CBLError &e1, const CBLError &e2)
Equality for two CBLError values.
Definition Base.hh:39
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
Manages Couchbase Lite extensions, such as the Vector Search extension.
Definition Database.hh:55
void remove()
Unregisters the listener early, before it leaves scope.
Definition Base.hh:211
~ListenerToken()
Unregisters the listener (if any) and releases the token.
Definition Base.hh:185
static void call(void *_Nullable context, Args... args)
Static thunk used as the C-API callback.
Definition Base.hh:228
ListenerToken()=default
Creates an empty, unregistered token.
ListenerToken & operator=(ListenerToken &&other)
Move-assigns a token: removes this token's existing listener (if any) and adopts the other token's re...
Definition Base.hh:202
ListenerToken(ListenerToken &&other)
Move-constructs a token, transferring ownership of the underlying listener registration.
Definition Base.hh:195
CBLListenerToken *_Nullable token() const
Returns the underlying CBLListenerToken (the C registration handle), or NULL if not registered.
Definition Base.hh:221
ListenerToken(Callback cb)
Creates a token wrapping the given callback.
Definition Base.hh:190
void *_Nullable context() const
Returns an opaque pointer used internally as the context argument for C-API callbacks.
Definition Base.hh:218
void setToken(CBLListenerToken *token)
Assigns the underlying CBLListenerToken returned from a C-API AddXxxListener call.
Definition Base.hh:224
std::function< void(Args...)> Callback
The type of the user callback that this token holds.
Definition Base.hh:180
A helper object for database transactions.
Definition Database.hh:495
FLSliceResult CBLError_Message(const CBLError *_cbl_nullable outError)
void CBLListener_Remove(CBLListenerToken *_cbl_nullable)
struct CBLListenerToken CBLListenerToken
void CBL_Log(CBLLogDomain domain, CBLLogLevel level, const char *format,...) __printflike(3
struct CBLRefCounted CBLRefCounted
void CBL_Release(CBLRefCounted *_cbl_nullable)
CBLRefCounted * CBL_Retain(CBLRefCounted *_cbl_nullable)
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49
CBLQueryLanguage QueryLanguage
Definition Base.hh:53
fleece::alloc_slice alloc_slice
Convenience alias for fleece::alloc_slice, an owning byte buffer.
Definition Base.hh:51
int code
Error code, specific to the domain. 0 always means no error.
Definition Base.hh:119
Error & operator=(const Error &other)
Definition Base.hh:112
Error()
Definition Base.hh:107
Error(CBLErrorDomain domain, int code, const std::string &what)
Constructs an Error.
Definition Base.hh:102
CBLErrorDomain domain
Domain of errors.
Definition Base.hh:118