• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LIBTEXTCLASSIFIER_UTILS_BASE_MACROS_H_
18 #define LIBTEXTCLASSIFIER_UTILS_BASE_MACROS_H_
19 
20 #include "utils/base/config.h"
21 
22 namespace libtextclassifier3 {
23 
24 #if LANG_CXX11
25 #define TC3_DISALLOW_COPY_AND_ASSIGN(TypeName) \
26   TypeName(const TypeName &) = delete;         \
27   TypeName &operator=(const TypeName &) = delete
28 #else  // C++98 case follows
29 
30 // Note that these C++98 implementations cannot completely disallow copying,
31 // as members and friends can still accidentally make elided copies without
32 // triggering a linker error.
33 #define TC3_DISALLOW_COPY_AND_ASSIGN(TypeName) \
34   TypeName(const TypeName &);                  \
35   TypeName &operator=(const TypeName &)
36 #endif  // LANG_CXX11
37 
38 // The TC3_FALLTHROUGH_INTENDED macro can be used to annotate implicit
39 // fall-through between switch labels:
40 //
41 //  switch (x) {
42 //    case 40:
43 //    case 41:
44 //      if (truth_is_out_there) {
45 //        ++x;
46 //        TC3_FALLTHROUGH_INTENDED;  // Use instead of/along with annotations in
47 //                                  // comments.
48 //      } else {
49 //        return x;
50 //      }
51 //    case 42:
52 //      ...
53 //
54 //  As shown in the example above, the TC3_FALLTHROUGH_INTENDED macro should be
55 //  followed by a semicolon. It is designed to mimic control-flow statements
56 //  like 'break;', so it can be placed in most places where 'break;' can, but
57 //  only if there are no statements on the execution path between it and the
58 //  next switch label.
59 //
60 //  When compiled with clang in C++11 mode, the TC3_FALLTHROUGH_INTENDED macro
61 //  is expanded to [[clang::fallthrough]] attribute, which is analysed when
62 //  performing switch labels fall-through diagnostic ('-Wimplicit-fallthrough').
63 //  See clang documentation on language extensions for details:
64 //  http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
65 //
66 //  When used with unsupported compilers, the TC3_FALLTHROUGH_INTENDED macro has
67 //  no effect on diagnostics.
68 //
69 //  In either case this macro has no effect on runtime behavior and performance
70 //  of code.
71 #if defined(__clang__) && defined(__has_warning)
72 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
73 #define TC3_FALLTHROUGH_INTENDED [[clang::fallthrough]]
74 #endif
75 #elif defined(__GNUC__) && __GNUC__ >= 7
76 #define TC3_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
77 #endif
78 
79 #ifndef TC3_FALLTHROUGH_INTENDED
80 #define TC3_FALLTHROUGH_INTENDED \
81   do {                           \
82   } while (0)
83 #endif
84 
85 }  // namespace libtextclassifier3
86 
87 #endif  // LIBTEXTCLASSIFIER_UTILS_BASE_MACROS_H_
88