1 //===-- Baton.h -------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef lldb_Baton_h_
10 #define lldb_Baton_h_
11 
12 #include "lldb/lldb-enumerations.h"
13 #include "lldb/lldb-public.h"
14 
15 #include <memory>
16 
17 namespace lldb_private {
18 class Stream;
19 }
20 
21 namespace lldb_private {
22 
23 /// \class Baton Baton.h "lldb/Core/Baton.h"
24 /// A class designed to wrap callback batons so they can cleanup
25 ///        any acquired resources
26 ///
27 /// This class is designed to be used by any objects that have a callback
28 /// function that takes a baton where the baton might need to
29 /// free/delete/close itself.
30 ///
31 /// The default behavior is to not free anything. Subclasses can free any
32 /// needed resources in their destructors.
33 class Baton {
34 public:
35   Baton() {}
36   virtual ~Baton() {}
37 
38   virtual void *data() = 0;
39 
40   virtual void GetDescription(Stream *s,
41                               lldb::DescriptionLevel level) const = 0;
42 };
43 
44 class UntypedBaton : public Baton {
45 public:
46   UntypedBaton(void *Data) : m_data(Data) {}
47   ~UntypedBaton() override {
48     // The default destructor for an untyped baton does NOT attempt to clean up
49     // anything in m_data.
50   }
51 
52   void *data() override { return m_data; }
53   void GetDescription(Stream *s, lldb::DescriptionLevel level) const override;
54 
55   void *m_data; // Leave baton public for easy access
56 };
57 
58 template <typename T> class TypedBaton : public Baton {
59 public:
60   explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {}
61 
62   T *getItem() { return Item.get(); }
63   const T *getItem() const { return Item.get(); }
64 
65   void *data() override { return Item.get(); }
66   void GetDescription(Stream *s, lldb::DescriptionLevel level) const override {}
67 
68 protected:
69   std::unique_ptr<T> Item;
70 };
71 
72 } // namespace lldb_private
73 
74 #endif // lldb_Baton_h_
75