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_UTILITY_BATON_H 10 #define LLDB_UTILITY_BATON_H 11 12 #include "lldb/lldb-enumerations.h" 13 #include "lldb/lldb-public.h" 14 15 #include "llvm/Support/raw_ostream.h" 16 17 #include <memory> 18 19 namespace lldb_private { 20 class Stream; 21 } 22 23 namespace lldb_private { 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 class Baton { 36 public: Baton()37 Baton() {} ~Baton()38 virtual ~Baton() {} 39 40 virtual void *data() = 0; 41 42 virtual void GetDescription(llvm::raw_ostream &s, 43 lldb::DescriptionLevel level, 44 unsigned indentation) const = 0; 45 }; 46 47 class UntypedBaton : public Baton { 48 public: UntypedBaton(void * Data)49 UntypedBaton(void *Data) : m_data(Data) {} ~UntypedBaton()50 ~UntypedBaton() override { 51 // The default destructor for an untyped baton does NOT attempt to clean up 52 // anything in m_data. 53 } 54 data()55 void *data() override { return m_data; } 56 void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, 57 unsigned indentation) const override; 58 59 void *m_data; // Leave baton public for easy access 60 }; 61 62 template <typename T> class TypedBaton : public Baton { 63 public: TypedBaton(std::unique_ptr<T> Item)64 explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {} 65 getItem()66 T *getItem() { return Item.get(); } getItem()67 const T *getItem() const { return Item.get(); } 68 data()69 void *data() override { return Item.get(); } GetDescription(llvm::raw_ostream & s,lldb::DescriptionLevel level,unsigned indentation)70 void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, 71 unsigned indentation) const override {} 72 73 protected: 74 std::unique_ptr<T> Item; 75 }; 76 77 } // namespace lldb_private 78 79 #endif // LLDB_UTILITY_BATON_H 80