| 1 | #ifndef GI_INC_HPP |
| 2 | #define GI_INC_HPP |
| 3 | |
| 4 | // gi preprocessor/macro interface |
| 5 | // aka global module fragment part |
| 6 | |
| 7 | #define GI_VERSION_MAJAOR (2) |
| 8 | #define GI_VERSION_MINOR (0) |
| 9 | #define GI_VERSION_MICRO (0) |
| 10 | |
| 11 | #ifdef GI_INLINE |
| 12 | #define GI_INLINE_DECL inline |
| 13 | #else |
| 14 | #define GI_INLINE_DECL |
| 15 | #endif |
| 16 | |
| 17 | // == we could be included in some module setting |
| 18 | #ifdef GI_MODULE_IN_INTERFACE |
| 19 | #if __cplusplus < 201703L |
| 20 | #error "need at least C++17 for modules" |
| 21 | #endif |
| 22 | #define GI_MODULE_EXPORT export |
| 23 | #define GI_MODULE_INLINE inline |
| 24 | #define GI_MODULE_STATIC_OR_INLINE inline |
| 25 | #else // GI_MODULE_IN_INTERFACE |
| 26 | #define GI_MODULE_EXPORT |
| 27 | #define GI_MODULE_INLINE |
| 28 | #define GI_MODULE_STATIC_OR_INLINE static |
| 29 | #endif |
| 30 | |
| 31 | // only used in module context |
| 32 | #ifdef GI_MODULE_EXTERN |
| 33 | #define GI_MODULE_EXTERN_BEGIN extern "C++" { |
| 34 | #define GI_MODULE_EXTERN_END } |
| 35 | #else |
| 36 | #define GI_MODULE_EXTERN_BEGIN |
| 37 | #define GI_MODULE_EXTERN_END |
| 38 | #endif |
| 39 | // related; another clang warning |
| 40 | // https://github.com/llvm/llvm-project/issues/68615 |
| 41 | // `#include <filename>' attaches the declarations to the named module` |
| 42 | #ifdef __clang__ |
| 43 | #define GI_MODULE_DISABLE_WARN_BEGIN \ |
| 44 | _Pragma("GCC diagnostic push") \ |
| 45 | _Pragma("GCC diagnostic ignored \"-Winclude-angled-in-module-purview\"") |
| 46 | #define GI_MODULE_DISABLE_WARN_END _Pragma("GCC diagnostic pop") |
| 47 | #else |
| 48 | #define GI_MODULE_DISABLE_WARN_BEGIN |
| 49 | #define GI_MODULE_DISABLE_WARN_END |
| 50 | #endif |
| 51 | |
| 52 | #define GI_MODULE_BEGIN \ |
| 53 | GI_MODULE_DISABLE_WARN_BEGIN \ |
| 54 | GI_MODULE_EXTERN_BEGIN |
| 55 | |
| 56 | #define GI_MODULE_END \ |
| 57 | GI_MODULE_EXTERN_END \ |
| 58 | GI_MODULE_DISABLE_WARN_BEGIN |
| 59 | // == |
| 60 | |
| 61 | // lots of declarations might be attributed as deprecated, |
| 62 | // but not so annotated, so let's avoid warning floods |
| 63 | // also handle complaints about const qualified casts |
| 64 | // (due to silly const qualified scalar parameters) |
| 65 | #define GI_DISABLE_DEPRECATED_WARN_BEGIN \ |
| 66 | _Pragma("GCC diagnostic push") \ |
| 67 | _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \ |
| 68 | _Pragma("GCC diagnostic push") \ |
| 69 | _Pragma("GCC diagnostic ignored \"-Wignored-qualifiers\"") |
| 70 | |
| 71 | #define GI_DISABLE_DEPRECATED_WARN_END \ |
| 72 | _Pragma("GCC diagnostic pop") _Pragma("GCC diagnostic pop") |
| 73 | |
| 74 | // typically clang might warn but gcc might complain about pragma clang ... |
| 75 | #ifdef GI_CLASS_IMPL_PRAGMA |
| 76 | #ifndef GI_CLASS_IMPL_BEGIN |
| 77 | #define GI_CLASS_IMPL_BEGIN \ |
| 78 | _Pragma("GCC diagnostic push") \ |
| 79 | _Pragma("GCC diagnostic ignored \"-Woverloaded-virtual\"") |
| 80 | #endif |
| 81 | |
| 82 | #ifndef GI_CLASS_IMPL_END |
| 83 | #define GI_CLASS_IMPL_END _Pragma("GCC diagnostic pop") |
| 84 | #endif |
| 85 | #else |
| 86 | #define GI_CLASS_IMPL_BEGIN |
| 87 | #define GI_CLASS_IMPL_END |
| 88 | #endif |
| 89 | |
| 90 | // attempt to auto-discover exception support: |
| 91 | #ifndef GI_CONFIG_EXCEPTIONS |
| 92 | #if defined(_MSC_VER) |
| 93 | #include <cstddef> // for _HAS_EXCEPTIONS |
| 94 | #endif |
| 95 | #if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS) |
| 96 | #define GI_CONFIG_EXCEPTIONS 1 |
| 97 | #else |
| 98 | #define GI_CONFIG_EXCEPTIONS 0 |
| 99 | #endif |
| 100 | #endif |
| 101 | |
| 102 | #include <cstddef> |
| 103 | |
| 104 | #include <exception> |
| 105 | #include <functional> |
| 106 | #include <limits> |
| 107 | #include <memory> |
| 108 | #include <string> |
| 109 | #include <tuple> |
| 110 | #include <type_traits> |
| 111 | #include <typeinfo> |
| 112 | #include <utility> |
| 113 | |
| 114 | #include <iterator> |
| 115 | #include <map> |
| 116 | #include <vector> |
| 117 | |
| 118 | #if __has_include("nonstd/expected.hpp") |
| 119 | #if __cpp_concepts >= 202002L |
| 120 | // this is also required in gcc's libstdc++ expected |
| 121 | #else |
| 122 | // so if that is missing, then prevent delegation to <expected> |
| 123 | #define nsel_CONFIG_SELECT_EXPECTED nsel_EXPECTED_NONSTD |
| 124 | #endif |
| 125 | #include "nonstd/expected.hpp" |
| 126 | #else |
| 127 | #include <expected> |
| 128 | #endif |
| 129 | |
| 130 | #if __cplusplus >= 201703L |
| 131 | #include <optional> |
| 132 | #include <string_view> |
| 133 | #endif |
| 134 | |
| 135 | // define << operator if not unwanted |
| 136 | #ifndef GI_NO_STRING_IOS |
| 137 | #include <iostream> |
| 138 | #endif |
| 139 | |
| 140 | #if GI_DL |
| 141 | #include <dlfcn.h> |
| 142 | #endif |
| 143 | |
| 144 | #include <assert.h> |
| 145 | #include <string.h> |
| 146 | |
| 147 | #include <glib-object.h> |
| 148 | #include <glib.h> |
| 149 | #include <gobject/gvaluecollector.h> |
| 150 | |
| 151 | // various macros from all over the place |
| 152 | // sadly, module refactor means breaking things apart |
| 153 | |
| 154 | // exception |
| 155 | // exception specification is generated according to settings and situation |
| 156 | // some derived code (e.g. overrides) may need to follow suit accordingly |
| 157 | #if GI_EXPECTED |
| 158 | // no exception if reported through expected |
| 159 | #define GI_NOEXCEPT_DECL(nonthrowing) noexcept |
| 160 | #elif GI_DL |
| 161 | // otherwise, everything can start failing if resolved at runtime |
| 162 | #define GI_NOEXCEPT_DECL(nonthrowing) |
| 163 | #else |
| 164 | // otherwise, depends on whether (wrapped) function is (GError) throwing |
| 165 | #define GI_NOEXCEPT_DECL(nonthrowing) noexcept(nonthrowing) |
| 166 | #endif |
| 167 | |
| 168 | // callback |
| 169 | #define GI_CB_ARG_CALLBACK_CUSTOM(Type, CF_CType, CF_handler) \ |
| 170 | struct Type \ |
| 171 | { \ |
| 172 | using handler_cb_type = CF_CType; \ |
| 173 | static constexpr auto handler = CF_handler; \ |
| 174 | } |
| 175 | |
| 176 | // boxed |
| 177 | // should be used within gi.repository namespace |
| 178 | #define GI_ENABLE_BOXED_COPY(CType) \ |
| 179 | template<> \ |
| 180 | struct enable_boxed_copy<CType> : public std::true_type \ |
| 181 | {}; |
| 182 | |
| 183 | // enumflag |
| 184 | #define GI_FLAG_OPERATORS(FlagType) \ |
| 185 | inline FlagType operator|(FlagType lhs, FlagType rhs) \ |
| 186 | { \ |
| 187 | return static_cast<FlagType>( \ |
| 188 | static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); \ |
| 189 | } \ |
| 190 | \ |
| 191 | inline FlagType operator&(FlagType lhs, FlagType rhs) \ |
| 192 | { \ |
| 193 | return static_cast<FlagType>( \ |
| 194 | static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); \ |
| 195 | } \ |
| 196 | \ |
| 197 | inline FlagType operator^(FlagType lhs, FlagType rhs) \ |
| 198 | { \ |
| 199 | return static_cast<FlagType>( \ |
| 200 | static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); \ |
| 201 | } \ |
| 202 | \ |
| 203 | inline FlagType operator~(FlagType flags) \ |
| 204 | { \ |
| 205 | return static_cast<FlagType>(~static_cast<unsigned>(flags)); \ |
| 206 | } \ |
| 207 | \ |
| 208 | inline FlagType &operator|=(FlagType &lhs, FlagType rhs) \ |
| 209 | { \ |
| 210 | return (lhs = static_cast<FlagType>( \ |
| 211 | static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs))); \ |
| 212 | } \ |
| 213 | \ |
| 214 | inline FlagType &operator&=(FlagType &lhs, FlagType rhs) \ |
| 215 | { \ |
| 216 | return (lhs = static_cast<FlagType>( \ |
| 217 | static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs))); \ |
| 218 | } \ |
| 219 | \ |
| 220 | inline FlagType &operator^=(FlagType &lhs, FlagType rhs) \ |
| 221 | { \ |
| 222 | return (lhs = static_cast<FlagType>( \ |
| 223 | static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs))); \ |
| 224 | } |
| 225 | |
| 226 | #define GI_ENUM_NUMERIC(EnumType) \ |
| 227 | inline std::underlying_type<EnumType>::type operator+(EnumType e) \ |
| 228 | { \ |
| 229 | return static_cast<std::underlying_type<EnumType>::type>(e); \ |
| 230 | } |
| 231 | |
| 232 | // objectclass |
| 233 | // helper macro to obtain data from factory (if provided) |
| 234 | #define GI_MEMBER_INIT_DATA(ClassType, factory) \ |
| 235 | (factory ? ((ClassType(*)())(factory))() : ClassType()); |
| 236 | |
| 237 | // conflicts might arise between interfaces and/or class |
| 238 | // generate some dummy check types to force failure |
| 239 | #define GI_MEMBER_CHECK_CONFLICT(member) _check_member_conflict_##member |
| 240 | |
| 241 | // generated code tries to detect a defined member in SubClass as follows |
| 242 | #define GI_MEMBER_DEFAULT_HAS_DEFINITION(BaseDef, member) \ |
| 243 | template<typename SubClass> \ |
| 244 | constexpr static bool has_definition(const member##_t *, const SubClass *) \ |
| 245 | { \ |
| 246 | /* the use of conflict type check is only to trigger a compiler error \ |
| 247 | * if there is such a conflict \ |
| 248 | * in that case, manual specification of definitions are needed \ |
| 249 | * (which will then avoid this code path instantiation) \ |
| 250 | * (type should never be void, so merely serves as dummy check) \ |
| 251 | */ \ |
| 252 | return std::is_void<typename SubClass::GI_MEMBER_CHECK_CONFLICT( \ |
| 253 | member)>::value || \ |
| 254 | !std::is_same<decltype(&BaseDef::member##_), \ |
| 255 | decltype(&SubClass::member##_)>::value; \ |
| 256 | } |
| 257 | |
| 258 | // helper macro used in generated code |
| 259 | #define GI_MEMBER_DEFINE(BaseDef, member) \ |
| 260 | struct member##_tag; \ |
| 261 | using member##_t = detail::member_type<member##_tag>; \ |
| 262 | member##_t member; \ |
| 263 | GI_MEMBER_DEFAULT_HAS_DEFINITION(BaseDef, member) |
| 264 | |
| 265 | // the automated way might/will fail in case of overload resolution failure |
| 266 | // (due to member conflicts with interfaces) |
| 267 | // so the following can be used to specify definition situation |
| 268 | // should be used in an inner struct DefinitionData in the SubClass |
| 269 | #define GI_DEFINES_MEMBER(BaseDef, member, _defines) \ |
| 270 | template<typename SubClass> \ |
| 271 | constexpr static bool defines( \ |
| 272 | const BaseDef::TypeInitData::member##_t *, const SubClass *) \ |
| 273 | { \ |
| 274 | return _defines; \ |
| 275 | } |
| 276 | |
| 277 | // uses function overload on all of the above to determine |
| 278 | // if member of DefData is defined/overridden in SubClass |
| 279 | // (and should then be registered in the class/interface struct) |
| 280 | #define GI_MEMBER_HAS_DEFINITION(SubClass, DefData, member) \ |
| 281 | DefData::defines((member##_t *)(nullptr), (SubClass *)(nullptr)) |
| 282 | |
| 283 | #endif // GI_INC_HPP |