1 //===-- Baton.h -------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
12 #include "lldb/lldb-enumerations.h"
13 #include "lldb/lldb-public.h"
17 namespace lldb_private {
21 namespace lldb_private {
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
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.
31 /// The default behavior is to not free anything. Subclasses can free any
32 /// needed resources in their destructors.
38 virtual void *data() = 0;
40 virtual void GetDescription(Stream *s,
41 lldb::DescriptionLevel level) const = 0;
44 class UntypedBaton : public Baton {
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.
52 void *data() override { return m_data; }
53 void GetDescription(Stream *s, lldb::DescriptionLevel level) const override;
55 void *m_data; // Leave baton public for easy access
58 template <typename T> class TypedBaton : public Baton {
60 explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {}
62 T *getItem() { return Item.get(); }
63 const T *getItem() const { return Item.get(); }
65 void *data() override { return Item.get(); }
66 void GetDescription(Stream *s, lldb::DescriptionLevel level) const override {}
69 std::unique_ptr<T> Item;
72 } // namespace lldb_private
74 #endif // lldb_Baton_h_