1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_MACROS_H_
18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_MACROS_H_
19 
20 #define SAFTM_DISALLOW_COPY_AND_ASSIGN(TypeName) \
21   TypeName(const TypeName &) = delete;         \
22   TypeName &operator=(const TypeName &) = delete
23 
24 // The SAFTM_FALLTHROUGH_INTENDED macro can be used to annotate implicit
25 // fall-through between switch labels:
26 //
27 //  switch (x) {
28 //    case 40:
29 //    case 41:
30 //      if (truth_is_out_there) {
31 //        ++x;
32 //        SAFTM_FALLTHROUGH_INTENDED;  // Use instead of/along with annotations
33 //                                     // in comments.
34 //      } else {
35 //        return x;
36 //      }
37 //    case 42:
38 //      ...
39 //
40 //  As shown in the example above, the SAFTM_FALLTHROUGH_INTENDED macro should
41 //  be followed by a semicolon. It is designed to mimic control-flow statements
42 //  like 'break;', so it can be placed in most places where 'break;' can, but
43 //  only if there are no statements on the execution path between it and the
44 //  next switch label.
45 //
46 //  When compiled with clang, the SAFTM_FALLTHROUGH_INTENDED macro is expanded
47 //  to [[clang::fallthrough]] attribute, which is analysed when performing
48 //  switch labels fall-through diagnostic ('-Wimplicit-fallthrough').  See clang
49 //  documentation on language extensions for details:
50 //  http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
51 //
52 //  When used with unsupported compilers, the SAFTM_FALLTHROUGH_INTENDED macro
53 //  has no effect on diagnostics.
54 //
55 //  In either case this macro has no effect on runtime behavior and performance
56 //  of code.
57 #if defined(__clang__) && defined(__has_warning)
58 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
59 #define SAFTM_FALLTHROUGH_INTENDED [[clang::fallthrough]]  // NOLINT
60 #endif
61 #endif
62 
63 #ifndef SAFTM_FALLTHROUGH_INTENDED
64 #define SAFTM_FALLTHROUGH_INTENDED \
65   do {                           \
66   } while (0)
67 #endif
68 
69 // SAFTM_UNIQUE_ID(prefix) expands to a unique id that starts with prefix.
70 //
71 // The current implementation expands to prefix_<line_number>; hence, multiple
72 // uses of this macro with the same prefix and on the same line will result in
73 // the same identifier name.  In those cases, if you need different ids, we
74 // suggest you use different prefixes.
75 //
76 // Implementation is tricky; for more info, see
77 // https://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr
78 #define SAFTM_UNIQUE_ID_INTERNAL2(x, y)  x ## y
79 #define SAFTM_UNIQUE_ID_INTERNAL(x, y)   SAFTM_UNIQUE_ID_INTERNAL2(x, y)
80 #define SAFTM_UNIQUE_ID(prefix)  SAFTM_UNIQUE_ID_INTERNAL(prefix ## _, __LINE__)
81 
82 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_BASE_MACROS_H_
83