c++opencvboostcastinglexical-cast

Extended boost::lexical_cast for other class datatypes


Is it possible to extend boost::lexical_cast to handle other datatypes, without actually modifying those classes?

In my case, I want to extend it to handle things like cv::Point and cv::Point3, taking a string-separated list of coordinates and loading them.. So the ability to do something like:

cv::Point mypoint = boost::lexical_cast<cv::Point>("2,4");

The cv::Point class already has stream operators, but not compatible with istream and wstream, so it fails.

Edit

I ask this because I'm working in a framework with a templated function get_parameter that uses boost::lexical_cast to convert a string (read from a configuration file) into the desired datatype. It works great for ints & floats, but right now I have to call it multiple times to read a 2D or 3D point (or even worse, arrays of coefficients). It would be nice to be able to modify lexical_cast to handle these cases.

As such, this isn't specific to OpenCV, I just chose that as the simplest datatype.. I'm more interested in the general solution.

Edit 2

Here's a sample app that I've been trying:

#include <opencv2/opencv.hpp>
#include <boost/lexical_cast.hpp>

template <typename T>
std::istream& operator>>(std::istream& stream, cv::Point_<T> &p) {
   // Eventually something will go here
   // to put stream into p
}

int main(int argc, char **argv) {
  cv::Point_<float> p = boost::lexical_cast<cv::Point_<float>>(std::string("1,2"));
  std::cout << "p = " << p << std::endl;
  return 0;
}

And it fails with a beautiful C++ template error like so:

In file included from /home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:41:0,
             from /home/rhand/Development/experiments/lexical_Cast/test.cc:2:
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp: In instantiation of ‘struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<cv::Point_<float> > >’:
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:415:89:   required from ‘struct boost::detail::deduce_target_char<cv::Point_<float> >’
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:674:92:   required from ‘struct boost::detail::lexical_cast_stream_traits<std::basic_string<char>, cv::Point_<float> >’
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:2363:19:   required from ‘static Target boost::detail::lexical_cast_do_cast<Target, Source>::lexical_cast_impl(const Source&) [with Target = cv::Point_<float>; Source = std::basic_string<char>]’
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:2543:50:   required from ‘Target boost::lexical_cast(const Source&) [with Target = cv::Point_<float>; Source = std::basic_string<char>]’
/home/rhand/Development/experiments/lexical_Cast/test.cc:11:82:   required from here
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/static_assert.hpp:31:45: error: static assertion failed: Target type is neither std::istream`able nor std::wistream`able
 #     define BOOST_STATIC_ASSERT_MSG( ... ) static_assert(__VA_ARGS__)
                                         ^
/home/rhand/Development/mlx/ml_3rdparty/install/boost/include/boost/lexical_cast.hpp:388:13: note: in expansion of macro ‘BOOST_STATIC_ASSERT_MSG’
         BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value),
         ^
make[2]: *** [CMakeFiles/test.dir/test.cc.o] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

Solution

  • You can define a specialization of boost::lexical_cast for the types that you want to convert.

    Toy example:

    typedef struct { int x; int y; } Point;
    
    namespace boost {
        template<>
          std::string lexical_cast(const Point& arg) { return "2,3"; }
    }
    
    int main () {
        std::cout << boost::lexical_cast<std::string>(Point ()) << std::endl;
        }
    

    prints 2,3.

    Going from a string to a point requires a bit more work, but you can see how to do it.