LiteCore
Couchbase Lite cross-platform core implementation
Loading...
Searching...
No Matches
c4Compat.h
Go to the documentation of this file.
1//
2// c4Compat.h
3//
4// Copyright 2018-Present Couchbase, Inc.
5//
6// Use of this software is governed by the Business Source License included
7// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
8// in that file, in accordance with the Business Source License, use of this
9// software will be governed by the Apache License, Version 2.0, included in
10// the file licenses/APL2.txt.
11//
12
13#pragma once
15
16#ifdef _MSC_VER
17# define C4INLINE __forceinline
18#else
19# define C4INLINE inline
20#endif
21
22// Non-null annotations, for function parameters and struct fields.
23// In between C4_ASSUME_NONNULL_BEGIN and C4_ASSUME_NONNULL_END, all pointer declarations implicitly
24// disallow NULL values, unless annotated with C4NULLABLE (which must come after the `*`.)
25// (C4NONNULL is occasionally necessary when there are multiple levels of pointers.)
26// NOTE: Does not apply to function return values, for some reason. Those may still be null,
27// unless annotated with C4_RETURNS_NONNULL.
28// NOTE: Only supported in Clang, so far.
29#if __has_feature(nullability)
30# define C4_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
31# define C4_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
32# define C4NULLABLE _Nullable
33# define C4NONNULL _Nonnull
34# define C4_RETURNS_NONNULL __attribute__((returns_nonnull))
35#else
36# define C4_ASSUME_NONNULL_BEGIN
37# define C4_ASSUME_NONNULL_END
38# define C4NULLABLE
39# define C4NONNULL
40# define C4_RETURNS_NONNULL
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 C4_ENUM(baseIntType, name) { ... };
46// To define an enumeration of option flags that will be ORed together:
47// typedef C4_OPTIONS(baseIntType, name) { ... };
48// These aren't just a convenience; they are required for Swift bindings.
49#if __has_attribute(enum_extensibility)
50# define C4_ENUM_ATTRIBUTES __attribute__((enum_extensibility(open)))
51# define C4_OPTIONS_ATTRIBUTES __attribute__((flag_enum, enum_extensibility(open)))
52#else
53# define C4_ENUM_ATTRIBUTES
54# define C4_OPTIONS_ATTRIBUTES
55#endif
56
57#if __APPLE__
58# include <CoreFoundation/CFBase.h> /* for CF_ENUM and CF_OPTIONS macros */
59# define C4_ENUM CF_ENUM
60# define C4_OPTIONS CF_OPTIONS
61#elif DOXYGEN_PARSING
62# define C4_ENUM(_type, _name) \
63 enum _name : _type _name; \
64 enum _name : _type
65# define C4_OPTIONS(_type, _name) \
66 enum _name : _type _name; \
67 enum _name : _type
68#else
69# if ( __cplusplus && _MSC_VER ) \
70 || (__cplusplus && __cplusplus >= 201103L \
71 && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) \
72 || (!__cplusplus && __has_feature(objc_fixed_enum))
73# define C4_ENUM(_type, _name) \
74 int __C4_ENUM_##_name; \
75 enum C4_ENUM_ATTRIBUTES _name : _type; \
76 typedef enum _name _name; \
77 enum _name : _type
78# if ( __cplusplus )
79# define C4_OPTIONS(_type, _name) \
80 _type _name; \
81 enum C4_OPTIONS_ATTRIBUTES : _type
82# else
83# define C4_OPTIONS(_type, _name) \
84 int __C4_OPTIONS_##_name; \
85 enum C4_OPTIONS_ATTRIBUTES _name : _type; \
86 typedef enum _name _name; \
87 enum _name : _type
88# endif
89# else
90# define C4_ENUM(_type, _name) \
91 _type _name; \
92 enum
93# define C4_OPTIONS(_type, _name) \
94 _type _name; \
95 enum
96# endif
97#endif
98
99
100// Suppresses warnings if an entity is unused.
101#if __has_attribute(unused)
102# define C4UNUSED __attribute__((unused))
103#else
104# define C4UNUSED
105#endif
106
107
108// Declaration for API functions; should be just before the ending ";".
109#ifdef __cplusplus
110# define C4API noexcept
111# define C4API_BEGIN_DECLS extern "C" {
112# define C4API_END_DECLS }
113#else
114# define C4API
115# define C4API_BEGIN_DECLS
116# define C4API_END_DECLS
117#endif
118
119// Deprecating functions & types (Note: In C++only code, can use standard `[[deprecated]]`)
120#ifdef _MSC_VER
121# define C4_DEPRECATED(MSG) __declspec(deprecated(MSG))
122#else
123# define C4_DEPRECATED(MSG) __attribute((deprecated(MSG)))
124#endif
125
126// Export/import stuff:
127#ifdef _MSC_VER
128# ifdef LITECORE_EXPORTS
129# define CBL_CORE_API __declspec(dllexport)
130# else
131# define CBL_CORE_API __declspec(dllimport)
132# endif
133#else
134# define CBL_CORE_API __attribute__((visibility("default")))
135#endif
136
137
138// Type-checking for printf-style vararg functions:
139#ifdef _MSC_VER
140# define __printflike(A, B)
141#else
142# ifndef __printflike
143# define __printflike(fmtarg, firstvararg) __attribute__((__format__(__printf__, fmtarg, firstvararg)))
144# endif
145#endif