Boost Serialization question, or maybe just serialization:

This is a discussion on Boost Serialization question, or maybe just serialization: within the C++ Programming forums, part of the General Programming Boards category; So I have a database class that I want to handle all the writing to disk of the objects in ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Boost Serialization question, or maybe just serialization:

    So I have a database class that I want to handle all the writing to disk of the objects in my program. All the objects will have a customized serialize function that will write all the necessary information to disk, the saving function looks like this:

    Code:
    class Database
    {
    	friend class boost::serialization::access;
    	void Save_to_Disk(std::string Filename, CObject Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
        	// archive and stream closed when destructors are called
    	}
    private:
    	Object_Manager Menu_Objects;
    };
    Unfortunately this isn't working as described in the boost tutorials, I'm getting this error:
    Code:
    ------ Build started: Project: TextGame, Configuration: Debug Win32 ------
    Compiling...
    Main.cpp
    c:\program files\microsoft visual studio 8\vc\boost_1_34_0\boost\archive\detail\oserializer.hpp(566) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'
            with
            [
                x=false
            ]
            c:\program files\microsoft visual studio 8\vc\boost_1_34_0\boost\archive\detail\common_oarchive.hpp(62) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
            with
            [
                Archive=boost::archive::text_oarchive,
                T=CObject
            ]
            c:\program files\microsoft visual studio 8\vc\boost_1_34_0\boost\archive\basic_text_oarchive.hpp(75) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &,int)' being compiled
            with
            [
                Archive=boost::archive::text_oarchive,
                T=CObject
            ]
            c:\program files\microsoft visual studio 8\vc\boost_1_34_0\boost\archive\detail\interface_oarchive.hpp(79) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(T &,int)' being compiled
            with
            [
                Archive=boost::archive::text_oarchive,
                T=CObject
            ]
            c:\documents and settings\home\my documents\visual studio 2005\projects\textgame\textgame\database.h(13) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<CObject>(T &)' being compiled
            with
            [
                Archive=boost::archive::text_oarchive,
                T=CObject
            ]
    Build log was saved at "file://c:\Documents and Settings\Home\My Documents\Visual Studio 2005\Projects\TextGame\TextGame\Debug\BuildLog.htm"
    TextGame - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    As far as I know ofs << Data is the line that actually saves the data to the file, but I might be doing something wrong, I'm not sure what...

    Any suggestions?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Fixed, sorry about that, silly mistake:

    Data needed to be constant:
    Code:
    class Database
    {
    	friend class boost::serialization::access;
    	void Save_to_Disk(std::string Filename, const CObject Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
        	// archive and stream closed when destructors are called
    	}
    private:
    	Object_Manager Menu_Objects;
    };
    I may have more questions so stay tuned folks , appreciate the help
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okay, so I completed the saving and loading functions, well not completely.....

    Now I gotta make sure the objects I create go in the right manager container...

    Code:
    #ifndef DATABASE_H
    #define DATABASE_H
    
    #include "Menu_Object.h"
    #include <vector>
    class Database
    {
    	friend class boost::serialization::access;
    	void Save_to_File(std::string Filename, const CObject Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
        	// archive and stream closed when destructors are called
    	}
    
    	void Read_File(std::string Filename, CObject Data)
    	{
            // create and open an archive for input
            std::ifstream ifs(Filename.c_str(), std::ios::binary);
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> Data;
            // archive and stream closed when destructors are called
    	}
    private:
    	Object_Manager Menu_Objects;
    };
    Any suggestions? Feedback? Questions?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Another Question:

    For my menu classes to reconstruct they need pointers to other menu classes, I'm wondering if this serialization function will suffice in saving the pointer data, and I need to know if my read file function is enough to recreate the menu object, I don't think it is because I think somehow the pointer I save is useless unless I can recreate the object via that pointer, which might have different values than the menu object we load.. What can you all tell me?

    Code:
    class Menu : public CObject
    {
    public:
    	Menu(){};
    	Menu(std::string mSelection) 
    	{
    		mSelection = Selection;
    	}
    	virtual ~Menu() { Destroy(); }
    	// deletes self
    	void Release() { delete this; }
    	// call update on all children
    	virtual void Update()
    	{
    		for( std::list<Menu*>::iterator i = MenuChoices.begin();
    			i != MenuChoices.end(); i++ )
    		{
    			(*i)->Update();
    		}
    	}
    	// recursively destroy all children and self
    	void Destroy()
    	{
    		for( std::list<Menu*>::iterator i = MenuChoices.begin();
    			i != MenuChoices.end(); i++ )
    		(*i)->Release();
      
    		MenuChoices.clear();
    	}
    	// add a child
    	void MenuSelection( Menu* MenuSelection )
    	{
    		MenuSelection->SetRootMenu(this);
    	    MenuChoices.push_back(MenuSelection);
    	}
    	// Set the parent of the child
    	void SetRootMenu(Menu* Root)
    	{
    		RootMenu = Root;
    	}
    private:
    	friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            // serialize base class information
            ar & boost::serialization::base_object<CObject>(*this);
    		ar & Selection;
    		ar & RootMenu;
        }
    	// list of children
    	std::list<Menu*> MenuChoices;
    	// pointer to parent
    	Menu * RootMenu;
    
    	std::string Selection;
    };
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost Serialization unordered_multimap
    By idleman in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2009, 12:14 PM
  2. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 08:17 PM
  3. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 06:49 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21