xref: /petsc/include/petsc/private/cpp/macros.hpp (revision 9dd11ecf0918283bb567d8b33a92f53ac4ea7840)
1 #pragma once
2 
3 #include <petscmacros.h>
4 
5 // basic building blocks
6 #define PETSC_DECLTYPE_AUTO(...) ->decltype(__VA_ARGS__)
7 #define PETSC_NOEXCEPT_AUTO(...) noexcept(noexcept(__VA_ARGS__))
8 #define PETSC_RETURNS(...) \
9   { \
10     return __VA_ARGS__; \
11   }
12 
13 // one without the other
14 #define PETSC_DECLTYPE_AUTO_RETURNS(...) PETSC_DECLTYPE_AUTO(__VA_ARGS__) PETSC_RETURNS(__VA_ARGS__)
15 #define PETSC_NOEXCEPT_AUTO_RETURNS(...) PETSC_NOEXCEPT_AUTO(__VA_ARGS__) PETSC_RETURNS(__VA_ARGS__)
16 
17 // both
18 #define PETSC_DECLTYPE_NOEXCEPT_AUTO(...) PETSC_NOEXCEPT_AUTO(__VA_ARGS__) PETSC_DECLTYPE_AUTO(__VA_ARGS__)
19 // all
20 #define PETSC_DECLTYPE_NOEXCEPT_AUTO_RETURNS(...) PETSC_DECLTYPE_NOEXCEPT_AUTO(__VA_ARGS__) PETSC_RETURNS(__VA_ARGS__)
21 
22 // PETSC_CXX_COMPAT_DECL() - Helper macro to declare a C++ class member function or
23 // free-standing function guaranteed to be compatible with C
24 //
25 // input params:
26 // __VA_ARGS__ - the function declaration
27 //
28 // notes:
29 // Normally member functions of C++ structs or classes are not callable from C as they have an
30 // implicit "this" parameter tacked on the front (analogous to Pythons "self"). Static
31 // functions on the other hand do not have this restriction. This macro applies static to the
32 // function declaration as well as noexcept (as C++ exceptions escaping the C++ boundary is
33 // undefined behavior anyways) and [[nodiscard]].
34 //
35 // Note that the user should take care that function arguments and return type are also C
36 // compatible.
37 //
38 // example usage:
39 // class myclass
40 // {
41 // public:
42 //   PETSC_CXX_COMPAT_DECL(PetscErrorCode foo(int,Vec,char));
43 // };
44 //
45 // use this to define inline as well
46 //
47 // class myclass
48 // {
49 // public:
50 //   PETSC_CXX_COMPAT_DECL(PetscErrorCode foo(int a, Vec b, charc))
51 //   {
52 //     ...
53 //   }
54 // };
55 //
56 // or to define a free-standing function
57 //
58 // PETSC_CXX_COMPAT_DECL(bool bar(int x, int y))
59 // {
60 //   ...
61 // }
62 #define PETSC_CXX_COMPAT_DECL(...) PETSC_NODISCARD static inline __VA_ARGS__ noexcept
63 
64 // PETSC_CXX_COMPAT_DEFN() - Corresponding macro to define a C++ member function declared using
65 // PETSC_CXX_COMPAT_DECL()
66 //
67 // input params:
68 // __VA_ARGS__ - the function prototype (not the body!)
69 //
70 // notes:
71 // prepends inline and appends noexcept to the function
72 //
73 // example usage:
74 // PETSC_CXX_COMPAT_DEFN(PetscErrorCode myclass::foo(int a, Vec b, char c))
75 // {
76 //   ...
77 // }
78 #define PETSC_CXX_COMPAT_DEFN(...) inline __VA_ARGS__ noexcept
79