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 Couchbase. All rights reserved.
7//
8
9#pragma once
10#ifndef _FLSLICE_H
11#define _FLSLICE_H
12
13#include "Base.h"
14#include <stdbool.h>
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18
19
20#ifdef __cplusplus
21 #include <string>
22 #define FLAPI noexcept
23 namespace fleece { struct alloc_slice; }
24#else
25 #define FLAPI
26#endif
27
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33
40typedef struct FLSlice {
41 const void *buf;
42 size_t size;
43
44#ifdef __cplusplus
45 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
46 explicit operator std::string() const {return std::string((char*)buf, size);}
47#endif
48} FLSlice;
49
50
58typedef struct FLSliceResult {
59 const void *buf;
60 size_t size;
61
62#ifdef __cplusplus
63 explicit operator bool() const noexcept FLPURE {return buf != nullptr;}
64 explicit operator FLSlice () const {return {buf, size};}
65 inline explicit operator std::string() const;
66#endif
68
69
73#ifdef __cplusplus
74 struct FLHeapSlice : public FLSlice {
75 constexpr FLHeapSlice() noexcept :FLSlice{nullptr, 0} { }
76 private:
77 constexpr FLHeapSlice(const void *b, size_t s) noexcept :FLSlice{b, s} { }
78 friend struct fleece::alloc_slice;
79 };
80#else
82#endif
83
84
85// Aliases used to indicate that a slice is expected to contain UTF-8 data.
88
89
91#ifdef _MSC_VER
92 static const FLSlice kFLSliceNull = { NULL, 0 };
93#else
94 #define kFLSliceNull ((FLSlice){NULL, 0})
95#endif
96
97
100static inline FLPURE int FLMemCmp(const void *a, const void *b, size_t size) FLAPI {
101 if (_usuallyFalse(size == 0))
102 return 0;
103 return memcmp(a, b, size);
104}
105
108static inline void FLMemCpy(void *dst, const void *src, size_t size) FLAPI {
109 if (_usuallyTrue(size > 0))
110 memcpy(dst, src, size);
111}
112
113
118static inline FLSlice FLStr(const char *str) FLAPI {
119 FLSlice foo = { str, str ? strlen(str) : 0 };
120 return foo;
121}
122
125#ifdef __cplusplus
126 #define FLSTR(STR) (FLSlice {("" STR), sizeof(("" STR))-1})
127#else
128 #define FLSTR(STR) ((FLSlice){("" STR), sizeof(("" STR))-1})
129#endif
130
131
134
138
141
149bool FLSlice_ToCString(FLSlice s, char* buffer NONNULL, size_t capacity) FLAPI;
150
153
156
157
159static inline FLSliceResult FLSliceResult_CreateWith(const void *bytes, size_t size) FLAPI {
160 FLSlice s = {bytes, size};
161 return FLSlice_Copy(s);
162}
163
164
165void _FLBuf_Retain(const void*) FLAPI; // internal; do not call
166void _FLBuf_Release(const void*) FLAPI; // internal; do not call
167
170 _FLBuf_Retain(s.buf);
171 return s;
172}
173
176 _FLBuf_Release(s.buf);
177}
178
181 return *(FLSlice*)&sr;
182}
183
184
188void FL_WipeMemory(void *dst, size_t size) FLAPI;
189
190
193#ifdef __cplusplus
194}
195
196 FLPURE static inline bool operator== (FLSlice s1, FLSlice s2) {return FLSlice_Equal(s1, s2);}
197 FLPURE static inline bool operator!= (FLSlice s1, FLSlice s2) {return !(s1 == s2);}
198
199 FLPURE static inline bool operator== (FLSliceResult sr, FLSlice s) {return (FLSlice)sr == s;}
200 FLPURE static inline bool operator!= (FLSliceResult sr, FLSlice s) {return !(sr ==s);}
201
202
203 FLSliceResult::operator std::string () const {
204 auto str = std::string((char*)buf, size);
206 return str;
207 }
208#endif
209
210#endif // _FLSLICE_H
#define _usuallyTrue(VAL)
Definition: Base.h:49
#define _usuallyFalse(VAL)
Definition: Base.h:50
#define FLPURE
Definition: Base.h:83
#define NONNULL
Definition: Base.h:62
#define FLAPI
Definition: FLSlice.h:25
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:175
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:100
#define kFLSliceNull
A convenient constant denoting a null slice.
Definition: FLSlice.h:94
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:108
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:169
int FLSlice_Compare(FLSlice, FLSlice) FLPURE
Lexicographic comparison of two slices; basically like memcmp(), but taking into account differences ...
FLSliceResult FLStringResult
Definition: FLSlice.h:87
FLSlice FLString
Definition: FLSlice.h:86
FLSlice FLHeapSlice
A heap-allocated, reference-counted slice.
Definition: FLSlice.h:81
static FLSlice FLSliceResult_AsSlice(FLSliceResult sr)
Type-casts a FLSliceResult to FLSlice, since C doesn't know it's a subclass.
Definition: FLSlice.h:180
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:118
static FLSliceResult FLSliceResult_CreateWith(const void *bytes, size_t size)
Allocates an FLSliceResult, copying size bytes starting at buf.
Definition: FLSlice.h:159
A simple reference to a block of memory.
Definition: FLSlice.h:40
const void * buf
Definition: FLSlice.h:41
size_t size
Definition: FLSlice.h:42
A heap-allocated block of memory returned from an API call.
Definition: FLSlice.h:58
const void * buf
Definition: FLSlice.h:59
size_t size
Definition: FLSlice.h:60