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 "CompilerSupport.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 namespace fleece { struct alloc_slice; }
29#endif
30
31
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38
45typedef struct FLSlice {
46 const void* FL_NULLABLE buf;
47 size_t size;
48
49#ifdef __cplusplus
50 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
51 explicit operator std::string() const {return std::string((char*)buf, size);}
52#endif
53} FLSlice;
54
55
63typedef struct FLSliceResult {
64 const void* FL_NULLABLE buf;
65 size_t size;
66
67#ifdef __cplusplus
68 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
69 explicit operator FLSlice () const {return {buf, size};}
70 inline explicit operator std::string() const;
71#endif
73
74
78#ifdef __cplusplus
79 struct FLHeapSlice : public FLSlice {
80 constexpr FLHeapSlice() noexcept :FLSlice{nullptr, 0} { }
81 private:
82 constexpr FLHeapSlice(const void *FL_NULLABLE b, size_t s) noexcept :FLSlice{b, s} { }
83 friend struct fleece::alloc_slice;
84 };
85#else
87#endif
88
89
90// Aliases used to indicate that a slice is expected to contain UTF-8 data.
93
94
96#ifdef _MSC_VER
97 static const FLSlice kFLSliceNull = { NULL, 0 };
98#else
99 #define kFLSliceNull ((FLSlice){NULL, 0})
100#endif
101
102
105static inline FLPURE int FLMemCmp(const void * FL_NULLABLE a,
106 const void * FL_NULLABLE b, size_t size) FLAPI
107{
108 if (_usuallyFalse(size == 0))
109 return 0;
110 return memcmp(a, b, size);
111}
112
115static inline void FLMemCpy(void* FL_NULLABLE dst, const void* FL_NULLABLE src, size_t size) FLAPI {
116 if (_usuallyTrue(size > 0))
117 memcpy(dst, src, size);
118}
119
120
125static inline FLSlice FLStr(const char* FL_NULLABLE str) FLAPI {
126 FLSlice foo = { str, str ? strlen(str) : 0 };
127 return foo;
128}
129
132#ifdef __cplusplus
133 #define FLSTR(STR) (FLSlice {("" STR), sizeof(("" STR))-1})
134#else
135 #define FLSTR(STR) ((FLSlice){("" STR), sizeof(("" STR))-1})
136#endif
137
138
141
145
148
156FLEECE_PUBLIC bool FLSlice_ToCString(FLSlice s, char* buffer, size_t capacity) FLAPI;
157
160
163
164
166static inline FLSliceResult FLSliceResult_CreateWith(const void* FL_NULLABLE bytes, size_t size) FLAPI {
167 FLSlice s = {bytes, size};
168 return FLSlice_Copy(s);
169}
170
171
172FLEECE_PUBLIC void _FLBuf_Retain(const void* FL_NULLABLE) FLAPI; // internal; do not call
173FLEECE_PUBLIC void _FLBuf_Release(const void* FL_NULLABLE) FLAPI; // internal; do not call
174
177 _FLBuf_Retain(s.buf);
178 return s;
179}
180
183 _FLBuf_Release(s.buf);
184}
185
188 FLSlice ret;
189 memcpy(&ret, &sr, sizeof(ret));
190 return ret;
191}
192
193
197FLEECE_PUBLIC void FL_WipeMemory(void *dst, size_t size) FLAPI;
198
199
202#ifdef __cplusplus
203}
204
205 FLPURE static inline bool operator== (FLSlice s1, FLSlice s2) {return FLSlice_Equal(s1, s2);}
206 FLPURE static inline bool operator!= (FLSlice s1, FLSlice s2) {return !(s1 == s2);}
207
208 FLPURE static inline bool operator== (FLSliceResult sr, FLSlice s) {return (FLSlice)sr == s;}
209 FLPURE static inline bool operator!= (FLSliceResult sr, FLSlice s) {return !(sr ==s);}
210
211
212 FLSliceResult::operator std::string () const {
213 auto str = std::string((char*)buf, size);
215 return str;
216 }
217#endif
218
220#endif // _FLSLICE_H
#define FL_NULLABLE
Definition: CompilerSupport.h:74
#define FLEECE_PUBLIC
Definition: CompilerSupport.h:241
#define FL_ASSUME_NONNULL_BEGIN
Definition: CompilerSupport.h:72
#define _usuallyTrue(VAL)
Definition: CompilerSupport.h:55
#define _usuallyFalse(VAL)
Definition: CompilerSupport.h:56
#define FLPURE
Definition: CompilerSupport.h:110
#define FL_ASSUME_NONNULL_END
Definition: CompilerSupport.h:73
#define FLAPI
Definition: CompilerSupport.h:247
FLEECE_PUBLIC void _FLBuf_Release(const void *FL_NULLABLE)
static void FLSliceResult_Release(FLSliceResult s)
Decrements the ref-count of a FLSliceResult, freeing its memory if it reached zero.
Definition: FLSlice.h:182
FLEECE_PUBLIC void FL_WipeMemory(void *dst, size_t size)
Writes zeroes to size bytes of memory starting at dst.
FLEECE_PUBLIC void _FLBuf_Retain(const void *FL_NULLABLE)
FLEECE_PUBLIC FLSliceResult FLSliceResult_New(size_t)
Allocates an FLSliceResult of the given size, without initializing the buffer.
#define kFLSliceNull
A convenient constant denoting a null slice.
Definition: FLSlice.h:99
FLEECE_PUBLIC bool FLSlice_Equal(FLSlice a, FLSlice b) FLPURE
Equality test of two slices.
static void FLMemCpy(void *FL_NULLABLE dst, const void *FL_NULLABLE 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:115
static FLSliceResult FLSliceResult_Retain(FLSliceResult s)
Increments the ref-count of a FLSliceResult.
Definition: FLSlice.h:176
static FLSliceResult FLSliceResult_CreateWith(const void *FL_NULLABLE bytes, size_t size)
Allocates an FLSliceResult, copying size bytes starting at buf.
Definition: FLSlice.h:166
FLSliceResult FLStringResult
Definition: FLSlice.h:92
static FLSlice FLStr(const char *FL_NULLABLE str)
Returns a slice pointing to the contents of a C string.
Definition: FLSlice.h:125
FLSlice FLString
Definition: FLSlice.h:91
FLSlice FLHeapSlice
A heap-allocated, reference-counted slice.
Definition: FLSlice.h:86
static FLSlice FLSliceResult_AsSlice(FLSliceResult sr)
Type-casts a FLSliceResult to FLSlice, since C doesn't know it's a subclass.
Definition: FLSlice.h:187
static FLPURE int FLMemCmp(const void *FL_NULLABLE a, const void *FL_NULLABLE 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:105
FLEECE_PUBLIC 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.
FLEECE_PUBLIC int FLSlice_Compare(FLSlice, FLSlice) FLPURE
Lexicographic comparison of two slices; basically like memcmp(), but taking into account differences ...
FLEECE_PUBLIC uint32_t FLSlice_Hash(FLSlice s) FLPURE
Computes a 32-bit hash of a slice's data, suitable for use in hash tables.
FLEECE_PUBLIC FLSliceResult FLSlice_Copy(FLSlice)
Allocates an FLSliceResult, copying the given slice.
A simple reference to a block of memory.
Definition: FLSlice.h:45
size_t size
Definition: FLSlice.h:47
const void *FL_NULLABLE buf
Definition: FLSlice.h:46
A heap-allocated block of memory returned from an API call.
Definition: FLSlice.h:63
const void *FL_NULLABLE buf
Definition: FLSlice.h:64
size_t size
Definition: FLSlice.h:65