1317027Sdim//===-- Baton.h -------------------------------------------------*- C++ -*-===// 2317027Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6317027Sdim// 7317027Sdim//===----------------------------------------------------------------------===// 8317027Sdim 9317027Sdim#ifndef lldb_Baton_h_ 10317027Sdim#define lldb_Baton_h_ 11317027Sdim 12344779Sdim#include "lldb/lldb-enumerations.h" 13317027Sdim#include "lldb/lldb-public.h" 14317027Sdim 15360784Sdim#include "llvm/Support/raw_ostream.h" 16360784Sdim 17344779Sdim#include <memory> 18317027Sdim 19317027Sdimnamespace lldb_private { 20317027Sdimclass Stream; 21317027Sdim} 22317027Sdim 23317027Sdimnamespace lldb_private { 24317027Sdim 25353358Sdim/// \class Baton Baton.h "lldb/Core/Baton.h" 26341825Sdim/// A class designed to wrap callback batons so they can cleanup 27317027Sdim/// any acquired resources 28317027Sdim/// 29341825Sdim/// This class is designed to be used by any objects that have a callback 30341825Sdim/// function that takes a baton where the baton might need to 31317027Sdim/// free/delete/close itself. 32317027Sdim/// 33341825Sdim/// The default behavior is to not free anything. Subclasses can free any 34341825Sdim/// needed resources in their destructors. 35317027Sdimclass Baton { 36317027Sdimpublic: 37317027Sdim Baton() {} 38317027Sdim virtual ~Baton() {} 39317027Sdim 40317027Sdim virtual void *data() = 0; 41317027Sdim 42360784Sdim virtual void GetDescription(llvm::raw_ostream &s, 43360784Sdim lldb::DescriptionLevel level, 44360784Sdim unsigned indentation) const = 0; 45317027Sdim}; 46317027Sdim 47317027Sdimclass UntypedBaton : public Baton { 48317027Sdimpublic: 49317027Sdim UntypedBaton(void *Data) : m_data(Data) {} 50353358Sdim ~UntypedBaton() override { 51317027Sdim // The default destructor for an untyped baton does NOT attempt to clean up 52317027Sdim // anything in m_data. 53317027Sdim } 54317027Sdim 55317027Sdim void *data() override { return m_data; } 56360784Sdim void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, 57360784Sdim unsigned indentation) const override; 58317027Sdim 59317027Sdim void *m_data; // Leave baton public for easy access 60317027Sdim}; 61317027Sdim 62317027Sdimtemplate <typename T> class TypedBaton : public Baton { 63317027Sdimpublic: 64317027Sdim explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {} 65317027Sdim 66317027Sdim T *getItem() { return Item.get(); } 67317027Sdim const T *getItem() const { return Item.get(); } 68317027Sdim 69317027Sdim void *data() override { return Item.get(); } 70360784Sdim void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, 71360784Sdim unsigned indentation) const override {} 72317027Sdim 73317027Sdimprotected: 74317027Sdim std::unique_ptr<T> Item; 75317027Sdim}; 76317027Sdim 77317027Sdim} // namespace lldb_private 78317027Sdim 79317027Sdim#endif // lldb_Baton_h_ 80