Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_BASE_DUMPABLE_H_ |
| 18 | #define ART_RUNTIME_BASE_DUMPABLE_H_ |
| 19 | |
Andreas Gampe | 0e92f4f | 2015-01-26 17:37:27 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 22 | #include "base/macros.h" |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 23 | #include "base/mutex.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // A convenience to allow any class with a "Dump(std::ostream& os)" member function |
| 28 | // but without an operator<< to be used as if it had an operator<<. Use like this: |
| 29 | // |
| 30 | // os << Dumpable<MyType>(my_type_instance); |
| 31 | // |
| 32 | template<typename T> |
| 33 | class Dumpable FINAL { |
| 34 | public: |
| 35 | explicit Dumpable(const T& value) : value_(value) { |
| 36 | } |
| 37 | |
| 38 | void Dump(std::ostream& os) const { |
| 39 | value_.Dump(os); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | const T& value_; |
| 44 | |
| 45 | DISALLOW_COPY_AND_ASSIGN(Dumpable); |
| 46 | }; |
| 47 | |
| 48 | template<typename T> |
| 49 | std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) { |
| 50 | rhs.Dump(os); |
| 51 | return os; |
| 52 | } |
| 53 | |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 54 | template<typename T> |
| 55 | class MutatorLockedDumpable { |
| 56 | public: |
| 57 | explicit MutatorLockedDumpable(T& value) REQUIRES_SHARED(Locks::mutator_lock_) : value_(value) {} |
| 58 | |
| 59 | void Dump(std::ostream& os) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 60 | value_.Dump(os); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | const T& value_; |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable); |
| 67 | }; |
| 68 | |
| 69 | template<typename T> |
| 70 | std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs) |
| 71 | // TODO: should be REQUIRES_SHARED(Locks::mutator_lock_) however annotalysis |
| 72 | // currently fails for this. |
| 73 | NO_THREAD_SAFETY_ANALYSIS; |
| 74 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 75 | } // namespace art |
| 76 | |
| 77 | #endif // ART_RUNTIME_BASE_DUMPABLE_H_ |