Prism Ruby parser
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1
6#ifndef PRISM_BUFFER_HPP
7#define PRISM_BUFFER_HPP
8
9#include "prism/compiler/cplusplus.hpp"
10#include "prism/compiler/lifetimebound.hpp"
11
12#include "prism/buffer.h"
13
14#include <memory>
15#include <string_view>
16
17namespace prism {
18
26 void operator()(pm_buffer_t *buffer) const noexcept { pm_buffer_free(buffer); }
27};
28
30using unique_buffer = std::unique_ptr<pm_buffer_t, buffer_deleter>;
31
37[[nodiscard]] inline unique_buffer
38make_buffer() noexcept {
40}
41
48[[nodiscard]] inline std::string_view
49view(const pm_buffer_t *buffer PRISM_LIFETIMEBOUND) noexcept {
50 return std::string_view(pm_buffer_value(buffer), pm_buffer_length(buffer));
51}
52
53}
54
55#endif
A wrapper around a contiguous block of allocated memory.
struct pm_buffer_t pm_buffer_t
A wrapper around a contiguous block of allocated memory.
Definition buffer.h:22
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void)
Allocate and initialize a new buffer.
PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer) PRISM_NONNULL(1)
Return the length of the buffer.
PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer) PRISM_NONNULL(1)
Return the value of the buffer.
PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer) PRISM_NONNULL(1)
Free both the memory held by the buffer and the buffer itself.
unique_buffer make_buffer() noexcept
Allocate a new empty buffer.
Definition buffer.hpp:38
std::unique_ptr< pm_buffer_t, buffer_deleter > unique_buffer
An owned buffer.
Definition buffer.hpp:30
std::string_view view(const pm_buffer_t *buffer) noexcept
The bytes written to the given buffer so far.
Definition buffer.hpp:49
Frees a buffer with pm_buffer_free.
Definition buffer.hpp:20
void operator()(pm_buffer_t *buffer) const noexcept
Frees the given buffer.
Definition buffer.hpp:26