1 /* 2 * Copyright (C) 2017 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_UTIL_BASE_MACROS_H_ 18 #define LIBTEXTCLASSIFIER_UTIL_BASE_MACROS_H_ 19 20 #include "util/base/config.h" 21 22 namespace libtextclassifier { 23 24 #if LANG_CXX11 25 #define TC_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 TC_DISALLOW_COPY_AND_ASSIGN(TypeName) \ 34 TypeName(const TypeName &); \ 35 TypeName &operator=(const TypeName &) 36 #endif // LANG_CXX11 37 38 // The TC_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 // TC_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 TC_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 TC_FALLTHROUGH_INTENDED macro is 61 // 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 TC_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(LANG_CXX11) && defined(__has_warning) 72 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough") 73 #define TC_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT 74 #endif 75 #endif 76 77 #ifndef TC_FALLTHROUGH_INTENDED 78 #define TC_FALLTHROUGH_INTENDED do { } while (0) 79 #endif 80 81 } // namespace libtextclassifier 82 83 #endif // LIBTEXTCLASSIFIER_UTIL_BASE_MACROS_H_ 84