]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/Baton.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / Baton.h
1 //===-- Baton.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef lldb_Baton_h_
11 #define lldb_Baton_h_
12
13 #include "lldb/lldb-enumerations.h"
14 #include "lldb/lldb-public.h"
15
16 #include <memory>
17
18 namespace lldb_private {
19 class Stream;
20 }
21
22 namespace lldb_private {
23
24 //----------------------------------------------------------------------
25 /// @class Baton Baton.h "lldb/Core/Baton.h"
26 /// A class designed to wrap callback batons so they can cleanup
27 ///        any acquired resources
28 ///
29 /// This class is designed to be used by any objects that have a callback
30 /// function that takes a baton where the baton might need to
31 /// free/delete/close itself.
32 ///
33 /// The default behavior is to not free anything. Subclasses can free any
34 /// needed resources in their destructors.
35 //----------------------------------------------------------------------
36 class Baton {
37 public:
38   Baton() {}
39   virtual ~Baton() {}
40
41   virtual void *data() = 0;
42
43   virtual void GetDescription(Stream *s,
44                               lldb::DescriptionLevel level) const = 0;
45 };
46
47 class UntypedBaton : public Baton {
48 public:
49   UntypedBaton(void *Data) : m_data(Data) {}
50   virtual ~UntypedBaton() {
51     // The default destructor for an untyped baton does NOT attempt to clean up
52     // anything in m_data.
53   }
54
55   void *data() override { return m_data; }
56   void GetDescription(Stream *s, lldb::DescriptionLevel level) const override;
57
58   void *m_data; // Leave baton public for easy access
59 };
60
61 template <typename T> class TypedBaton : public Baton {
62 public:
63   explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {}
64
65   T *getItem() { return Item.get(); }
66   const T *getItem() const { return Item.get(); }
67
68   void *data() override { return Item.get(); }
69   virtual void GetDescription(Stream *s,
70                               lldb::DescriptionLevel level) const override {}
71
72 protected:
73   std::unique_ptr<T> Item;
74 };
75
76 } // namespace lldb_private
77
78 #endif // lldb_Baton_h_