1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_BASE_PORT_H_
31 #define MOZC_BASE_PORT_H_
32 
33 #include "base/port_string.h"
34 
35 // Check duplicate OS_XXX definition.
36 
37 #ifdef OS_WIN
38 #define MOZC_OS_DEFINED
39 #endif  // OS_WIN
40 
41 #ifdef OS_MACOSX
42 #define MOZC_OS_DEFINED
43 #endif  // OS_MACOSX
44 
45 #ifdef OS_ANDROID
46 #define MOZC_OS_DEFINED
47 #endif  // OS_ANDROID
48 
49 #ifdef OS_NACL
50 #define MOZC_OS_DEFINED
51 #endif  // OS_NACL
52 
53 #ifdef OS_LINUX
54 // TODO(matsuzakit): Remove following guard.
55 // Currently OS_LINUX and (OS_ANDROID or OS_NACL) are defined at the same time.
56 #if !defined(OS_ANDROID) && !defined(OS_NACL)
57 #define MOZC_OS_DEFINED
58 #endif  // !OS_ANDROID && !OS_NACL
59 #endif  // OS_LINUX
60 
61 
62 #ifndef MOZC_OS_DEFINED
63 #error "OS_XXX (e.g., OS_WIN) must be defined."
64 #endif  // !MOZC_OS_DEFINED
65 
66 #undef MOZC_OS_DEFINED
67 
68 
69 
70 #ifndef _MSC_VER
71 #if !defined(__STDC_FORMAT_MACROS)
72 #define __STDC_FORMAT_MACROS
73 #endif  // !__STDC_FORMAT_MACROS
74 #include <inttypes.h>
75 #endif  // _MSC_VER
76 #include <sys/types.h>
77 #include <stdint.h>
78 #include <cstddef>
79 
80 // Integral types.
81 #ifdef OS_WIN
82 using int8 = __int8;
83 using int16 = __int16;
84 using int32 = __int32;
85 using int64 = __int64;
86 using uint8 = unsigned __int8;
87 using uint16 = unsigned __int16;
88 using uint32 = unsigned __int32;
89 using uint64 = unsigned __int64;
90 #else  // OS_WIN
91 using int8 = int8_t;
92 using int16 = int16_t;
93 using int32 = int32_t;
94 using int64 = int64_t;
95 using uint8 = uint8_t;
96 using uint16 = uint16_t;
97 using uint32 = uint32_t;
98 using uint64 = uint64_t;
99 #endif  // OS_WIN
100 using char32 = uint32;
101 
102 template <typename T, size_t N>
103 char (&ArraySizeHelper(T (&array)[N]))[N];
104 
105 #ifndef _MSC_VER
106 template <typename T, size_t N>
107 char (&ArraySizeHelper(const T (&array)[N]))[N];
108 #endif  // !_MSC_VER
109 
110 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
111 
112 #define GG_LONGLONG(x) x##LL
113 #define GG_ULONGLONG(x) x##ULL
114 
115 // Print format strings for 64-bit integers.
116 #ifdef _MSC_VER
117 #define MOZC_PRId64 "I64d"
118 #define MOZC_PRIo64 "I64o"
119 #define MOZC_PRIu64 "I64u"
120 #define MOZC_PRIx64 "I64x"
121 #define MOZC_PRIX64 "I64X"
122 #else  // _MSC_VER
123 #define MOZC_PRId64 PRId64
124 #define MOZC_PRIo64 PRIo64
125 #define MOZC_PRIu64 PRIu64
126 #define MOZC_PRIx64 PRIx64
127 #define MOZC_PRIX64 PRIX64
128 #endif  // _MSC_VER
129 
130 // INT_MIN, INT_MAX, UINT_MAX family at Google
131 static const uint8  kuint8max  = (( uint8) 0xFF);
132 static const uint16 kuint16max = ((uint16) 0xFFFF);
133 static const uint32 kuint32max = ((uint32) 0xFFFFFFFF);
134 static const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF));
135 static const  int8  kint8min   = ((  int8) 0x80);
136 static const  int8  kint8max   = ((  int8) 0x7F);
137 static const  int16 kint16min  = (( int16) 0x8000);
138 static const  int16 kint16max  = (( int16) 0x7FFF);
139 static const  int32 kint32min  = (( int32) 0x80000000);
140 static const  int32 kint32max  = (( int32) 0x7FFFFFFF);
141 static const  int64 kint64min  = (( int64) GG_LONGLONG(0x8000000000000000));
142 static const  int64 kint64max  = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
143 
144 #define DISALLOW_COPY_AND_ASSIGN(TypeName)    \
145     TypeName(const TypeName&) = delete;       \
146     void operator=(const TypeName&) = delete
147 
148 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)  \
149     TypeName() = delete;                          \
150     DISALLOW_COPY_AND_ASSIGN(TypeName)
151 
152 // Macro for annotating implicit fall-through
153 // TODO(team): Implement this.
154 #define  FALLTHROUGH_INTENDED do { } while (0)
155 
156 #if defined(__GNUC__) || defined(__clang__)
157 // Tell the compiler to do printf format string checking if the
158 // compiler supports it; see the 'format' attribute in
159 // <http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Function-Attributes.html>.
160 //
161 // N.B.: As the GCC manual states, "[s]ince non-static C++ methods
162 // have an implicit 'this' argument, the arguments of such methods
163 // should be counted from two, not one."
164 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
165   __attribute__((__format__(__printf__, string_index, first_to_check)))
166 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
167   __attribute__((__format__(__scanf__, string_index, first_to_check)))
168 #else
169 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
170 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
171 #endif  // __GNUC__ || __clang__
172 
173 #define AS_STRING(x)   AS_STRING_INTERNAL(x)
174 #define AS_STRING_INTERNAL(x)   #x
175 
176 #endif  // MOZC_BASE_PORT_H_
177