How to convert boost::Makevariantover to vector

156 Views Asked by At

I have a boost::makevariantover. How can I convert that to a vector . Any samples will be helpful using boost::apply_visitor

class pixel_visitor
    : public boost::static_visitor<>
{

public:

template <typename T>
    void operator() (const ome::compat::shared_ptr<PixelBuffer<T> >& v)
    {
      std::cout << std::real(v);
    }    
};

pixelBuffer test= buf.vbuffer();    
test.apply_visitor(&pixel_visitor());

where

typedef boost::make_variant_over<pixel_buffer_types>::type     pixelBuffer;
2

There are 2 best solutions below

7
sehe On BEST ANSWER

We don't know what your pixelbuffer types are.

Regardless, if you know how to convert those to vectors, you could simply return that from the visitor's call operator:

class pixel_visitor
    : public boost::static_visitor<std::vector<WhatEverYouWant> >
{

public:

template <typename T>
    result_type operator() (const ome::compat::shared_ptr<PixelBuffer<T> >& v)
    {
         std::vector<WhatEverYouWant> result;
         // do your conversion here
         return result;
    }    
};

So you can e.g.

std::vector<WhatEverYouWant> applied;

applied = boost_apply_visitor(pixel_visitor(), test);
2
VolkerK On

Instead of .vbuffer() call .array(), it already applies the/a visitor "for you":

template<typename T>
inline typename PixelBuffer<T>::array_ref_type&
VariantPixelBuffer::array()
{
  detail::VariantPixelBufferVisitor<T> v;
  return boost::apply_visitor(v, buffer).array();
}

or .data() for the raw type.