summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/allocator_op.hpp
blob: 90928002e37b14252ed509aa4b9091e2bd9f5de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * File: allocator_op.hpp
 * Purpose: Allocator Operations container.
 * Author: Amlal El Mahrouss (amlal@nekernel.org)
 * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. Licensed under the BSL 1.0 license
 */

#ifndef __OCL_CORE_ALLOC
#define __OCL_CORE_ALLOC

#include <ocl/detail/config.hpp>
#include <memory>

namespace ocl
{
	/// @note these are guidelines on allocating a resource
	template <typename type>
	struct global_new_op final
	{
		inline auto operator()() -> type*
		{
			return new type;
		}

		template <typename... var_type>
		inline auto var_alloc(var_type&&... args) -> type*
		{
			return new type{std::forward<var_type>(args)...};
		}
	};

	/// @note these are guidelines on deleting a resource
	template <typename type>
	struct global_delete_op final
	{
		inline auto operator()(type* t) -> void
		{
			delete t;
		}
	};

	template <typename ret_type, typename allocator_new, typename allocator_delete>
	class allocator_op
	{
		allocator_new	 alloc_op_{};
		allocator_delete free_op_{};

	public:
		allocator_op()	= default;
		~allocator_op() = default;

		allocator_op& operator=(const allocator_op&) = delete;
		allocator_op(const allocator_op&)			 = delete;

		template <typename... var_type>
		auto construct(var_type&&... args) -> std::shared_ptr<ret_type>
		{
			return std::shared_ptr<ret_type>(alloc_op_.template var_alloc<var_type...>(args...), free_op_);
		}
	};

	template <typename type>
	using allocator = allocator_op<type, global_new_op<type>, global_delete_op<type>>;
} // namespace ocl

#endif // ifndef __OCL_CORE_ALLOC