FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
export.hpp
Go to the documentation of this file.
1// Copyright 2026 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef FTXUI_UTIL_EXPORT_H_
6#define FTXUI_UTIL_EXPORT_H_
7
8// In an amalgamated build, we don't want any export/import annotations.
9#if defined(FTXUI_AMALGAMATED)
10#define FTXUI_EXPORT(component)
11#define INSIDE_FTXUI_COMPONENT_IMPL(component) 0
12#else
13
14// Used to annotate symbols which are exported by the component named
15// |component|. Note that this only does the right thing if the corresponding
16// component target's sources are compiled with |IS_FTXUI_$component_IMPL|
17// defined as 1. For example:
18//
19// class FTXUI_EXPORT(FOO) Bar {};
20//
21// If IS_FTXUI_FOO_IMPL=1 at compile time, then Bar will be annotated using the
22// FTXUI_EXPORT_ANNOTATION macro defined below. Otherwise it will be
23// annotated using the FTXUI_IMPORT_ANNOTATION macro.
24#define FTXUI_EXPORT(component) \
25 FTXUI_MACRO_CONDITIONAL_(IS_FTXUI_##component##_IMPL, \
26 FTXUI_EXPORT_ANNOTATION, FTXUI_IMPORT_ANNOTATION)
27
28// Indicates whether the current compilation unit is being compiled as part of
29// the implementation of the component named |component|. Expands to |1| if
30// |IS_FTXUI_$component_IMPL| is defined as |1|; expands to |0| otherwise.
31//
32// Note in particular that if |IS_FTXUI_$component_IMPL| is not defined at all,
33// it is still fine to test INSIDE_FTXUI_COMPONENT_IMPL(component), which
34// expands to |0| as expected.
35#define INSIDE_FTXUI_COMPONENT_IMPL(component) \
36 FTXUI_MACRO_CONDITIONAL_(IS_FTXUI_##component##_IMPL, 1, 0)
37
38#endif
39
40// Compiler-specific macros to annotate for export or import of a symbol. No-op
41// in non-component builds. These should not see much if any direct use.
42// Instead use the FTXUI_EXPORT macro defined above.
43#if defined(COMPONENT_BUILD)
44#if defined(WIN32)
45#define FTXUI_EXPORT_ANNOTATION __declspec(dllexport)
46#define FTXUI_IMPORT_ANNOTATION __declspec(dllimport)
47#else // defined(WIN32)
48#define FTXUI_EXPORT_ANNOTATION __attribute__((visibility("default")))
49#define FTXUI_IMPORT_ANNOTATION __attribute__((visibility("default")))
50#endif // defined(WIN32)
51#else // defined(COMPONENT_BUILD)
52#define FTXUI_EXPORT_ANNOTATION
53#define FTXUI_IMPORT_ANNOTATION
54#endif // defined(COMPONENT_BUILD)
55
56// Below this point are several internal utility macros used for the
57// implementation of the above macros. Not intended for external use.
58
59#define FTXUI_MACRO_EXPAND(x) x
60
61// Helper for conditional expansion to one of two token strings. If |condition|
62// expands to |1| then this macro expands to |consequent|; otherwise it expands
63// to |alternate|.
64#define FTXUI_MACRO_CONDITIONAL_(condition, consequent, alternate) \
65 FTXUI_MACRO_EXPAND(FTXUI_MACRO_SELECT_THIRD_ARGUMENT_( \
66 FTXUI_MACRO_CONDITIONAL_COMMA_(condition), consequent, alternate))
67
68// Expands to a comma (,) iff its first argument expands to |1|. Used in
69// conjunction with |FTXUI_MACRO_SELECT_THIRD_ARGUMENT_()|, as the presence
70// or absense of an extra comma can be used to conditionally shift subsequent
71// argument positions and thus influence which argument is selected.
72#define FTXUI_MACRO_CONDITIONAL_COMMA_(...) \
73 FTXUI_MACRO_EXPAND(FTXUI_MACRO_CONDITIONAL_COMMA_IMPL_(__VA_ARGS__, dummy))
74#define FTXUI_MACRO_CONDITIONAL_COMMA_IMPL_(x, ...) \
75 FTXUI_MACRO_CONDITIONAL_COMMA_##x##_
76#define FTXUI_MACRO_CONDITIONAL_COMMA_1_ ,
77
78// Helper which simply selects its third argument. Used in conjunction with
79// |FTXUI_MACRO_CONDITIONAL_COMMA_()| above to implement conditional macro
80// expansion.
81#define FTXUI_MACRO_SELECT_THIRD_ARGUMENT_(...) \
82 FTXUI_MACRO_EXPAND( \
83 FTXUI_MACRO_SELECT_THIRD_ARGUMENT_IMPL_(__VA_ARGS__, dummy))
84#define FTXUI_MACRO_SELECT_THIRD_ARGUMENT_IMPL_(a, b, c, ...) c
85
86#endif // FTXUI_UTIL_EXPORT_H_