blob: 9ef8d69b48984592ba620325d025ac472418b5a7 [file] [log] [blame]
Ian Rogersc7dd2952014-10-21 23:31:19 -07001/*
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 Gampe0e92f4f2015-01-26 17:37:27 -080020#include <ostream>
21
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include "base/macros.h"
Mathieu Chartier8778c522016-10-04 19:06:30 -070023#include "base/mutex.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070024
25namespace 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//
32template<typename T>
33class 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
48template<typename T>
49std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) {
50 rhs.Dump(os);
51 return os;
52}
53
Mathieu Chartier8778c522016-10-04 19:06:30 -070054template<typename T>
55class 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
69template<typename T>
70std::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 Rogersc7dd2952014-10-21 23:31:19 -070075} // namespace art
76
77#endif // ART_RUNTIME_BASE_DUMPABLE_H_