1/*
2 SPDX-FileCopyrightText: 2011 Will Stephenson <wstephenson@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#ifndef NETWORKMANAGERQT_MACROS_H
8#define NETWORKMANAGERQT_MACROS_H
9
10#define NM_GLOBAL_STATIC_STRUCT_NAME(NAME)
11typedef void (*NmCleanUpFunction)();
12class NmCleanUpGlobalStatic
13{
14public:
15 NmCleanUpFunction func;
16
17 inline ~NmCleanUpGlobalStatic()
18 {
19 func();
20 }
21};
22
23#define NM_GLOBAL_STATIC(TYPE, NAME) NM_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
24
25/* clang-format off */
26#define NM_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
27 static QBasicAtomicPointer<TYPE > _nm_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
28 static bool _nm_static_##NAME##_destroyed; \
29 static struct NM_GLOBAL_STATIC_STRUCT_NAME(NAME) \
30 { \
31 inline bool isDestroyed() const \
32 { \
33 return _nm_static_##NAME##_destroyed; \
34 } \
35 inline bool exists() const \
36 { \
37 return _nm_static_##NAME != 0; \
38 } \
39 inline operator TYPE*() \
40 { \
41 return operator->(); \
42 } \
43 inline TYPE *operator->() \
44 { \
45 if (!_nm_static_##NAME) { \
46 if (isDestroyed()) { \
47 qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
48 "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__); \
49 } \
50 TYPE *x = new TYPE ARGS; \
51 if (!_nm_static_##NAME.testAndSetOrdered(0, x) \
52 && _nm_static_##NAME != x ) { \
53 delete x; \
54 } else { \
55 static NmCleanUpGlobalStatic cleanUpObject = { destroy }; \
56 } \
57 } \
58 return _nm_static_##NAME; \
59 } \
60 inline TYPE &operator*() \
61 { \
62 return *operator->(); \
63 } \
64 static void destroy() \
65 { \
66 _nm_static_##NAME##_destroyed = true; \
67 TYPE *x = _nm_static_##NAME; \
68 _nm_static_##NAME = 0; \
69 delete x; \
70 } \
71 } NAME;
72/* clang-format on */
73
74#endif
75

source code of networkmanager-qt/src/macros.h