[serialization] Sending user data to serialize()?
Hi, I have a complex case I'm trying to work with. I may be doing this completely wrong so if there is a better design for this I'm always open for that. However for now I'm going to assume my particular problem can be solved. There exists two classes: A and B. Class A is defined as such: class A { public: template< typename Archive > void load( Archive& archive, unsigned int ) { archive & m_listOfB; } private: std::vector<int> m_listOfInt; boost::ptr_vector<B> m_listOfB; }; And class B is defined roughly as: class B { public: template< typename Archive > void load( Archive& archive, unsigned int ) { // Code isn't important here } }; Here's the problem: For each B that is deserialized by A::load(), I need to pass in a reference to m_listOfInt. So, essentially the load() function in B would look something like this: void load( Archive& archive, unsigned int, std::vector<int>& listOfInt ) { } I was hoping boost.bind could be used somewhere, but I just don't see how. One way to solve the problem is to call B::load() explicitly with the extra parameter, but I don't think that's an appropriate solution. Any ideas on how to solve this? Or perhaps I'm trying to solve the wrong problem.
I hate to pester the community but I am really blocked on this. Help would
be very much appreciated.
Thanks again to everyone for reading.
On Tue, Feb 3, 2009 at 9:30 AM, Robert Dailey
Hi,
I have a complex case I'm trying to work with. I may be doing this completely wrong so if there is a better design for this I'm always open for that. However for now I'm going to assume my particular problem can be solved.
There exists two classes: A and B. Class A is defined as such:
class A { public: template< typename Archive > void load( Archive& archive, unsigned int ) { archive & m_listOfB; }
private: std::vector<int> m_listOfInt; boost::ptr_vector<B> m_listOfB; };
And class B is defined roughly as:
class B { public: template< typename Archive > void load( Archive& archive, unsigned int ) { // Code isn't important here } };
Here's the problem:
For each B that is deserialized by A::load(), I need to pass in a reference to m_listOfInt. So, essentially the load() function in B would look something like this:
void load( Archive& archive, unsigned int, std::vector<int>& listOfInt ) { }
I was hoping boost.bind could be used somewhere, but I just don't see how. One way to solve the problem is to call B::load() explicitly with the extra parameter, but I don't think that's an appropriate solution. Any ideas on how to solve this? Or perhaps I'm trying to solve the wrong problem.
Robert Dailey wrote:
Hi,
I have a complex case I'm trying to work with. I may be doing this completely wrong so if there is a better design for this I'm always open for that. However for now I'm going to assume my particular problem can be solved.
There exists two classes: A and B. Class A is defined as such:
class A { public: template< typename Archive > void load( Archive& archive, unsigned int ) { archive & m_listOfB; }
private: std::vector<int> m_listOfInt; boost::ptr_vector<B> m_listOfB; };
And class B is defined roughly as:
class B { public: template< typename Archive > void load( Archive& archive, unsigned int ) { // Code isn't important here } };
Here's the problem:
For each B that is deserialized by A::load(), I need to pass in a reference to m_listOfInt. So, essentially the load() function in B would look something like this:
void load( Archive& archive, unsigned int, std::vector<int>& listOfInt ) { }
I was hoping boost.bind could be used somewhere, but I just don't see how. One way to solve the problem is to call B::load() explicitly with the extra parameter, but I don't think that's an appropriate solution. Any ideas on how to solve this? Or perhaps I'm trying to solve the wrong problem.
What's wrong with:
class A { public: template< typename Archive > void load( Archive& archive, unsigned int ) { archive & m_listOfInt; archive & m_listOfB; }
private: std::vector<int> m_listOfInt; boost::ptr_vector<B> m_listOfB; };
I'm guessing that the missing B::load code IS important. Jeff
Probably trying to solve the wrong problem. Anyway try this: template< typename Archive > void A::save( Archive& archive, unsigned int ) const { archive & m_listOfB; ar << m_listOfB.size(); for(i = 0; i < s; ++i) ar << m_listOfB[i] } template< typename Archive > void A::load( Archive& archive, unsigned int ) { unsigned int s; ar >> s; m_listOfB.reserve(s); for(i = 0; i < s; ++i) ar >> m_listOfB[i]; }
participants (3)
-
Jeff Flinn
-
Robert Dailey
-
Robert Ramey