Skip to content

Commit dc11e66

Browse files
committed
Avoid conflicts of embedded vfs with Qt.
1 parent d267253 commit dc11e66

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

Include/np_embed.h

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#ifndef __ASSEMBLER__
1010
#ifdef __cplusplus
11+
#if __has_include (<QtCore>)
12+
# include <QtCore>
13+
#endif
14+
1115
extern "C" {
1216
#endif
1317

@@ -129,13 +133,76 @@ NP_DECL(void) np_flockfile(void *e);
129133
NP_DECL(void) np_funlockfile(void *e);
130134
NP_DECL(int) np_ftrylockfile(void *e);
131135

132-
#if !defined(NUITKAPYTHON_EMBED_BUILD) && !defined(__cplusplus)
136+
#ifndef NUITKAPYTHON_EMBED_BUILD
137+
138+
/* Using a bunch of macros from
139+
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
140+
*/
141+
142+
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
143+
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
144+
145+
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
146+
#define IIF_0(t, ...) __VA_ARGS__
147+
#define IIF_1(t, ...) t
148+
149+
#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
150+
#define COMPL_0 1
151+
#define COMPL_1 0
152+
153+
#define DEC(x) PRIMITIVE_CAT(DEC_, x)
154+
#define DEC_0 0
155+
#define DEC_1 0
156+
#define DEC_2 1
157+
#define DEC_3 2
158+
#define DEC_4 3
159+
#define DEC_5 4
160+
#define DEC_6 5
161+
#define DEC_7 6
162+
#define DEC_8 7
163+
#define DEC_9 8
164+
#define DEC_10 9
165+
#define DEC_11 10
166+
#define DEC_12 11
167+
#define DEC_13 12
168+
#define DEC_14 13
169+
#define DEC_15 14
170+
#define DEC_16 15
171+
#define DEC_17 16
172+
#define DEC_18 17
173+
#define DEC_19 18
174+
#define DEC_20 19
175+
176+
#define CHECK_N(x, n, ...) n
177+
#define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
178+
#define PROBE(x) x, 1,
179+
180+
#define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
181+
#define IS_PAREN_PROBE(...) PROBE(~)
182+
183+
#define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
184+
#define NOT_0 PROBE(~)
185+
186+
#define BOOL(x) COMPL(NOT(x))
187+
#define IF(c) IIF(BOOL(c))
188+
189+
#define EAT(...)
190+
#define EXPAND(...) __VA_ARGS__
191+
#define WHEN(c) IF(c)(EXPAND, EAT)
192+
193+
#define EXPAND(...) __VA_ARGS__
194+
195+
#define NUM_ARGS1(_20,_19,_18,_17,_16,_15,_14,_13,_12,_11,_10,_9,_8,_7,_6,_5,_4,_3,_2,_1, n, ...) n
196+
#define NUM_ARGS0(...) NUM_ARGS1(__VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
197+
#define NUM_ARGS(...) IF(DEC(NUM_ARGS0(__VA_ARGS__)))(NUM_ARGS0(__VA_ARGS__),IF(IS_PAREN(__VA_ARGS__ ()))(0,1))
198+
133199
// Preprocessor Translation
134200
#define FILE EFILE
135201
/* File Opening and Closing */
136202
#define fopen np_fopen
137203
#define _fopen np_fopen
138-
#define open np_open
204+
// Basically this very complicated macro overrides only calls to open() functions that have 2 or more arguments to avoid compatibility issues.
205+
#define open(...) WHEN(DEC(NUM_ARGS( __VA_ARGS__ )))(np_open(__VA_ARGS__))WHEN(NOT(DEC(NUM_ARGS( __VA_ARGS__ ))))(open(__VA_ARGS__))
139206
#define _open np_open
140207
#define fdopen np_fdopen
141208
#define _fdopen np_fdopen

0 commit comments

Comments
 (0)