Files
snipplib/include/snipplib/containers/snplib_hvec.h
Timur A. Fatkhullin 6389be046b ...
2026-06-30 01:11:09 +03:00

158 lines
3.9 KiB
C++

#pragma once
#include <functional>
#include <unordered_map>
#include "snipplib/concepts/snplib_concepts.h"
namespace snplib
{
namespace details
{
template <typename OT, typename TT, size_t... Is>
concept snplib_all_func_oper_c = requires(OT obj) { (obj(std::declval<std::tuple_element_t<Is, TT>&>()), ...); };
template <typename OT, typename TT>
struct snplib_check_all_func_oper_t {
static constexpr bool value = false;
};
template <typename OT, typename TT>
requires requires { typename std::tuple_size<TT>::type; }
struct snplib_check_all_func_oper_t<OT, TT> {
static constexpr bool value = []<std::size_t... Is>(std::index_sequence<Is...>) {
return snplib_all_func_oper_c<OT, TT, Is...>;
}(std::make_index_sequence<std::tuple_size<TT>::value>{});
};
template <typename OT, typename TT>
static constexpr bool snplib_check_all_func_oper_v = snplib_check_all_func_oper_t<OT, TT>::value;
} // namespace details
template <typename T>
concept snplib_hvec_visitor_c = requires {
requires snplib_tuple_c<typename T::types>;
requires details::snplib_check_all_func_oper_v<T, typename T::types>;
};
class HeterogenVec
{
protected:
template <typename VT>
inline static std::unordered_map<const HeterogenVec*, std::vector<VT>> _values{};
inline static std::vector<std::function<void(const HeterogenVec*)>> _clearFunc{};
// move: void(from_obj_ptr, to_obj_ptr)
inline static std::vector<std::function<void(HeterogenVec*, HeterogenVec*)>> _moveFunc{};
// copy: void(const from_obj_ptr, to_obj_ptr)
inline static std::vector<std::function<void(const HeterogenVec*, HeterogenVec*)>> _copyFunc{};
public:
HeterogenVec() = default;
~HeterogenVec() = default;
HeterogenVec(const HeterogenVec& other)
{
if (&other != this) {
for (auto& func : _copyFunc) {
func(&other, this);
}
}
}
HeterogenVec(HeterogenVec&& other)
{
if (&other != this) {
for (auto& func : _moveFunc) {
func(&other, this);
}
}
}
HeterogenVec& operator=(const HeterogenVec& other)
{
if (&other != this) {
for (auto& func : _copyFunc) {
func(&other, this);
}
}
return *this;
}
HeterogenVec& operator=(HeterogenVec&& other)
{
if (&other != this) {
for (auto& func : _moveFunc) {
func(&other, this);
}
}
return *this;
}
void clear()
{
for (auto& func : _clearFunc) {
func(this);
}
}
template <typename VT>
void push(VT&& value)
{
using v_t = std::decay_t<VT>;
if (_values<v_t>.empty()) {
_clearFunc.emplace_back([](const HeterogenVec* obj) {
// _values<v_t>[obj].clear();
_values<v_t>.erase(obj);
});
_moveFunc.emplace_back([](HeterogenVec* from, HeterogenVec* to) {
if (_values<v_t>.contains(from)) {
_values<v_t>[to] = std::move(_values<v_t>[from]);
}
});
_copyFunc.emplace_back([](HeterogenVec* from, HeterogenVec* to) {
if (_values<v_t>.contains(from)) {
_values<v_t>[to] = _values<v_t>[from];
}
});
}
_values<v_t>[this].push_back(std::forward<VT>(value));
}
template <snplib_hvec_visitor_c VT>
void visit(VT&& visitor)
{
auto invoke_per_type = [&visitor, this]<typename T>() {
for (auto& elem : _values<T>[this]) {
visitor(elem);
}
};
[&invoke_per_type]<size_t... Is>(std::index_sequence<Is...>) {
(invoke_per_type.template operator()<std::tuple_element_t<Is, typename VT::types>>(), ...);
}(std::make_index_sequence<std::tuple_size_v<typename VT::types>>{});
}
};
} // end of namespace snplib