ruby/prism/arena.hpp
Kevin Newton 957a081892 [ruby/prism] Ship an official C++ interface
The C++ interface is intentionally minimal: it provides only a
couple of niceties in order to largely stay out of the way. It is
mostly used to provide:

* RAII for opaque resources
* Safe up and down casting for nodes
* string_view for various resources
* Iteration over resource lists

Note that if consumers want to use it, they must be on C++17 at
minimum.

https://github.com/ruby/prism/commit/57b8dbe0e9
2026-10-20 22:15:29 +00:00

43 lines
806 B
C++

/**
* @file arena.hpp
*
* Ownership of arenas from C++.
*/
#ifndef PRISM_ARENA_HPP
#define PRISM_ARENA_HPP
#include "prism/compiler/cplusplus.hpp"
#include "prism/arena.h"
#include <memory>
namespace prism {
/** Frees an arena with pm_arena_free. */
struct arena_deleter {
/**
* Frees the given arena.
*
* @param arena The arena to free.
*/
void operator()(pm_arena_t *arena) const noexcept { pm_arena_free(arena); }
};
/** An owned arena. */
using unique_arena = std::unique_ptr<pm_arena_t, arena_deleter>;
/**
* Allocate a new arena. Allocation failure aborts, so this never throws.
*
* @return A unique_arena managing the newly allocated arena.
*/
[[nodiscard]] inline unique_arena
make_arena() noexcept {
return unique_arena(pm_arena_new());
}
}
#endif