LLDB  mainline
Baton.h
Go to the documentation of this file.
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:
37  Baton() = default;
38  virtual ~Baton() = default;
39 
40  virtual void *data() = 0;
41 
42  virtual void GetDescription(llvm::raw_ostream &s,
44  unsigned indentation) const = 0;
45 };
46 
47 class UntypedBaton : public Baton {
48 public:
49  UntypedBaton(void *Data) : m_data(Data) {}
50  ~UntypedBaton() override {
51  // The default destructor for an untyped baton does NOT attempt to clean up
52  // anything in m_data.
53  }
54 
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:
64  explicit TypedBaton(std::unique_ptr<T> Item) : Item(std::move(Item)) {}
65 
66  T *getItem() { return Item.get(); }
67  const T *getItem() const { return Item.get(); }
68 
69  void *data() override { return Item.get(); }
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
lldb-public.h
lldb_private::TypedBaton::getItem
const T * getItem() const
Definition: Baton.h:67
lldb_private::TypedBaton::data
void * data() override
Definition: Baton.h:69
lldb_private::UntypedBaton::data
void * data() override
Definition: Baton.h:55
lldb_private::Baton::GetDescription
virtual void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, unsigned indentation) const =0
lldb_private::UntypedBaton::UntypedBaton
UntypedBaton(void *Data)
Definition: Baton.h:49
lldb_private::TypedBaton::TypedBaton
TypedBaton(std::unique_ptr< T > Item)
Definition: Baton.h:64
lldb_private::Baton::~Baton
virtual ~Baton()=default
lldb_private::TypedBaton::GetDescription
void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, unsigned indentation) const override
Definition: Baton.h:70
lldb-enumerations.h
lldb_private::TypedBaton::getItem
T * getItem()
Definition: Baton.h:66
lldb_private::Baton::data
virtual void * data()=0
lldb_private::TypedBaton
Definition: Baton.h:62
lldb_private::Baton::Baton
Baton()=default
lldb_private::TypedBaton::Item
std::unique_ptr< T > Item
Definition: Baton.h:74
lldb_private::UntypedBaton::~UntypedBaton
~UntypedBaton() override
Definition: Baton.h:50
lldb_private::UntypedBaton
Definition: Baton.h:47
lldb_private::Baton
Definition: Baton.h:35
lldb_private::UntypedBaton::m_data
void * m_data
Definition: Baton.h:59
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::UntypedBaton::GetDescription
void GetDescription(llvm::raw_ostream &s, lldb::DescriptionLevel level, unsigned indentation) const override
Definition: Baton.cpp:11
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207