Couchbase Lite C
Couchbase Lite C API
FLSlice.h
Go to the documentation of this file.
1//
2// FLSlice.h
3// Fleece
4//
5// Created by Jens Alfke on 8/13/18.
6// Copyright 2018-Present Couchbase, Inc.
7//
8// Use of this software is governed by the Business Source License included
9// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
10// in that file, in accordance with the Business Source License, use of this
11// software will be governed by the Apache License, Version 2.0, included in
12// the file licenses/APL2.txt.
13//
14
15#pragma once
16#ifndef _FLSLICE_H
17#define _FLSLICE_H
18
19#include "Base.h"
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <string.h>
24
25
26#ifdef __cplusplus
27 #include <string>
28 #define FLAPI noexcept
29 namespace fleece { struct alloc_slice; }
30#else
31 #define FLAPI
32#endif
33
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39
46typedef struct FLSlice {
47 const void *buf;
48 size_t size;
49
50#ifdef __cplusplus
51 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
52 explicit operator std::string() const {return std::string((char*)buf, size);}
53#endif
54} FLSlice;
55
56
64typedef struct FLSliceResult {
65 const void *buf;
66 size_t size;
67
68#ifdef __cplusplus
69 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
70 explicit operator FLSlice () const {return {buf, size};}
71 inline explicit operator std::string() const;
72#endif
74
75
79#ifdef __cplusplus
80 struct FLHeapSlice : public FLSlice {
81 constexpr FLHeapSlice() noexcept :FLSlice{nullptr, 0} { }
82 private:
83 constexpr FLHeapSlice(const void *b, size_t s) noexcept :FLSlice{b, s} { }
84 friend struct fleece::alloc_slice;
85 };
86#else
88#endif
89
90
91// Aliases used to indicate that a slice is expected to contain UTF-8 data.
94
95
97#ifdef _MSC_VER
98 static const FLSlice kFLSliceNull = { NULL, 0 };
99#else
100 #define kFLSliceNull ((FLSlice){NULL, 0})
101#endif
102
103
106static inline FLPURE int FLMemCmp(const void *a, const void *b, size_t size) FLAPI {
107 if (_usuallyFalse(size == 0))
108 return 0;
109 return memcmp(a, b, size);
110}
111
114static inline void FLMemCpy(void *dst, const void *src, size_t size) FLAPI {
115 if (_usuallyTrue(size > 0))
116 memcpy(dst, src, size);
117}
118
119
124static inline FLSlice FLStr(const char *str) FLAPI {
125 FLSlice foo = { str, str ? strlen(str) : 0 };
126 return foo;
127}
128
131#ifdef __cplusplus
132 #define FLSTR(STR) (FLSlice {("" STR), sizeof(("" STR))-1})
133#else
134 #define FLSTR(STR) ((FLSlice){("" STR), sizeof(("" STR))-1})
135#endif
136
137
140
144
147
155bool FLSlice_ToCString(FLSlice s, char* buffer NONNULL, size_t capacity) FLAPI;
156
159
162
163
165static inline FLSliceResult FLSliceResult_CreateWith(const void *bytes, size_t size) FLAPI {
166 FLSlice s = {bytes, size};
167 return FLSlice_Copy(s);
168}
169
170
171void _FLBuf_Retain(const void*) FLAPI; // internal; do not call
172void _FLBuf_Release(const void*) FLAPI; // internal; do not call
173
176 _FLBuf_Retain(s.buf);
177 return s;
178}
179
182 _FLBuf_Release(s.buf);
183}
184
187 return *(FLSlice*)&sr;
188}
189
190
194void FL_WipeMemory(void *dst, size_t size) FLAPI;
195
196
199#ifdef __cplusplus
200}
201
202 FLPURE static inline bool operator== (FLSlice s1, FLSlice s2) {return FLSlice_Equal(s1, s2);}
203 FLPURE static inline bool operator!= (FLSlice s1, FLSlice s2) {return !(s1 == s2);}
204
205 FLPURE static inline bool operator== (FLSliceResult sr, FLSlice s) {return (FLSlice)sr == s;}
206 FLPURE static inline bool operator!= (FLSliceResult sr, FLSlice s) {return !(sr ==s);}
207
208
209 FLSliceResult::operator std::string () const {
210 auto str = std::string((char*)buf, size);
212 return str;
213 }
214#endif
215
216#endif // _FLSLICE_H
#define _usuallyTrue(VAL)
Definition: Base.h:55
#define _usuallyFalse(VAL)
Definition: Base.h:56
#define FLPURE
Definition: Base.h:89
#define NONNULL
Definition: Base.h:68
#define FLAPI
Definition: FLSlice.h:31
bool FLSlice_Equal(FLSlice a, FLSlice b) FLPURE
Equality test of two slices.
static void FLSliceResult_Release(FLSliceResult s)
Decrements the ref-count of a FLSliceResult, freeing its memory if it reached zero.
Definition: FLSlice.h:181
static FLPURE int FLMemCmp(const void *a, const void *b, size_t size)
Exactly like memcmp, but safely handles the case where a or b is NULL and size is 0 (by returning 0),...
Definition: FLSlice.h:106
#define kFLSliceNull
A convenient constant denoting a null slice.
Definition: FLSlice.h:100
static void FLMemCpy(void *dst, const void *src, size_t size)
Exactly like memcmp, but safely handles the case where dst or src is NULL and size is 0 (as a no-op),...
Definition: FLSlice.h:114
FLSliceResult FLSliceResult_New(size_t)
Allocates an FLSliceResult of the given size, without initializing the buffer.
void _FLBuf_Retain(const void *)
void _FLBuf_Release(const void *)
static FLSliceResult FLSliceResult_Retain(FLSliceResult s)
Increments the ref-count of a FLSliceResult.
Definition: FLSlice.h:175
int FLSlice_Compare(FLSlice, FLSlice) FLPURE
Lexicographic comparison of two slices; basically like memcmp(), but taking into account differences ...
FLSliceResult FLStringResult
Definition: FLSlice.h:93
FLSlice FLString
Definition: FLSlice.h:92
FLSlice FLHeapSlice
A heap-allocated, reference-counted slice.
Definition: FLSlice.h:87
static FLSlice FLSliceResult_AsSlice(FLSliceResult sr)
Type-casts a FLSliceResult to FLSlice, since C doesn't know it's a subclass.
Definition: FLSlice.h:186
uint32_t FLSlice_Hash(FLSlice s) FLPURE
Computes a 32-bit hash of a slice's data, suitable for use in hash tables.
bool FLSlice_ToCString(FLSlice s, char *buffer, size_t capacity)
Copies a slice to a buffer, adding a trailing zero byte to make it a valid C string.
FLSliceResult FLSlice_Copy(FLSlice)
Allocates an FLSliceResult, copying the given slice.
void FL_WipeMemory(void *dst, size_t size)
Writes zeroes to size bytes of memory starting at dst.
static FLSlice FLStr(const char *str)
Returns a slice pointing to the contents of a C string.
Definition: FLSlice.h:124
static FLSliceResult FLSliceResult_CreateWith(const void *bytes, size_t size)
Allocates an FLSliceResult, copying size bytes starting at buf.
Definition: FLSlice.h:165
A simple reference to a block of memory.
Definition: FLSlice.h:46
const void * buf
Definition: FLSlice.h:47
size_t size
Definition: FLSlice.h:48
A heap-allocated block of memory returned from an API call.
Definition: FLSlice.h:64
const void * buf
Definition: FLSlice.h:65
size_t size
Definition: FLSlice.h:66