1 /* 2 * Copyright (C) 2019 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 ART_RUNTIME_VERIFIER_SCOPED_NEWLINE_H_ 18 #define ART_RUNTIME_VERIFIER_SCOPED_NEWLINE_H_ 19 20 #include <ostream> 21 22 #include <android-base/logging.h> 23 24 #include "base/macros.h" 25 26 namespace art HIDDEN { 27 namespace verifier { 28 29 // RAII to inject a newline after a message. 30 struct ScopedNewLine { ScopedNewLineScopedNewLine31 explicit ScopedNewLine(std::ostream& os) : stream(os) {} 32 ScopedNewLineScopedNewLine33 ScopedNewLine(ScopedNewLine&& other) noexcept : stream(other.stream), active(other.active) { 34 other.active = false; 35 } 36 37 ScopedNewLine(ScopedNewLine&) = delete; 38 ScopedNewLine& operator=(ScopedNewLine&) = delete; 39 ~ScopedNewLineScopedNewLine40 ~ScopedNewLine() { 41 if (active) { 42 stream << std::endl; 43 } 44 } 45 46 template <class T> 47 ScopedNewLine& operator<<(const T& t) { 48 DCHECK(active); 49 stream << t; 50 return *this; 51 } 52 53 ScopedNewLine& operator<<(std::ostream& (*f)(std::ostream&)) { 54 DCHECK(active); 55 stream << f; 56 return *this; 57 } 58 59 std::ostream& stream; 60 bool active = true; 61 }; 62 63 } // namespace verifier 64 } // namespace art 65 66 #endif // ART_RUNTIME_VERIFIER_SCOPED_NEWLINE_H_ 67