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
19namespace lldb_private {
20class Stream;
21}
22
23namespace 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.
35class Baton {
36public:
37  Baton() = default;
38  virtual ~Baton() = default;
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
47class UntypedBaton : public Baton {
48public:
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
62template <typename T> class TypedBaton : public Baton {
63public:
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
73protected:
74  std::unique_ptr<T> Item;
75};
76
77} // namespace lldb_private
78
79#endif // LLDB_UTILITY_BATON_H
80