FuncSketch
Loading...
Searching...
No Matches
expression_ptr.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>
23
26
27namespace func_sketch::expressions {
28
36public:
47 std::unique_ptr<ExpressionMemoryPool> memory_pool) noexcept
48 : expression_(expression, Deleter(std::move(memory_pool))) {}
49
53 ExpressionPtr() noexcept = default;
54
58 void reset() noexcept {
59 expression_ = std::unique_ptr<Expression, Deleter>(nullptr, Deleter());
60 }
61
67 [[nodiscard]] Expression* get() const noexcept { return expression_.get(); }
68
74 [[nodiscard]] Expression& operator*() const noexcept {
75 return *expression_;
76 }
77
83 [[nodiscard]] Expression* operator->() const noexcept {
84 return expression_.get();
85 }
86
87private:
89 class Deleter {
90 public:
96 explicit Deleter(
97 std::unique_ptr<ExpressionMemoryPool> memory_pool) noexcept
98 : memory_pool_(std::move(memory_pool)) {}
99
103 Deleter() noexcept = default;
104
110 void operator()(Expression* expression) const noexcept {
111 if (expression != nullptr && memory_pool_ != nullptr) {
112 memory_pool_->destroy(expression);
113 }
114 }
115
116 private:
118 std::unique_ptr<ExpressionMemoryPool> memory_pool_;
119 };
120
122 std::unique_ptr<Expression, Deleter> expression_;
123};
124
125} // namespace func_sketch::expressions
std::unique_ptr< ExpressionMemoryPool > memory_pool_
Memory pool.
Deleter() noexcept=default
Constructor.
Deleter(std::unique_ptr< ExpressionMemoryPool > memory_pool) noexcept
Constructor.
ExpressionPtr(Expression *expression, std::unique_ptr< ExpressionMemoryPool > memory_pool) noexcept
Constructor.
ExpressionPtr() noexcept=default
Constructor.
Expression * operator->() const noexcept
Access the member of the pointed expression.
void reset() noexcept
Clear the pointer.
Expression * get() const noexcept
Get the pointed expression.
Expression & operator*() const noexcept
Access the pointed expression.
std::unique_ptr< Expression, Deleter > expression_
Pointer.
Definition of Expression class.
Definition of ExpressionMemoryPool class.