Reputation: 1025
My question concerns the mix of boost::variant
conversions with std::vector
in C++. In my project, I use variants as output of SQL. I will always just use 1 type of variant. What I am trying to do is to have an easy conversion from variant, vector of variant or 2xvector of variant to the equivalent type of my choice. Naively, I need something along the lines of:
std::vector < int > my_variable = convert(some_variant_vector)
First here is my setup:
#include "boost\variant.hpp"
#include "boost\date_time\gregorian\gregorian.hpp"
typedef boost::variant< int, std::string, boost::gregorian::date> _var;
typedef std::vector<_var> _vec_var;
typedef std::vector<_vec_var> _vec2_var;
I would like to have a simple way to convert a _var
(or _vec_var
or _vec2_var
) into a int/string/date depending on my needs. From the following post I know that my answer should look something like this:
template<typename T>
struct converter_visitor : public boost::static_visitor<T>
{
const _var &converter;
converter_visitor(const _var &r) : converter(r) {}
T operator()(const _var &) const{
return boost::get<_var>(converter);
}
const _vec_var &v_converter; // case of vector<>
converter_visitor(const _vec_var &r) : v_converter(r) {}
T operator()(const _vec_var &) const{
T ans;
ans.reserve(_cont.size());
for (int i = 0; i < _cont.size(); ++i)
ans.push_back(boost::get<T>(v_converter[i]));
return ans;
}
const _vec2_var & v2_converter; // case of vector<vector>
converter_visitor(const _vec2_var &r) : v2_converter(r) {}
T operator()(const _vec2_var &) const {
T ans;
ans.reserve(v2_converter.size(), v2_converter[0].size());
for (int i = 0; i < _cont.size(); ++i)
{
for (size_t j = 0; j < v2_converter[0].size(); j++)
{
ans.push_back(boost::get<T>(v2_converter[i][j]));
}
}
return ans;
}
};
int main()
{
_var variable = 1;
int integer_conversion;
boost::apply_visitor(converter_visitor(test), integer_conversion);
return 0;
}
Unfortunately I have been stuck in a while with this as it is not working as intended and does not even compile.
Upvotes: 4
Views: 5469
Reputation: 5088
I found a solution to your problem, as I said in the comment, I did the following:
I wrote a bool_visitor<T>
wich returns true
if a boost::variant
holds T
and false
otherwise;
here is the visitor:
template<typename T>
struct bool_visitor : public boost::static_visitor<bool> {
inline constexpr bool operator()(T val) const
{
return false;
}
template<typename K>
typename std::enable_if<!std::is_convertible<K, T>::value, bool>::type
operator()(K val) const
{
return true;
}
};
with this visitor we are able to remove all variants of a vector which doesnt hold a specific type.
After this we use std::transform
to transform all the variants to the type they hold.
We use SFINAE to check if the given vector is a nested vector(vector) or not, if so, we flatten the vector first, with a flatten function:
template <typename FromIter, typename ToIter>
void flatten (FromIter start, FromIter end, ToIter dest) {
while (start != end) {
dest = std::copy(start->begin(), start->end(), dest);
++start;
}
}
to check if the vector is nested we need a is_vector
trait:
template<typename>
struct is_vector : std::false_type {};
template<typename T, typename A>
struct is_vector<std::vector<T,A>> : std::true_type {};
now we can use SFINAE to enable the function we want on a given vector:
template<typename T, typename K>
typename
std::enable_if_t<
!is_vector<K>::value,
std::vector<T>>
get_vec_of(std::vector<K>& input_vec){
//delete all variants not holding T
auto it = std::remove_if(input_vec.begin(), input_vec.end(),
[](auto item){
return boost::apply_visitor(bool_visitor<T>(), item);
});
//input_vec.erase(it, input_vec.end());
//create new vector of T
std::vector<T> return_vec;
//transform all variants holding T to T and put them in the return_vec
std::transform(input_vec.begin(), it, std::back_inserter(return_vec),
[](auto item){
//this can never throw because all variants in the vector are holding T
return boost::get<T>(item);
});
return return_vec;
}
template<typename T, typename K>
typename
std::enable_if_t<
is_vector<K>::value,
std::vector<T>>
get_vec_of(std::vector<K>& input_vec){
std::vector<typename K::value_type> flatten_vec;
flatten(input_vec.begin(), input_vec.end(), std::back_inserter(flatten_vec));
return get_vec_of<T>(flatten_vec);
};
This solution changes the order of the elements inside the given vector. If this is bad for you, you maybe should copy the vector first.
here is a demo on how to use this function, and how it works.
Upvotes: 1
Reputation: 930
I would suggest the following to get the types you want:
template<typename T>
class converter_visitor : public boost::static_visitor<>
{
public:
std::vector<T>& vec;
converter_visitor(std::vector<T>& r) : vec(r) {}
// only push back values of specific types...
void operator()(const T& u) const {
vec.push_back(u);
}
// ignore other types...
void operator()(...) const {}
};
template<typename T>
converter_visitor<T> make_visitor(std::vector<T>& r) { return converter_visitor<T>(r); }
and then funnel that to a recursive filter func that can handle nested vectors:
template<typename T,typename U>
void filter(std::vector<T>& result,const U& var) {
boost::apply_visitor( make_visitor(result), var );
}
template<typename T,typename U>
void filter(std::vector<T>& result,const std::vector<U>& cont) {
std::for_each(cont.begin(),cont.end(),[&](const U& c) {
filter(result,c);
});
}
then you can do:
_var v = 314;
std::vector<int> result;
filter(result,v);
print(result);
result: 314
_vec_var v;
v.push_back(2);
v.push_back(3);
v.push_back("hello");
v.push_back(5);
v.push_back(7);
v.push_back("world");
std::vector<int> result;
filter(result,v);
print(result);
std::vector<std::string> result2;
filter(result2,v);
print(result2);
result1: 2 3 5 7
result2: hello world
_vec_var v1;
v1.push_back(11);
v1.push_back(13);
v1.push_back("see ya");
_vec_var v2;
v2.push_back(17);
v2.push_back(19);
v2.push_back("later");
_vec2_var vv;
vv.push_back(v1);
vv.push_back(v2);
std::vector<int> result;
filter(result,vv);
print(result);
std::vector<std::string> result2;
filter(result2,vv);
print(result2);
result1: 11 13 17 19
result2: see ya later
Upvotes: 5