FuncSketch
Loading...
Searching...
No Matches
expression_memory_pool.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <memory_resource>
23#include <type_traits>
24#include <utility>
25#include <variant>
26
28
29namespace func_sketch::expressions {
30
37public:
43
52 template <typename T, typename... Args>
53 [[nodiscard]] Expression* create(Args&&... args) {
54 return allocator_.new_object<Expression>(
55 T(std::forward<Args>(args)...));
56 }
57
68 void destroy(Expression* expression) noexcept { // NOLINT(*-exception-*)
69 if (expression == nullptr) {
70 return;
71 }
72
73 // Remove child expressions.
74 std::visit(
75 [this](auto& expr) noexcept {
76 using ExpressionType = std::decay_t<decltype(expr)>;
77 if constexpr (std::is_same_v<ExpressionType, UnaryExpression>) {
78 destroy(expr.target);
79 } else if constexpr (std::is_same_v<ExpressionType,
81 destroy(expr.left);
82 destroy(expr.right);
83 } else if constexpr (std::is_same_v<ExpressionType,
85 for (auto& arg : expr.arguments) {
86 destroy(arg);
87 }
88 }
89 },
90 expression->as_variant());
91
92 allocator_.delete_object(expression);
93 }
94
95private:
97 std::pmr::monotonic_buffer_resource memory_resource_;
98
100 std::pmr::polymorphic_allocator<Expression> allocator_;
101};
102
103} // namespace func_sketch::expressions
Expression * create(Args &&... args)
Create an expression.
std::pmr::monotonic_buffer_resource memory_resource_
Memory resource.
void destroy(Expression *expression) noexcept
Destroy an expression including its child expressions.
std::pmr::polymorphic_allocator< Expression > allocator_
Allocator of expressions.
Definition of Expression class.