idlc 1.5.14
Interface Definition Language Compiler
Loading...
Searching...
No Matches
idl-platform.h
Go to the documentation of this file.
1/**
2 * @file idl-platform.h
3 * @brief Platform-specific definitions and utilities.
4 * @details This header provides cross-platform macros, type definitions, and utility
5 * macros for the Idl library. It handles:
6 * - Platform detection (Windows, macOS, iOS, Android, Linux, Web)
7 * - Symbol visibility control (DLL import/export on Windows)
8 * - C/C++ interoperability
9 * - Type definitions for consistent data sizes across platforms
10 * - Bit flag operations for enumerations (C++ only).
11 *
12 * @author Vladimir Shaleev <vladimirshaleev@gmail.com>
13 * @ingroup files
14 * @copyright MIT License
15 */
16#ifndef IDL_PLATFORM_H
17#define IDL_PLATFORM_H
18
19/**
20 * @def IDL_BEGIN
21 * @brief Begins a C-linkage declaration block.
22 * @details In C++, expands to `extern "C" {` to ensure C-compatible symbol naming.
23 * In pure C environments, expands to nothing.
24 * @sa IDL_END
25 * @ingroup macros
26 *
27 */
28
29/**
30 * @def IDL_END
31 * @brief Ends a C-linkage declaration block.
32 * @details Closes the scope opened by #IDL_BEGIN.
33 * @sa IDL_BEGIN
34 * @ingroup macros
35 *
36 */
37
38#ifdef __cplusplus
39# define IDL_BEGIN extern "C" {
40# define IDL_END }
41#else
42# define IDL_BEGIN
43# define IDL_END
44#endif
45
46/**
47 * @def idl_api
48 * @brief Controls symbol visibility for shared library builds.
49 * @details This macro is used to control symbol visibility when building or using the library.
50 * On Windows (**MSVC**) with dynamic linking (non-static build), it expands to `__declspec(dllimport)`.
51 * In all other cases (static builds or non-Windows platforms), it expands to nothing.
52 * This allows proper importing of symbols from DLLs on Windows platforms.
53 * @note Define `IDL_STATIC_BUILD` for static library configuration.
54 * @ingroup macros
55 */
56
57#ifndef idl_api
58# if defined(_MSC_VER) && !defined(IDL_STATIC_BUILD)
59# define idl_api __declspec(dllimport)
60# else
61# define idl_api
62# endif
63#endif
64
65#if defined(_WIN32) && !defined(IDL_PLATFORM_WINDOWS)
66# define IDL_PLATFORM_WINDOWS
67#elif defined(__APPLE__)
68# include <TargetConditionals.h>
69# include <unistd.h>
70# if TARGET_OS_IPHONE && !defined(IDL_PLATFORM_IOS)
71# define IDL_PLATFORM_IOS
72# elif TARGET_IPHONE_SIMULATOR && !defined(IDL_PLATFORM_IOS)
73# define IDL_PLATFORM_IOS
74# elif TARGET_OS_MAC && !defined(IDL_PLATFORM_MAC_OS)
75# define IDL_PLATFORM_MAC_OS
76# else
77# error unsupported Apple platform
78# endif
79#elif defined(__ANDROID__) && !defined(IDL_PLATFORM_ANDROID)
80# define IDL_PLATFORM_ANDROID
81#elif defined(__linux__) && !defined(IDL_PLATFORM_LINUX)
82# define IDL_PLATFORM_LINUX
83#elif defined(__EMSCRIPTEN__) && !defined(IDL_PLATFORM_WEB)
84# define IDL_PLATFORM_WEB
85#else
86# error unsupported platform
87#endif
88
89#ifdef __cpp_constexpr
90# define IDL_CONSTEXPR constexpr
91# if __cpp_constexpr >= 201304L
92# define IDL_CONSTEXPR_14 constexpr
93# else
94# define IDL_CONSTEXPR_14
95# endif
96#else
97# define IDL_CONSTEXPR
98# define IDL_CONSTEXPR_14
99#endif
100
101/**
102 * @addtogroup types Types
103 * @{
104 */
105
106/**
107 * @name Platform-independent type definitions.
108 * @brief Fixed-size types guaranteed to work across all supported platforms.
109 * @{
110 */
111#include <stdint.h>
112typedef char idl_char_t; /**< symbol type. */
113typedef int32_t idl_bool_t; /**< boolean type. */
114typedef int8_t idl_sint8_t; /**< 8 bit signed integer. */
115typedef uint8_t idl_uint8_t; /**< 8 bit unsigned integer. */
116typedef int16_t idl_sint16_t; /**< 16 bit signed integer. */
117typedef uint16_t idl_uint16_t; /**< 16 bit unsigned integer. */
118typedef int32_t idl_sint32_t; /**< 32 bit signed integer. */
119typedef uint32_t idl_uint32_t; /**< 32 bit unsigned integer. */
120typedef int64_t idl_sint64_t; /**< 64 bit signed integer. */
121typedef uint64_t idl_uint64_t; /**< 64 bit unsigned integer. */
122typedef float idl_float32_t; /**< 32 bit float point. */
123typedef double idl_float64_t; /**< 64 bit float point. */
124typedef const char* idl_utf8_t; /**< utf8 string. */
125typedef void* idl_data_t; /**< pointer to data. */
126typedef const void* idl_cdata_t; /**< pointer to immutable data. */
127/** @} */
128
129/** @} */
130
131/**
132 * @def IDL_FLAGS
133 * @brief Enables bit flag operations for enumerations (C++ only).
134 * @details Generates overloaded bitwise operators for type-safe flag manipulation:
135 * - Bitwise NOT (~)
136 * - OR (|, |=)
137 * - AND (&, &=)
138 * - XOR (^, ^=)
139 *
140 * @param[in] idl_enum_t Enumeration type to enhance with flag operations.
141 * @note Only active in C++ mode. In C, expands to nothing.
142 * @ingroup macros
143 */
144
145#ifdef __cplusplus
146# define IDL_FLAGS(idl_enum_t) extern
147 "C++" { inline
148 IDL_CONSTEXPR idl_enum_t operator~(idl_enum_t lhr) noexcept {
149 return static_cast<idl_enum_t>(~static_cast<idl_sint32_t>(lhr)); \
150}inline
151 IDL_CONSTEXPR idl_enum_t operator|(idl_enum_t lhr, idl_enum_t rhs) noexcept {
152 return static_cast<idl_enum_t>(static_cast<idl_sint32_t>(lhr) | static_cast<idl_sint32_t>(rhs)); \
153}inline
154 IDL_CONSTEXPR idl_enum_t operator&(idl_enum_t lhr, idl_enum_t rhs) noexcept {
155 return static_cast<idl_enum_t>(static_cast<idl_sint32_t>(lhr) & static_cast<idl_sint32_t>(rhs)); \
156}inline
157 IDL_CONSTEXPR idl_enum_t operator^(idl_enum_t lhr, idl_enum_t rhs) noexcept {
158 return static_cast<idl_enum_t>(static_cast<idl_sint32_t>(lhr) ^ static_cast<idl_sint32_t>(rhs)); \
159}inline
160 IDL_CONSTEXPR_14 idl_enum_t& operator|=(idl_enum_t& lhr, idl_enum_t rhs) noexcept {
161 return lhr = lhr | rhs; \
162}inline
163 IDL_CONSTEXPR_14 idl_enum_t& operator&=(idl_enum_t& lhr, idl_enum_t rhs) noexcept {
164 return lhr = lhr & rhs; \
165}inline
166 IDL_CONSTEXPR_14 idl_enum_t& operator^=(idl_enum_t& lhr, idl_enum_t rhs) noexcept {
167 return lhr = lhr ^ rhs; \
168}\
169}
170#else
171# define IDL_FLAGS(idl_enum_t)
172#endif
173
174/**
175 * @def IDL_TYPE
176 * @brief Declares an opaque handle type.
177 * @details Creates a typedef for a pointer to an incomplete struct type,
178 * providing type safety while hiding implementation details.
179 * @param[in] idl_name Base name for the type (suffix `_t` will be added).
180 * @ingroup macros
181 */
182#define IDL_TYPE(idl_name) typedef
183 struct _##idl_name* idl_name##_t;
184
185#endif /* IDL_PLATFORM_H */
const void * idl_cdata_t
pointer to immutable data.
Definition idl-platform.h:126
float idl_float32_t
32 bit float point.
Definition idl-platform.h:122
void * idl_data_t
pointer to data.
Definition idl-platform.h:125
int32_t idl_sint32_t
32 bit signed integer.
Definition idl-platform.h:118
int8_t idl_sint8_t
8 bit signed integer.
Definition idl-platform.h:114
int32_t idl_bool_t
boolean type.
Definition idl-platform.h:113
const char * idl_utf8_t
utf8 string.
Definition idl-platform.h:124
double idl_float64_t
64 bit float point.
Definition idl-platform.h:123
int16_t idl_sint16_t
16 bit signed integer.
Definition idl-platform.h:116
int64_t idl_sint64_t
64 bit signed integer.
Definition idl-platform.h:120
uint64_t idl_uint64_t
64 bit unsigned integer.
Definition idl-platform.h:121
uint16_t idl_uint16_t
16 bit unsigned integer.
Definition idl-platform.h:117
char idl_char_t
symbol type.
Definition idl-platform.h:112
uint32_t idl_uint32_t
32 bit unsigned integer.
Definition idl-platform.h:119
uint8_t idl_uint8_t
8 bit unsigned integer.
Definition idl-platform.h:115
#define IDL_CONSTEXPR
Definition idl-platform.h:97
#define IDL_CONSTEXPR_14
Definition idl-platform.h:98