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// Declaration for API functions; should be just before the ending ";".
101#ifdef __cplusplus
102# define C4API noexcept
103# define C4API_BEGIN_DECLS extern "C" {
104# define C4API_END_DECLS }
105#else
106# define C4API
107# define C4API_BEGIN_DECLS
108# define C4API_END_DECLS
109#endif
110
111// Deprecating functions & types (Note: In C++only code, can use standard `[[deprecated]]`)
112#ifdef _MSC_VER
113# define C4_DEPRECATED(MSG) __declspec(deprecated(MSG))
114#else
115# define C4_DEPRECATED(MSG) __attribute((deprecated(MSG)))
116#endif
117
118// Export/import stuff.
119// `CBL_CORE_API` goes before an `extern` declaration,
120// `CBL_CORE_API_IMPL` goes before the definition.
121#ifdef _MSC_VER
122# ifdef LITECORE_EXPORTS
123# define CBL_CORE_API __declspec(dllexport)
124# elif !defined(LITECORE_STATIC)
125# define CBL_CORE_API __declspec(dllimport)
126# else
127# define CBL_CORE_API
128# endif
129# define CBL_CORE_API_IMPL CBL_CORE_API
130#else
131# define CBL_CORE_API __attribute__((visibility("default")))
132# ifdef __clang__
133# define CBL_CORE_API_IMPL CBL_CORE_API
134# else
135# define CBL_CORE_API_IMPL
136# endif
137#endif
138
139
140// Type-checking for printf-style vararg functions:
141#ifdef _MSC_VER
142# define __printflike(A, B)
143#else
144# ifndef __printflike
145# define __printflike(fmtarg, firstvararg) __attribute__((__format__(__printf__, fmtarg, firstvararg)))
146# endif
147#endif