mdds
Loading...
Searching...
No Matches
block_funcs.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2022 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include "./types.hpp"
10#include "../global.hpp"
11
12#include <unordered_map>
13#include <functional>
14#include <sstream>
15#include <concepts>
16
17namespace mdds { namespace mtv {
18
19namespace detail {
20
21inline void throw_unknown_block(const char* func, const mdds::mtv::element_t type)
22{
23 std::ostringstream os;
24 os << func << ": failed to map to a element block function (type=" << type << ")";
25 throw general_error(os.str());
26}
27
28template<typename Ret, typename... Args>
29auto& find_func(
30 const std::unordered_map<element_t, std::function<Ret(Args...)>>& func_map, element_t type,
31 const char* src_func_name)
32{
33 auto it = func_map.find(type);
34 if (it == func_map.end())
35 detail::throw_unknown_block(src_func_name, type);
36
37 return it->second;
38}
39
40} // namespace detail
41
42template<typename... Ts>
44{
45 static base_element_block* create_new_block(element_t type, std::size_t init_size)
46 {
47 static const std::unordered_map<element_t, std::function<base_element_block*(std::size_t)>> func_map{
48 {Ts::block_type, Ts::create_block}...};
49
50 auto& f = detail::find_func(func_map, type, __func__);
51 return f(init_size);
52 }
53
54 static base_element_block* copy_block(const base_element_block& block)
55 {
56 static const std::unordered_map<element_t, std::function<base_element_block*(const base_element_block&)>>
57 func_map{{Ts::block_type, Ts::copy_block}...};
58
59 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
60 return f(block);
61 }
62
72 template<typename... Passed>
73 static base_element_block* clone_block(const base_element_block& block, Passed&... passed)
74 {
75 static const std::unordered_map<
76 element_t, std::function<base_element_block*(const base_element_block&, Passed&...)>>
77 func_map{{Ts::block_type, &Ts::template clone_block<Passed...>}...};
78
79 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
80 return f(block, passed...);
81 }
82
83 static bool is_copyable(const base_element_block& block)
84 {
85 // Compile-time copyability per block type; a plain bool map, so it does
86 // not go through detail::find_func (which is typed for std::function maps).
87 static const std::unordered_map<element_t, bool> func_map{{Ts::block_type, std::copy_constructible<Ts>}...};
88
89 auto it = func_map.find(get_block_type(block));
90 if (it == func_map.end())
91 detail::throw_unknown_block(__func__, get_block_type(block));
92
93 return it->second;
94 }
95
96 static void delete_block(const base_element_block* p)
97 {
98 if (!p)
99 return;
100
101 static const std::unordered_map<element_t, std::function<void(const base_element_block*)>> func_map{
102 {Ts::block_type, Ts::delete_block}...};
103
104 // This gets called from a destructor, so an unknown block type only
105 // asserts.
106 auto it = func_map.find(get_block_type(*p));
107 if (it == func_map.end())
108 {
109 assert(!"delete_block: failed to map to an element block function");
110 return;
111 }
112
113 it->second(p);
114 }
115
116 static void resize_block(base_element_block& block, std::size_t new_size)
117 {
118 static const std::unordered_map<element_t, std::function<void(base_element_block&, std::size_t)>> func_map{
119 {Ts::block_type, Ts::resize_block}...};
120
121 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
122 f(block, new_size);
123 }
124
125 static void print_block(const base_element_block& block)
126 {
127 static const std::unordered_map<element_t, std::function<void(const base_element_block&)>> func_map{
128 {Ts::block_type, Ts::print_block}...};
129
130 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
131 f(block);
132 }
133
134 static void erase(base_element_block& block, std::size_t pos)
135 {
136 static const std::unordered_map<element_t, std::function<void(base_element_block&, std::size_t)>> func_map{
137 {Ts::block_type, Ts::erase_value}...};
138
139 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
140 f(block, pos);
141 }
142
143 static void erase(base_element_block& block, std::size_t pos, std::size_t size)
144 {
145 static const std::unordered_map<element_t, std::function<void(base_element_block&, std::size_t, std::size_t)>>
146 func_map{{Ts::block_type, Ts::erase_values}...};
147
148 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
149 f(block, pos, size);
150 }
151
152 static void append_block(base_element_block& dest, const base_element_block& src)
153 {
154 static const std::unordered_map<element_t, std::function<void(base_element_block&, const base_element_block&)>>
155 func_map{{Ts::block_type, Ts::append_block}...};
156
157 auto& f = detail::find_func(func_map, get_block_type(dest), __func__);
158 f(dest, src);
159 }
160
161 static void append_values_from_block(
162 base_element_block& dest, const base_element_block& src, std::size_t begin_pos, std::size_t len)
163 {
164 using func_type = std::function<void(base_element_block&, const base_element_block&, std::size_t, std::size_t)>;
165 static const std::unordered_map<element_t, func_type> func_map{
166 {Ts::block_type, Ts::append_values_from_block}...};
167
168 auto& f = detail::find_func(func_map, get_block_type(dest), __func__);
169 f(dest, src, begin_pos, len);
170 }
171
172 static void assign_values_from_block(
173 base_element_block& dest, const base_element_block& src, std::size_t begin_pos, std::size_t len)
174 {
175 using func_type = std::function<void(base_element_block&, const base_element_block&, std::size_t, std::size_t)>;
176 static const std::unordered_map<element_t, func_type> func_map{
177 {Ts::block_type, Ts::assign_values_from_block}...};
178
179 auto& f = detail::find_func(func_map, get_block_type(dest), __func__);
180 f(dest, src, begin_pos, len);
181 }
182
183 static void prepend_values_from_block(
184 base_element_block& dest, const base_element_block& src, std::size_t begin_pos, std::size_t len)
185 {
186 using func_type = std::function<void(base_element_block&, const base_element_block&, std::size_t, std::size_t)>;
187 static const std::unordered_map<element_t, func_type> func_map{
188 {Ts::block_type, Ts::prepend_values_from_block}...};
189
190 auto& f = detail::find_func(func_map, get_block_type(dest), __func__);
191 f(dest, src, begin_pos, len);
192 }
193
194 static void swap_values(
195 base_element_block& blk1, base_element_block& blk2, std::size_t pos1, std::size_t pos2, std::size_t len)
196 {
197 element_t blk1_type = get_block_type(blk1);
198 assert(blk1_type == get_block_type(blk2));
199
200 using func_type =
201 std::function<void(base_element_block&, base_element_block&, std::size_t, std::size_t, std::size_t)>;
202 static const std::unordered_map<element_t, func_type> func_map{{Ts::block_type, Ts::swap_values}...};
203
204 auto& f = detail::find_func(func_map, blk1_type, __func__);
205 f(blk1, blk2, pos1, pos2, len);
206 }
207
208 static bool equal_block(const base_element_block& left, const base_element_block& right)
209 {
210 element_t block_type = get_block_type(left);
211 if (block_type != get_block_type(right))
212 return false;
213
214 using func_type = std::function<bool(const base_element_block&, const base_element_block&)>;
215 static const std::unordered_map<element_t, func_type> func_map{{Ts::block_type, Ts::equal_block}...};
216
217 auto& f = detail::find_func(func_map, block_type, __func__);
218 return f(left, right);
219 }
220
221 static void overwrite_values(base_element_block& block, std::size_t pos, std::size_t len)
222 {
223 using func_type = std::function<void(base_element_block&, std::size_t, std::size_t)>;
224 static const std::unordered_map<element_t, func_type> func_map{{Ts::block_type, Ts::overwrite_values}...};
225
226 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
227 f(block, pos, len);
228 }
229
230 static void shrink_to_fit(base_element_block& block)
231 {
232 using func_type = std::function<void(base_element_block&)>;
233 static const std::unordered_map<element_t, func_type> func_map{{Ts::block_type, Ts::shrink_to_fit}...};
234
235 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
236 f(block);
237 }
238
239 static std::size_t size(const base_element_block& block)
240 {
241 using func_type = std::function<std::size_t(const base_element_block&)>;
242 static const std::unordered_map<element_t, func_type> func_map{{Ts::block_type, Ts::size}...};
243
244 auto& f = detail::find_func(func_map, get_block_type(block), __func__);
245 return f(block);
246 }
247};
248
249}} // namespace mdds::mtv
250
251/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition types.hpp:231
Definition block_funcs.hpp:44
static base_element_block * clone_block(const base_element_block &block, Passed &... passed)
Definition block_funcs.hpp:73