Instrument Neutral Distributed Interface INDI  2.0.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 
37 #ifdef _MSC_VER
38 # include <BaseTsd.h>
39 typedef SSIZE_T ssize_t;
40 #else
41 # include <unistd.h>
42 #endif
43 
47 #ifndef INDI_HAS_CPP_ATTRIBUTE
48 # ifdef __has_cpp_attribute
49 # define INDI_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
50 # else
51 # define INDI_HAS_CPP_ATTRIBUTE(x) 0
52 # endif
53 #endif
54 
58 #ifndef INDI_HAS_ATTRIBUTE
59 # ifdef __has_attribute
60 # define INDI_HAS_ATTRIBUTE(x) __has_attribute(x)
61 # else
62 # define INDI_HAS_ATTRIBUTE(x) 0
63 # endif
64 #endif
65 
87 #ifndef INDI_FALLTHROUGH
88 # if defined(__cplusplus)
89 # if INDI_HAS_CPP_ATTRIBUTE(clang::fallthrough)
90 # define INDI_FALLTHROUGH [[clang::fallthrough]]
91 # elif INDI_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
92 # define INDI_FALLTHROUGH [[gnu::fallthrough]]
93 # elif INDI_HAS_CPP_ATTRIBUTE(fallthrough)
94 # define INDI_FALLTHROUGH [[fallthrough]]
95 # endif
96 # else
97 # if INDI_HAS_ATTRIBUTE(fallthrough)
98 # define INDI_FALLTHROUGH __attribute__((fallthrough))
99 # else
100 # define INDI_FALLTHROUGH do {} while (0)
101 # endif
102 # endif
103 #endif
104 
112 #if defined(__cplusplus)
113 
114 #include <memory>
115 
116 template <typename T>
117 static inline std::shared_ptr<T> make_shared_weak(T *object)
118 {
119  return std::shared_ptr<T>(object, [](T*) {});
120 }
121 
122 template <typename T>
123 static inline T *getPtrHelper(T *ptr)
124 {
125  return ptr;
126 }
127 
128 template <typename Wrapper>
129 static inline typename Wrapper::element_type *getPtrHelper(const Wrapper &p)
130 {
131  return p.get();
132 }
133 
134 #define DECLARE_PRIVATE(Class) \
135  inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(getPtrHelper(this->d_ptr)); } \
136  inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(getPtrHelper(this->d_ptr)); } \
137  friend Class##Private;
138 
139 #define DECLARE_PRIVATE_D(Dptr, Class) \
140  inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(getPtrHelper(Dptr)); } \
141  inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(getPtrHelper(Dptr)); } \
142  friend Class##Private;
143 
144 #define D_PTR(Class) Class##Private * const d = d_func()
145 
146 #endif
147 
148 // enable warnings for printf-style functions
149 #ifndef ATTRIBUTE_FORMAT_PRINTF
150 # if INDI_HAS_ATTRIBUTE(format)
151 # define ATTRIBUTE_FORMAT_PRINTF(A, B) __attribute__((format(printf, (A), (B))))
152 # else
153 # define ATTRIBUTE_FORMAT_PRINTF(A, B)
154 # endif
155 #endif
156 
157 #ifdef SWIG
158 # define INDI_DEPRECATED(message)
159 #elif __cplusplus
160 # define INDI_DEPRECATED(message) [[deprecated(message)]]
161 #else
162 # define INDI_DEPRECATED(message) __attribute__ ((deprecated))
163 #endif