cv::FileStorage does not work for std::vector of user-defined struct (Bug #3599)
Description
cv::FileStorage
supports writing std::vector<T>
and allows extending operator<<
to accept user-defined struct like:
// excerpted from opencv/samples/cpp/filestorage.cpp struct MyData { int A; double X; string id; void write(FileStorage& fs) const //Write serialization for this class { fs << "{" << "A" << A << "X" << X << "id" << id << "}"; } }; static void write(FileStorage& fs, const std::string&, const MyData& x){ x.write(fs); }
So I expected vector<MyData>
can be written by adding overloading for MyData
, but it does not work. More precisely, when we use operator<<
to implement write(FileStorage&, const std::string&, const MyData&)
, it cannot be an element of vector.
I'm using following workaround. With this modification, attached code will work expectedly. But this workaround changes the behaviour of modified function. I think it is safe if this function is always called via operator<<
and not directly from user code.
// in file opencv/modules/core/include/opencv2/core/persistence.hpp // at line 697 template<typename _Tp> static inline void write( FileStorage& fs, const String& name, const std::vector<_Tp>& vec ) { - internal::WriteStructContext ws(fs, name, FileNode::SEQ+(DataType<_Tp>::fmt != 0 ? FileNode::FLOW : 0)); + fs << (DataType<_Tp>::fmt != 0 ? "[:" : "["); write(fs, vec); + fs << "]"; }
Associated revisions
Merge pull request #3599 from a-wi:DShow_COM_interfaces_v3
History
Updated by Ivan Korolev almost 12 years ago
Hi Koji,
thanks for bug report.
We would appreciate if you could describe this change in the form of pull request, so it could be reviewed and merged.
Here are some instructions on how to do it: http://www.code.opencv.org/projects/opencv/wiki/How_to_contribute.
- Assignee changed from Vadim Pisarevsky to Koji Miyazato
- Status changed from New to Open
Updated by Koji Miyazato almost 11 years ago
Thanks Ivan, I sent pull request
https://github.com/Itseez/opencv/pull/2476
Updated by Daniil Osokin almost 11 years ago
Koji, thanks again for PR!
- Pull request set to https://github.com/Itseez/opencv/pull/2476
Updated by Dmitry Retinskiy over 10 years ago
- Status changed from Open to Done