• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <ostream>
21 
22 #include "base/macros.h"
23 #include "base/mutex.h"
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:
Dumpable(const T & value)35   explicit Dumpable(const T& value) : value_(value) {
36   }
37 
Dump(std::ostream & os)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 
54 template<typename T>
55 class MutatorLockedDumpable {
56  public:
MutatorLockedDumpable(T & value)57   explicit MutatorLockedDumpable(T& value) REQUIRES_SHARED(Locks::mutator_lock_) : value_(value) {}
58 
Dump(std::ostream & os)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 
75 }  // namespace art
76 
77 #endif  // ART_RUNTIME_BASE_DUMPABLE_H_
78