Baton.h revision 317027
1317027Sdim//===-- Baton.h -------------------------------------------------*- C++ -*-===// 2317027Sdim// 3317027Sdim// The LLVM Compiler Infrastructure 4317027Sdim// 5317027Sdim// This file is distributed under the University of Illinois Open Source 6317027Sdim// License. See LICENSE.TXT for details. 7317027Sdim// 8317027Sdim//===----------------------------------------------------------------------===// 9317027Sdim 10317027Sdim#ifndef lldb_Baton_h_ 11317027Sdim#define lldb_Baton_h_ 12317027Sdim 13317027Sdim#include "lldb/lldb-enumerations.h" // for DescriptionLevel 14317027Sdim#include "lldb/lldb-public.h" 15317027Sdim 16317027Sdim#include <memory> // for unique_ptr 17317027Sdim 18317027Sdimnamespace lldb_private { 19317027Sdimclass Stream; 20317027Sdim} 21317027Sdim 22317027Sdimnamespace lldb_private { 23317027Sdim 24317027Sdim//---------------------------------------------------------------------- 25317027Sdim/// @class Baton Baton.h "lldb/Core/Baton.h" 26317027Sdim/// @brief A class designed to wrap callback batons so they can cleanup 27317027Sdim/// any acquired resources 28317027Sdim/// 29317027Sdim/// This class is designed to be used by any objects that have a 30317027Sdim/// callback function that takes a baton where the baton might need to 31317027Sdim/// free/delete/close itself. 32317027Sdim/// 33317027Sdim/// The default behavior is to not free anything. Subclasses can 34317027Sdim/// free any needed resources in their destructors. 35317027Sdim//---------------------------------------------------------------------- 36317027Sdimclass Baton { 37317027Sdimpublic: 38317027Sdim Baton() {} 39317027Sdim virtual ~Baton() {} 40317027Sdim 41317027Sdim virtual void *data() = 0; 42317027Sdim 43317027Sdim virtual void GetDescription(Stream *s, 44317027Sdim lldb::DescriptionLevel level) const = 0; 45317027Sdim}; 46317027Sdim 47317027Sdimclass UntypedBaton : public Baton { 48317027Sdimpublic: 49317027Sdim UntypedBaton(void *Data) : m_data(Data) {} 50317027Sdim virtual ~UntypedBaton() { 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; } 56317027Sdim void GetDescription(Stream *s, lldb::DescriptionLevel level) const override; 57317027Sdim 58317027Sdim void *m_data; // Leave baton public for easy access 59317027Sdim}; 60317027Sdim 61317027Sdimtemplate <typename T> class TypedBaton : public Baton { 62317027Sdimpublic: 63317027Sdim explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {} 64317027Sdim 65317027Sdim T *getItem() { return Item.get(); } 66317027Sdim const T *getItem() const { return Item.get(); } 67317027Sdim 68317027Sdim void *data() override { return Item.get(); } 69317027Sdim virtual void GetDescription(Stream *s, 70317027Sdim lldb::DescriptionLevel level) const override {} 71317027Sdim 72317027Sdimprotected: 73317027Sdim std::unique_ptr<T> Item; 74317027Sdim}; 75317027Sdim 76317027Sdim} // namespace lldb_private 77317027Sdim 78317027Sdim#endif // lldb_Baton_h_ 79