1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef DDLogObject_h_
8 #define DDLogObject_h_
9 
10 #include "nsString.h"
11 
12 namespace mozilla {
13 
14 // DDLogObject identifies a C++ object by its pointer and its class name (as
15 // provided in a DDLoggedTypeTrait.)
16 // Note that a DDLogObject could have the exact same pointer&type as a previous
17 // one, so extra information is needed to distinguish them, see DDLifetime.
18 class DDLogObject {
19  public:
20   // Default-initialization with null pointer.
DDLogObject()21   DDLogObject() : mTypeName("<unset>"), mPointer(nullptr) {}
22 
23   // Construction with given non-null type name and pointer.
DDLogObject(const char * aTypeName,const void * aPointer)24   DDLogObject(const char* aTypeName, const void* aPointer)
25       : mTypeName(aTypeName), mPointer(aPointer) {
26     MOZ_ASSERT(aTypeName);
27     MOZ_ASSERT(aPointer);
28   }
29 
30   // Sets this DDLogObject to an actual object.
Set(const char * aTypeName,const void * aPointer)31   void Set(const char* aTypeName, const void* aPointer) {
32     MOZ_ASSERT(aTypeName);
33     MOZ_ASSERT(aPointer);
34     mTypeName = aTypeName;
35     mPointer = aPointer;
36   }
37 
38   // Object pointer, used for identification purposes only.
Pointer()39   const void* Pointer() const { return mPointer; }
40 
41   // Type name. Should only be accessed after non-null pointer initialization.
TypeName()42   const char* TypeName() const {
43     MOZ_ASSERT(mPointer);
44     return mTypeName;
45   }
46 
47   bool operator==(const DDLogObject& a) const {
48     return mPointer == a.mPointer && (!mPointer || mTypeName == a.mTypeName);
49   }
50 
51   // Print the type name and pointer, e.g.: "MediaDecoder[136078200]".
52   void AppendPrintf(nsCString& mString) const;
53   nsCString Printf() const;
54 
55  private:
56   const char* mTypeName;
57   const void* mPointer;
58 };
59 
60 }  // namespace mozilla
61 
62 #endif  // DDLogObject_h_
63