Go to the documentation of this file.00001 #ifndef CROSSING_DETECTOR_WRAPPER_UTILS_H
00002 #define CROSSING_DETECTOR_WRAPPER_UTILS_H
00003
00004 #include <string>
00005
00006 #include <boost/python.hpp>
00007
00008 #include <ros/serialization.h>
00009
00010 namespace rs = ros::serialization;
00011
00012
00013 template<class T>
00014 struct vector_to_python
00015 {
00016 static PyObject* convert(const std::vector<T>& vec)
00017 {
00018 boost::python::list* l = new boost::python::list();
00019 for(std::size_t i = 0; i < vec.size(); i++)
00020 (*l).append(vec[i]);
00021
00022 return l->ptr();
00023 }
00024 };
00025
00026
00027
00028 template <typename M>
00029 M from_python(const std::string str_msg)
00030 {
00031 size_t serial_size = str_msg.size();
00032 boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00033 for (size_t i = 0; i < serial_size; ++i)
00034 {
00035 buffer[i] = str_msg[i];
00036 }
00037 rs::IStream stream(buffer.get(), serial_size);
00038 M msg;
00039 rs::Serializer<M>::read(stream, msg);
00040 return msg;
00041 }
00042
00043
00044
00045 template <typename M>
00046 std::string to_python(const M& msg)
00047 {
00048 size_t serial_size = rs::serializationLength(msg);
00049 boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00050 rs::OStream stream(buffer.get(), serial_size);
00051 rs::serialize(stream, msg);
00052 std::string str_msg;
00053 str_msg.reserve(serial_size);
00054 for (size_t i = 0; i < serial_size; ++i)
00055 {
00056 str_msg.push_back(buffer[i]);
00057 }
00058 return str_msg;
00059 }
00060
00061 #endif // CROSSING_DETECTOR_WRAPPER_UTILS_H
00062
00063