1317027Sdim//===-- DataBuffer.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 liblldb_DataBuffer_h_
10317027Sdim#define liblldb_DataBuffer_h_
11317027Sdim#if defined(__cplusplus)
12317027Sdim
13317027Sdim#include <stdint.h>
14317027Sdim#include <string.h>
15317027Sdim
16317027Sdim#include "lldb/lldb-types.h"
17317027Sdim
18317027Sdim#include "llvm/ADT/ArrayRef.h"
19317027Sdim
20317027Sdimnamespace lldb_private {
21317027Sdim
22353358Sdim/// \class DataBuffer DataBuffer.h "lldb/Core/DataBuffer.h"
23341825Sdim/// A pure virtual protocol class for abstracted data buffers.
24317027Sdim///
25317027Sdim/// DataBuffer is an abstract class that gets packaged into a shared pointer
26341825Sdim/// that can use to implement various ways to store data (on the heap, memory
27341825Sdim/// mapped, cached inferior memory). It gets used by DataExtractor so many
28341825Sdim/// DataExtractor objects can share the same data and sub-ranges of that
29341825Sdim/// shared data, and the last object that contains a reference to the shared
30341825Sdim/// data will free it.
31317027Sdim///
32317027Sdim/// Subclasses can implement as many different constructors or member
33341825Sdim/// functions that allow data to be stored in the object's buffer prior to
34341825Sdim/// handing the shared data to clients that use these buffers.
35317027Sdim///
36341825Sdim/// All subclasses must override all of the pure virtual functions as they are
37341825Sdim/// used by clients to access the data. Having a common interface allows
38341825Sdim/// different ways of storing data, yet using it in one common way.
39317027Sdim///
40341825Sdim/// This class currently expects all data to be available without any extra
41341825Sdim/// calls being made, but we can modify it to optionally get data on demand
42341825Sdim/// with some extra function calls to load the data before it gets accessed.
43317027Sdimclass DataBuffer {
44317027Sdimpublic:
45317027Sdim  /// Destructor
46317027Sdim  ///
47341825Sdim  /// The destructor is virtual as other classes will inherit from this class
48341825Sdim  /// and be downcast to the DataBuffer pure virtual interface. The virtual
49341825Sdim  /// destructor ensures that destructing the base class will destruct the
50341825Sdim  /// class that inherited from it correctly.
51317027Sdim  virtual ~DataBuffer() {}
52317027Sdim
53317027Sdim  /// Get a pointer to the data.
54317027Sdim  ///
55353358Sdim  /// \return
56317027Sdim  ///     A pointer to the bytes owned by this object, or NULL if the
57317027Sdim  ///     object contains no bytes.
58317027Sdim  virtual uint8_t *GetBytes() = 0;
59317027Sdim
60317027Sdim  /// Get a const pointer to the data.
61317027Sdim  ///
62353358Sdim  /// \return
63317027Sdim  ///     A const pointer to the bytes owned by this object, or NULL
64317027Sdim  ///     if the object contains no bytes.
65317027Sdim  virtual const uint8_t *GetBytes() const = 0;
66317027Sdim
67317027Sdim  /// Get the number of bytes in the data buffer.
68317027Sdim  ///
69353358Sdim  /// \return
70317027Sdim  ///     The number of bytes this object currently contains.
71317027Sdim  virtual lldb::offset_t GetByteSize() const = 0;
72317027Sdim
73317027Sdim  llvm::ArrayRef<uint8_t> GetData() const {
74317027Sdim    return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
75317027Sdim  }
76317027Sdim
77317027Sdim  llvm::MutableArrayRef<uint8_t> GetData() {
78317027Sdim    return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
79317027Sdim  }
80317027Sdim};
81317027Sdim
82317027Sdim} // namespace lldb_private
83317027Sdim
84317027Sdim#endif /// #if defined(__cplusplus)
85317027Sdim#endif /// lldb_DataBuffer_h_
86