Can you serialize a variable that isn't wrapped in an Object (Boost)

This is a discussion on Can you serialize a variable that isn't wrapped in an Object (Boost) within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I was wondering about the usefulness of a manager class when all it really is is a wrapper ...

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

    Can you serialize a variable that isn't wrapped in an Object (Boost)

    Hello everyone, I was wondering about the usefulness of a manager class when all it really is is a wrapper to a map. Right now I'm using boost serialization to serialize the entire manager class because I find it easier to do that. But I'm wondering if I really need a manager class at all as opposed to just a simple container.. Can anyone shed some light on this issue?

    Code:
    class Object_Manager
    {  
    public:
    	Object_Manager() {};
    	~Object_Manager() {};
    
    	typedef boost::shared_ptr<Object> Object_Ptr;
    	typedef boost::weak_ptr<Object> Object_Observer;
    	typedef std::map< std::string, Object_Ptr > Object_Map;
    
    	void Add_Object(const std::string & name, Object * pObject)
    	{
    		Object_Ptr Raw_Object(pObject);
    		mObjects.insert(std::make_pair(name, Raw_Object));
    	}
    
    	Object_Observer Request_Object(const std::string & name)
    	{
    		Object_Map::iterator  it = mObjects.find(name);
    
    		if (it == mObjects.end())
    		{
    			std::cout << "Internal program error, " << name << " does not exist!" << std::endl;
    		}
    		else
    		{
    			return Object_Observer(it->second);
    		}
    	}
    
    	void Request_Object_Removal(const std::string & name)
    	{
    		Object_Map::iterator it = mObjects.find(name);
    
    		if (it != mObjects.end())
    		{
    			mObjects.erase(it);
    		}
    	}
    private:
    	friend class boost::serialization::access;
    	template<class Archive>
    	void serialize(Archive & ar, const unsigned int version)
    	{
    		ar & mObjects;
    	}
    	Object_Map  mObjects;
    };
    Do I really need this big ol class just to wrap a map? Or can I get away with just having maps and still be able to serialize them?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    Taken from http://www.boost.org/libs/serializat.../tutorial.html:

    "The serialization library contains code for serialization of all STL classes."

    The question then becomes, is the functionality worth the overhead?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    It would be even more overhead doing it himself...

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    88
    > It would be even more overhead doing it himself...

    Awww, you ruined it. I was just going to let the obvious conclusion go unsaid.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I guess I can really do away with the entire object manager class then...

    Thanks alot for the little nudge
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I guess my only issue with that is, where would I keep my maps? floating in freespace? Or maybe I can create them in the database class....

    Code:
    #ifndef DATABASE_H
    #define DATABASE_H
    
    #include "Object.h"
    #include <map>
    #include <iostream>
    class Database
    {
    public:	
    
    	typedef boost::shared_ptr<Object_Manager> Manager_Ptr;
    	typedef boost::weak_ptr<Object_Manager> Manager_Observer;
    	typedef std::map< std::string, Manager_Ptr > Manager_Map;
    
    	// Function to access a specific manager within the map
    	Manager_Observer Access_Manager(std::string Filename)
    	{
    		Manager_Map::iterator it = Database_Manager.find(Filename);
    		// check if the manager exists in the map
    		if (it == Database_Manager.end())
    		{
    			// if not throw an error
    			std::cout << "Manager does not exist!" << std::endl;
    		}
    		else
    		{
    			// if so return a pointer to the manager
    			return Manager_Observer(it->second);
    		}
    	}
    
    	//Save a manager completely to a file
    	void Save_to_File(std::string Filename, const Object_Manager Data)
    	{
    		std::ofstream ofs(Filename.c_str());
    		boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << Data;
    		Data.~Object_Manager();
        	// archive and stream closed when destructors are called
    	}
    
    	// Load a manager from a file and add it to the list of managers
    	void Read_File(std::string Filename, Object_Manager * 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;
    		// create a pointer to the data
    		Manager_Ptr Raw_Manager(Data);
    		// pair the filename with the manager typ for easy access
    		Database_Manager.insert(std::make_pair(Filename, Raw_Manager));
            // archive and stream closed when destructors are called
    	}
    private:
    	Manager_Map Database_Manager;
    };
    
    #endif
    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. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  3. error display variable value/type
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-08-2008, 04:24 PM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM

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