Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
10#include <Ultralight/RefPtr.h>
11
12namespace ultralight {
13
14///
15/// Function signature for a user-defined destruction callback to optionally be called when Buffer
16/// is destroyed. Users can use this to deallocate any data associated with the Buffer.
17///
18/// @param user_data Pointer to user-defined user-data (this will be the same value as what was
19/// passed to Buffer::Create, if any)
20///
21/// @param data Pointer to raw Buffer data.
22///
23typedef void (*DestroyBufferCallback)(void* user_data, void* data);
24
25///
26/// A fixed-size container for raw byte data.
27///
28/// This class is used to represent raw data buffers in Ultralight. It intelligently manages the
29/// lifetime of the data and can optionally call a user-supplied callback to deallocate the data
30/// when the Buffer is destroyed.
31///
32class UExport Buffer : public RefCounted {
33 public:
34 ///
35 /// Create a Buffer from existing, user-owned data without any copies. An optional, user-supplied
36 /// callback will be called to deallocate data upon destruction.
37 ///
38 /// @param data A pointer to the data.
39 ///
40 /// @param size Size of the data in bytes.
41 ///
42 /// @param user_data Optional user data that will be passed to destruction_callback
43 /// when the returned Buffer is destroyed.
44 ///
45 /// @param destruction_callback Optional callback that will be called upon destruction. Pass a
46 /// null pointer if you don't want to be informed of destruction.
47 ///
48 ///
49 /// @return A ref-counted Buffer object that wraps the existing data.
50 ///
51 static RefPtr<Buffer> Create(void* data, size_t size, void* user_data,
52 DestroyBufferCallback destruction_callback);
53
54 ///
55 /// Create a Buffer from existing data, a deep copy of data will be made.
56 ///
57 static RefPtr<Buffer> CreateFromCopy(const void* data, size_t size);
58
59 ///
60 /// Get a pointer to the raw byte data.
61 ///
62 virtual void* data() = 0;
63
64 ///
65 /// Get the size in bytes.
66 ///
67 virtual size_t size() const = 0;
68
69 ///
70 /// Get the user data associated with this Buffer, if any.
71 ///
72 virtual void* user_data() = 0;
73
74 ///
75 /// Check whether this Buffer owns its own data (Buffer was created via CreateFromCopy).
76 /// If this is false, Buffer will call the user-supplied destruction callback to deallocate data
77 /// when this Buffer instance is destroyed.
78 ///
79 virtual bool owns_data() const = 0;
80
81 protected:
83 virtual ~Buffer();
84 Buffer(const Buffer&);
85 void operator=(const Buffer&);
86};
87
88} // namespace ultralight
#define UExport
Definition Exports.h:25
A fixed-size container for raw byte data.
Definition Buffer.h:32
Buffer(const Buffer &)
virtual void * user_data()=0
Get the user data associated with this Buffer, if any.
virtual bool owns_data() const =0
Check whether this Buffer owns its own data (Buffer was created via CreateFromCopy).
static RefPtr< Buffer > Create(void *data, size_t size, void *user_data, DestroyBufferCallback destruction_callback)
Create a Buffer from existing, user-owned data without any copies.
void operator=(const Buffer &)
virtual size_t size() const =0
Get the size in bytes.
virtual void * data()=0
Get a pointer to the raw byte data.
static RefPtr< Buffer > CreateFromCopy(const void *data, size_t size)
Create a Buffer from existing data, a deep copy of data will be made.
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:47
A nullable smart pointer.
Definition RefPtr.h:79
Definition App.h:14
void(* DestroyBufferCallback)(void *user_data, void *data)
Function signature for a user-defined destruction callback to optionally be called when Buffer is des...
Definition Buffer.h:23