portability.h
Go to the documentation of this file.
1 /**************************************************************************/ /**
2  * @brief Library portability helper definitions.
3  * @file
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include <cstdint>
9 
10 // References:
11 // - https://gcc.gnu.org/wiki/Visibility.
12 // - https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/port_def.inc
13 #if defined(_WIN32) || defined(_MSC_VER) || defined(__CYGWIN__)
14 # ifdef BUILDING_DLL
15 # ifdef __GNUC__
16 # define P1_EXPORT __attribute__((dllexport))
17 # else
18 # define P1_EXPORT __declspec(dllexport)
19 # endif
20 # else
21 # ifdef __GNUC__
22 # define P1_EXPORT __attribute__((dllimport))
23 # else
24 # define P1_EXPORT __declspec(dllimport)
25 # endif
26 # endif
27 # define P1_HIDDEN
28 #else
29 # if __GNUC__ >= 4
30 # define P1_EXPORT __attribute__((visibility("default")))
31 # define P1_HIDDEN __attribute__((visibility("hidden")))
32 # else
33 # define P1_EXPORT
34 # define P1_HIDDEN
35 # endif
36 #endif
37 
38 // Support ARM CC quirks
39 #ifdef __CC_ARM
40 // The cstdint included with Keil ARM CC does not appear to include stdint.h
41 // or to define most of the stdint types (uint8_t, etc.), even though it should.
42 # include <stdint.h>
43 // Fixes bug in macro used Keil's math.h header where they define NAN (see
44 // http://www.keil.com/forum/60227/).
45 # define __ESCAPE__(__x) (__x)
46 # define P1_HAVE_STD_FUNCTION 0
47 # define P1_HAVE_STD_OSTREAM 0
48 # define P1_HAVE_STD_SMART_PTR 0
49 # define P1_NO_LOGGING 1
50 #endif
51 
52 // Different compilers support different ways of specifying default struct
53 // alignment. Since there's no universal method, a macro is used instead.
54 #ifdef __CC_ARM
55 # define P1_ALIGNAS(N) __attribute__((aligned(N)))
56 #else
57 # define P1_ALIGNAS(N) alignas(N)
58 #endif
59 
60 // ssize_t is a POSIX extension and is not supported on Windows/ARM CC.
61 #if defined(_WIN32) || defined(__CC_ARM)
62 typedef int32_t p1_ssize_t;
63 #elif defined(_MSC_VER)
64 typedef int64_t p1_ssize_t;
65 #else
66 # include <sys/types.h> // For ssize_t
67 typedef ssize_t p1_ssize_t;
68 #endif
69 
70 #ifndef P1_HAVE_STD_OSTREAM
71 # define P1_HAVE_STD_OSTREAM 1
72 #endif
73 #if P1_HAVE_STD_OSTREAM
74 # include <ostream>
75 using p1_ostream = std::ostream;
76 #else
77 class p1_ostream {
78  public:
79  p1_ostream() = default;
80 };
81 template <class T>
82 inline p1_ostream& operator<<(p1_ostream& stream, const T&) {
83  return stream;
84 }
85 #endif
86 
87 #ifndef P1_HAVE_STD_FUNCTION
88 # define P1_HAVE_STD_FUNCTION 1
89 #endif
90 
91 #ifndef P1_HAVE_STD_SMART_PTR
92 # define P1_HAVE_STD_SMART_PTR 1
93 #endif
94 
95 // Support for multi-statement constexpr functions was not added until C++14.
96 // When compiling with C++11, we'll simply use inline instead.
97 #ifndef P1_HAVE_MULTILINE_CONSTEXPR_FUNC
98 # define P1_HAVE_MULTILINE_CONSTEXPR_FUNC (__cplusplus >= 201402L)
99 #endif
100 
101 #ifndef P1_CONSTEXPR_FUNC
102 # if P1_HAVE_MULTILINE_CONSTEXPR_FUNC
103 # define P1_CONSTEXPR_FUNC constexpr
104 # else
105 # define P1_CONSTEXPR_FUNC inline
106 # endif
107 #endif
NullStream & operator<<(NullStream &stream, const T &)
Definition: logging.h:50
ssize_t p1_ssize_t
Definition: portability.h:67
std::ostream p1_ostream
Definition: portability.h:75