Instrument Neutral Distributed Interface INDI  1.9.2
indimacros.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 by Pawel Soja <kernel32.pl@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #pragma once
20 
29 #ifndef INDI_UNUSED
30 # define INDI_UNUSED(x) (void)x
31 #endif
32 
36 #ifndef INDI_HAS_CPP_ATTRIBUTE
37 # ifdef __has_cpp_attribute
38 # define INDI_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
39 # else
40 # define INDI_HAS_CPP_ATTRIBUTE(x) 0
41 # endif
42 #endif
43 
47 #ifndef INDI_HAS_ATTRIBUTE
48 # ifdef __has_attribute
49 # define INDI_HAS_ATTRIBUTE(x) __has_attribute(x)
50 # else
51 # define INDI_HAS_ATTRIBUTE(x) 0
52 # endif
53 #endif
54 
76 #ifndef INDI_FALLTHROUGH
77 # if defined(__cplusplus)
78 # if INDI_HAS_CPP_ATTRIBUTE(clang::fallthrough)
79 # define INDI_FALLTHROUGH [[clang::fallthrough]]
80 # elif INDI_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
81 # define INDI_FALLTHROUGH [[gnu::fallthrough]]
82 # elif INDI_HAS_CPP_ATTRIBUTE(fallthrough)
83 # define INDI_FALLTHROUGH [[fallthrough]]
84 # endif
85 # else
86 # if INDI_HAS_ATTRIBUTE(fallthrough)
87 # define INDI_FALLTHROUGH __attribute__((fallthrough))
88 # else
89 # define INDI_FALLTHROUGH do {} while (0)
90 # endif
91 # endif
92 #endif
93 
101 #if defined(__cplusplus)
102 
103 template <typename T>
104 static inline T *getPtrHelper(T *ptr) { return ptr; }
105 
106 template <typename Wrapper>
107 static inline typename Wrapper::element_type *getPtrHelper(const Wrapper &p) { return p.get(); }
108 
109 #define DECLARE_PRIVATE(Class) \
110  inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(getPtrHelper(this->d_ptr)); } \
111  inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(getPtrHelper(this->d_ptr)); } \
112  friend Class##Private;
113 
114 #define DECLARE_PRIVATE_D(Dptr, Class) \
115  inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(getPtrHelper(Dptr)); } \
116  inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(getPtrHelper(Dptr)); } \
117  friend Class##Private;
118 
119 #define D_PTR(Class) Class##Private * const d = d_func()
120 
121 #endif
122 
123 // enable warnings for printf-style functions
124 #ifndef ATTRIBUTE_FORMAT_PRINTF
125 # if INDI_HAS_ATTRIBUTE(format)
126 # define ATTRIBUTE_FORMAT_PRINTF(A, B) __attribute__((format(printf, (A), (B))))
127 # else
128 # define ATTRIBUTE_FORMAT_PRINTF(A, B)
129 # endif
130 #endif
131