• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 //
15 // Small, general preprocessor macros for C and C++ code.
16 #pragma once
17 
18 // Returns the number of elements in a C array.
19 #define PW_ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))
20 
21 // Returns a string literal of the arguments after expanding macros.
22 #define PW_STRINGIFY(...) _PW_STRINGIFY(__VA_ARGS__)
23 #define _PW_STRINGIFY(...) #__VA_ARGS__
24 
25 #ifdef __cplusplus
26 
27 // Macro for inline extern "C" declarations. The following will compile
28 // correctly for C and C++:
29 //
30 //   PW_EXTERN_C ThisFunctionHasCLinkage(void);
31 //
32 #define PW_EXTERN_C extern "C"
33 
34 // Macros for opening and closing an extern "C" block. This avoids the need for
35 // an #ifdef __cplusplus check around the extern "C" { and closing }. Example:
36 //
37 //   PW_EXTERN_C_START
38 //
39 //   void FunctionDeclarationForCppAndC(void);
40 //
41 //   void AnotherFunctionDeclarationForCppAndC(int, char);
42 //
43 //   PW_EXTERN_C_END
44 //
45 #define PW_EXTERN_C_START extern "C" {
46 #define PW_EXTERN_C_END }  // extern "C"
47 
48 #else  // extern "C" is removed from C code
49 
50 #define PW_EXTERN_C
51 #define PW_EXTERN_C_START
52 #define PW_EXTERN_C_END
53 
54 #endif  // __cplusplus
55