Couchbase Lite C
Couchbase Lite C API
CBL_Compat.h
Go to the documentation of this file.
1//
2// CBL_Compat.h
3//
4// Copyright (c) 2018 Couchbase, Inc All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
18
19#pragma once
20
21
22#ifndef __has_feature
23 #define __has_feature(x) 0
24#endif
25#ifndef __has_attribute
26 #define __has_attribute(x) 0
27#endif
28#ifndef __has_extension
29 #define __has_extension(x) 0
30#endif
31
32
33#ifdef _MSC_VER
34 #include <sal.h>
35 #define CBLINLINE __forceinline
36 #define _cbl_nonnull _In_
37 #define _cbl_warn_unused _Check_return_
38#else
39 #define CBLINLINE inline
40 #define _cbl_warn_unused __attribute__((warn_unused_result))
41#endif
42
43// Macros for defining typed enumerations and option flags.
44// To define an enumeration whose values won't be combined:
45// typedef CBL_ENUM(baseIntType, name) { ... };
46// To define an enumeration of option flags that will be ORed together:
47// typedef CBL_OPTIONS(baseIntType, name) { ... };
48// These aren't just a convenience; they are required for Swift bindings.
49#if __has_attribute(enum_extensibility)
50#define __CBL_ENUM_ATTRIBUTES __attribute__((enum_extensibility(open)))
51#define __CBL_OPTIONS_ATTRIBUTES __attribute__((flag_enum,enum_extensibility(open)))
52#else
53#define __CBL_ENUM_ATTRIBUTES
54#define __CBL_OPTIONS_ATTRIBUTES
55#endif
56
57#if __APPLE__
58 #include <CoreFoundation/CFBase.h> /* for CF_ENUM and CF_OPTIONS macros */
59 #define CBL_ENUM CF_ENUM
60 #define CBL_OPTIONS CF_OPTIONS
61#elif DOXYGEN_PARSING
62 #define CBL_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
63 #define CBL_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
64#else
65 #if (__cplusplus && _MSC_VER) || (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
66 #define CBL_ENUM(_type, _name) int __CBL_ENUM_ ## _name; enum __CBL_ENUM_ATTRIBUTES _name : _type; typedef enum _name _name; enum _name : _type
67 #if (__cplusplus)
68 #define CBL_OPTIONS(_type, _name) _type _name; enum __CBL_OPTIONS_ATTRIBUTES : _type
69 #else
70 #define CBL_OPTIONS(_type, _name) int __CBL_OPTIONS_ ## _name; enum __CBL_OPTIONS_ATTRIBUTES _name : _type; typedef enum _name _name; enum _name : _type
71 #endif
72 #else
73 #define CBL_ENUM(_type, _name) _type _name; enum
74 #define CBL_OPTIONS(_type, _name) _type _name; enum
75 #endif
76#endif
77
78
79// Non-null annotations, for function parameters and struct fields.
80// In between CBL_ASSUME_NONNULL_BEGIN and CBL_ASSUME_NONNULL_END, all pointer declarations implicitly
81// disallow NULL values, unless annotated with _cbl_nullable (which must come after the `*`.)
82// (_cbl_nonnull is occasionally necessary when there are C arrays or multiple levels of pointers.)
83// NOTE: Only supported in Clang, so far.
84#if __has_feature(nullability)
85# define CBL_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
86# define CBL_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
87# define _cbl_nullable _Nullable
88# define _cbl_nonnull _Nonnull
89#else
90# define CBL_ASSUME_NONNULL_BEGIN
91# define CBL_ASSUME_NONNULL_END
92# define _cbl_nullable
93#ifndef _cbl_nonnull
94# define _cbl_nonnull
95#endif
96#endif
97
98
99#ifdef __cplusplus
100 #define CBLAPI noexcept
101 #define CBL_CAPI_BEGIN extern "C" { CBL_ASSUME_NONNULL_BEGIN
102 #define CBL_CAPI_END CBL_ASSUME_NONNULL_END }
103#else
104 #define CBLAPI
105 #define CBL_CAPI_BEGIN CBL_ASSUME_NONNULL_BEGIN
106 #define CBL_CAPI_END CBL_ASSUME_NONNULL_END
107#endif
108
109
110// On Windows, CBL_PUBLIC marks symbols as being exported from the shared library.
111// However, this is not the whole list of things that are exported. The API methods
112// are exported using a definition list, but it is not possible to correctly include
113// initialized global variables, so those need to be marked (both in the header and
114// implementation) with CBL_PUBLIC. See kCBLTypeProperty in CBLBlob.h and CBLBlob_CPI.cc
115// for an example.
116#ifdef _MSC_VER
117 #ifdef CBL_EXPORTS
118 #define CBL_PUBLIC __declspec(dllexport)
119 #else
120 #define CBL_PUBLIC __declspec(dllimport)
121 #endif
122#else // _MSC_VER
123 #define CBL_PUBLIC
124#endif
125
126// Type-checking for printf-style vararg functions:
127#ifdef _MSC_VER
128 #define __printflike(A, B)
129#else
130 #ifndef __printflike
131 #define __printflike(fmtarg, firstvararg) __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
132 #endif
133#endif
134