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 #define _cbl_deprecated
39#else
40 #define CBLINLINE inline
41 #define _cbl_warn_unused __attribute__((warn_unused_result))
42 #define _cbl_deprecated __attribute__((deprecated()))
43#endif
44
45// Macros for defining typed enumerations and option flags.
46// To define an enumeration whose values won't be combined:
47// typedef CBL_ENUM(baseIntType, name) { ... };
48// To define an enumeration of option flags that will be ORed together:
49// typedef CBL_OPTIONS(baseIntType, name) { ... };
50// These aren't just a convenience; they are required for Swift bindings.
51#if __APPLE__
52 #include <CoreFoundation/CFBase.h> /* for CF_ENUM and CF_OPTIONS macros */
53 #define CBL_ENUM CF_ENUM
54 #define CBL_OPTIONS CF_OPTIONS
55#elif DOXYGEN_PARSING
56 #define CBL_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
57 #define CBL_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
58#else
59 #if (__cplusplus && _MSC_VER) || (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
60 #define CBL_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
61 #if (__cplusplus)
62 #define CBL_OPTIONS(_type, _name) _type _name; enum : _type
63 #else
64 #define CBL_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
65 #endif
66 #else
67 #define CBL_ENUM(_type, _name) _type _name; enum
68 #define CBL_OPTIONS(_type, _name) _type _name; enum
69 #endif
70#endif
71
72
73// Non-null annotations, for function parameters and struct fields.
74// In between CBL_ASSUME_NONNULL_BEGIN and CBL_ASSUME_NONNULL_END, all pointer declarations implicitly
75// disallow NULL values, unless annotated with _cbl_nullable (which must come after the `*`.)
76// (_cbl_nonnull is occasionally necessary when there are C arrays or multiple levels of pointers.)
77// NOTE: Only supported in Clang, so far.
78#if __has_feature(nullability)
79# define CBL_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
80# define CBL_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
81# define _cbl_nullable _Nullable
82# define _cbl_nonnull _Nonnull
83#else
84# define CBL_ASSUME_NONNULL_BEGIN
85# define CBL_ASSUME_NONNULL_END
86# define _cbl_nullable
87#ifndef _cbl_nonnull
88# define _cbl_nonnull
89#endif
90#endif
91
92
93#ifdef __cplusplus
94 #define CBLAPI noexcept
95 #define CBL_CAPI_BEGIN extern "C" { CBL_ASSUME_NONNULL_BEGIN
96 #define CBL_CAPI_END CBL_ASSUME_NONNULL_END }
97#else
98 #define CBLAPI
99 #define CBL_CAPI_BEGIN CBL_ASSUME_NONNULL_BEGIN
100 #define CBL_CAPI_END CBL_ASSUME_NONNULL_END
101#endif
102
103
104// On Windows, CBL_PUBLIC marks symbols as being exported from the shared library.
105// However, this is not the whole list of things that are exported. The API methods
106// are exported using a definition list, but it is not possible to correctly include
107// initialized global variables, so those need to be marked (both in the header and
108// implementation) with CBL_PUBLIC. See kCBLTypeProperty in CBLBlob.h and CBLBlob_CPI.cc
109// for an example.
110#ifdef _MSC_VER
111 #ifdef CBL_EXPORTS
112 #define CBL_PUBLIC __declspec(dllexport)
113 #else
114 #define CBL_PUBLIC __declspec(dllimport)
115 #endif
116#else // _MSC_VER
117 #define CBL_PUBLIC
118#endif
119
120// Type-checking for printf-style vararg functions:
121#ifdef _MSC_VER
122 #define __printflike(A, B)
123#else
124 #ifndef __printflike
125 #define __printflike(fmtarg, firstvararg) __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
126 #endif
127#endif
128