cv::FileStorage does not work for std::vector of user-defined struct (Bug #3599)


Added by Koji Miyazato about 11 years ago. Updated over 10 years ago.


Status:Done Start date:2014-03-10
Priority:Normal Due date:
Assignee:Koji Miyazato % Done:

0%

Category:core
Target version:3.0
Affected version:branch 'master' (3.0-dev) Operating System:Any
Difficulty: HW Platform:Any
Pull request:https://github.com/Itseez/opencv/pull/2476

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 << "]";
 }

filestorage_mod.cpp - modified sample source code to test FileStorage << vector<T> (5 kB) Koji Miyazato, 2014-03-10 07:18 am


Associated revisions

Revision 61f11114
Added by Vadim Pisarevsky about 10 years ago

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

Also available in: Atom PDF