Reputation: 672
I have a tuple of vectors, I would like to push_back()
each value from an initializer list into the corresponding vector in the "vectors tuple". The create()
function in the code is where I would like to do this.
template<typename...Fields>
class ComponentManager
{
using Index = int;
public:
/**
* Provides a handle to a component
**/
struct ComponentHandle {
static constexpr Index Nil = -1;
bool nil() { return index == Nil; }
const Index index;
};
ComponentHandle lookup(Entity e) {
return ComponentHandle{get(m_map,e,-1)};
}
template<int i>
auto get(ComponentHandle handle) {
return std::get<i>(m_field)[handle.index];
}
ComponentHandle create(Entity e, Fields ...fields) {
m_entity.push_back(e);
// m_fields.push_back ... ???
}
private:
std::vector<Entity> m_entity;
std::tuple<std::vector<Fields>...> m_field;
std::map<Entity,Index> m_map;
};
Example:
Entity e1, e2;
ComponentManager<int,float,int> cm{};
cm.create(e1, 42, 1337.0, 99);
cm.create(e2, 84, 2674.0, 198);
// Resulting contents of cm.m_field tuple
// {
// vector<int> [ 42, 84 ],
// vector<float> [ 1337.0, 2674.0 ]
// vector<int> [ 99, 198 ]
// }
Upvotes: 2
Views: 2633
Reputation: 275350
C++14 solution that implements apply
from C++17 half way down.
template<std::size_t I>
using index_t = std::integral_constant<std::size_t, I>;
template<std::size_t I>
constexpr index_t<I> index{};
template<class=void,std::size_t...Is>
auto index_over( std::index_sequence<Is...> ) {
return [](auto&&f)->decltype(auto) {
return decltype(f)(f)( index<Is>... );
};
}
template<std::size_t N>
auto index_over( index_t<N> ={} ) {
return index_over( std::make_index_sequence<N>{} );
}
template<class F>
auto for_each_arg( F&& f ) {
return [f = std::forward<F>(f)](auto&&...args)->decltype(auto) {
using discard=int[];
(void)discard{0,(void(
f(decltype(args)(args))
),0)...};
};
}
These are useful, but not needed:
template<class F, class Tuple>
decltype(auto) apply( F&& f, Tuple&& tuple ) {
auto count = index< std::tuple_size< std::decay_t<Tuple> >{} >;
return index_over( count )( [&](auto...Is) {
using std::get;
return std::forward<F>(f)( get<decltype(Is)::value>( std::forward<Tuple>(tuple) )... );
} );
}
template<class Tuple>
auto for_each_tuple_element(Tuple&& tuple) {
return [&](auto&& f)->decltype(auto){
return apply(
for_each_arg( decltype(f)(f) ),
std::forward<Tuple>(tuple)
);
};
}
test code:
int main() {
std::tuple< std::vector<int>, std::vector<char> > tup;
for_each_tuple_element( tup )( [](auto&& v) {
v.push_back(3);
});
std::cout << std::get<0>(tup).size() << "," << std::get<1>(tup).size() << "\n";
}
Then we can apply it to your problem.
ComponentHandle create(Entity e, Fields ...fields) {
m_entity.push_back(e);
auto indexer = index_over<sizeof...(fields>();
auto fields_tuple = std::forward_as_tuple( std::forward<Fields>(fields)... );
indexer( for_each_arg([&](auto Is){
std::get<Is>(m_fields).push_back(
std::get<Is>(decltype(fields_tuple)(fields_tuple))
);
} );
}
index_over
takes a compile-time N and returns a lambda. This lambda takes a callable, and calls it with index_t<0>
through index_t<N-1>
.
for_each_arg
takes a callable, and returns a lambda that takes any number of arguments. It calls the callable with each of those arguments in turn.
Stitched together we take our Fields...fields
, build an index_over
it to get us a compile-time set of indexes into it. We then store those Fields in a tuple
of r and l value references.
We write an operation on a single index Is
. We then pass that to for_each_arg
, and pass the return value to the index_over
, and get the single index-handling lambda to be called for each index.
Some compilers don't permit a non-constexpr
std::integral_constant
to convert to a scalar in a constexpr
context. They are wrong and/or obsolete. For those, you'll have to do decltype(Is)::value
.
Upvotes: 1
Reputation: 302787
It may not be readily obvious, but the way to unpack a tuple in C++ is to use std::apply()
. With C++17, this is as easy as:
void create(Entity e, Fields ...fields) {
m_entity.push_back(e);
std::apply([&](auto&... vs) {
(vs.push_back(fields), ...);
}, m_field);
}
With C++14, I'd suggest implementing apply()
yourself anyway (it's a short function), and then you need to use the expander trick instead of using fold-expressions:
void create(Entity e, Fields ...fields) {
m_entity.push_back(e);
not_std::apply([&](auto&... vs) {
using swallow = int[];
(void)swallow{0,
(vs.push_back(fields), 0)...
};
}, m_field);
}
With C++11, most of the same applies, except we can't use generic lambdas and implementing std::apply
is more verbose (but not more complicated) than in the linked reference. Thankfully, we don't actually need to for anything other than making the code shorter - we know all the vector types:
void create(Entity e, Fields ...fields) {
m_entity.push_back(e);
not_std::apply([&](std::vector<Fields>&... vs) {
using swallow = int[];
(void)swallow{0,
(vs.push_back(fields), 0)...
};
}, m_field);
}
Upvotes: 3
Reputation: 93264
template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
(void) std::initializer_list<int>{(f(std::forward<Args>(args)), 0)...};
}
ComponentHandle create(Entity e, Fields ...fields)
{
for_each_argument([&](auto field)
{
using field_type = std::vector<std::decay_t<decltype(field)>>;
std::get<field_type>(m_field).push_back(field);
},
fields...);
}
I didn't mention in the question that it is a requirement that the fields can be the same type, so there can be several vector's in the tuple for example.
template <typename TVec, typename TFieldTuple, std::size_t... TIdxs>
void expander(TVec& vec, TFieldTuple ft, std::index_sequence<TIdxs...>)
{
for_each_argument([&](auto idx)
{
std::get<idx>(vec).push_back(std::get<idx>(ft));
}, std::integral_constant<std::size_t, TIdxs>{}...);
}
const auto create = [](auto& vec, auto ...fields)
{
expander(vec,
std::make_tuple(fields...),
std::make_index_sequence<sizeof...(fields)>());
};
Upvotes: 2