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