Skip to content
Snippets Groups Projects
Commit 446236e1 authored by Nikita Pettik's avatar Nikita Pettik Committed by Nikita Pettik
Browse files

memtx: introduce foreach_allocator template walker

It is supposed to generalize workflow with allocators so that apply the
same function (like create()/destroy() etc) for each existing allocator.
Newborn functions are not used yet since we are going to add more
wrappers.

Warning: dark template magic is involved, do not try what you're about
to see at home.

Follow-up #5419
parent 96793697
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,8 @@
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <tuple>
#include <small/small.h>
#include "sysalloc.h"
......@@ -161,3 +163,47 @@ class SysAlloc
private:
static struct sys_alloc sys_alloc;
};
using allocators = std::tuple<SmallAlloc, SysAlloc>;
template <class F, class... ARGS>
static void
foreach_allocator_internal(std::tuple<>*, F&, ARGS && ...)
{
}
template <class ALL, class... MORE, class F, class... ARGS>
static void
foreach_allocator_internal(std::tuple<ALL, MORE...>*, F& f, ARGS && ...args)
{
f.template invoke<ALL>(std::forward<ARGS>(args)...);
foreach_allocator_internal((std::tuple<MORE...> *) nullptr,
f, std::forward<ARGS>(args)...);
}
template<class F, class... ARGS>
static void
foreach_allocator(ARGS && ...args)
{
F f;
foreach_allocator_internal((allocators *) nullptr, f,
std::forward<ARGS>(args)...);
}
struct allocator_create {
template<typename Allocator, typename...Arg>
void
invoke(Arg&&...settings)
{
Allocator::create(settings...);
}
};
struct allocator_destroy {
template<typename Allocator, typename...Arg>
void
invoke(Arg&&...)
{
Allocator::destroy();
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment