Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 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 be optionally called when Buffer
16/// is destroyed.
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 byte container for passing data around.
27///
28class UExport Buffer : public RefCounted {
29 public:
30 ///
31 /// Create a Buffer from existing, user-owned data without any copies. An optional, user-supplied
32 /// callback will be called to deallocate data upon destruction.
33 ///
34 /// @param data A pointer to the data.
35 ///
36 /// @param size Size of the data in bytes.
37 ///
38 /// @param user_data Optional user data that will be passed to destruction_callback
39 /// when the returned Buffer is destroyed.
40 ///
41 /// @param destruction_callback Optional callback that will be called upon destruction. Pass a
42 /// null pointer if you don't want to be informed of destruction.
43 ///
44 ///
45 /// @return A ref-counted Buffer object that wraps the existing data.
46 ///
47 static RefPtr<Buffer> Create(void* data, size_t size, void* user_data,
48 DestroyBufferCallback destruction_callback);
49
50 ///
51 /// Create a Buffer from existing data, a deep copy of data will be made.
52 ///
53 static RefPtr<Buffer> CreateFromCopy(const void* data, size_t size);
54
55 ///
56 /// Get a pointer to the raw byte data.
57 ///
58 virtual void* data() = 0;
59
60 ///
61 /// Get the size in bytes.
62 ///
63 virtual size_t size() const = 0;
64
65 ///
66 /// Get the user data associated with this Buffer, if any.
67 ///
68 virtual void* user_data() = 0;
69
70 ///
71 /// Check whether this Buffer owns its own data (Buffer was created via CreateFromCopy).
72 /// If this is false, Buffer will call the user-supplied destruction callback to deallocate data
73 /// when this Buffer instance is destroyed.
74 ///
75 virtual bool owns_data() const = 0;
76
77 protected:
79 virtual ~Buffer();
80 Buffer(const Buffer&);
81 void operator=(const Buffer&);
82};
83
84} // namespace ultralight
#define UExport
Definition Defines.h:65
A fixed-size byte container for passing data around.
Definition Buffer.h:28
virtual void * user_data()=0
Get the user data associated with this Buffer, if any.
static RefPtr< Buffer > CreateFromCopy(const void *data, size_t size)
Create a Buffer from existing data, a deep copy of data will be made.
virtual bool owns_data() const =0
Check whether this Buffer owns its own data (Buffer was created via CreateFromCopy).
void operator=(const Buffer &)
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.
virtual size_t size() const =0
Get the size in bytes.
Buffer(const Buffer &)
virtual void * data()=0
Get a pointer to the raw byte data.
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 be optionally called when Buffer is des...
Definition Buffer.h:23