Source

packet-code-generator / packet_base.h

/*
Packet Code Generator - generates packet codes using only C++ templates and macros
Copyright (C) 2008, 2011 Woojong Koh <wjngkoh@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PACKET_BASE_H
#define PACKET_BASE_H

#ifndef ALL_H
#include <iosfwd>
#include <string>
#include <sstream>
#include <boost/serialization/access.hpp>

#include <boost/serialization/level.hpp>
#include <boost/serialization/tracking.hpp>
#endif

class packet_base
{
	friend class boost::serialization::access;
	template <class Archive>
	void serialize(Archive& ar, const unsigned int version) {}

public:
	packet_base() {}
	virtual ~packet_base() = 0;
	virtual const std::string& id() const = 0;

	virtual void notify() const = 0; 
	virtual std::ostream& print(std::ostream& os) const = 0; 

	std::string get_string()
	{
		std::ostringstream os;
		print(os);
		return os.str();
	}
};

// a pure virtual destructor definition
packet_base::~packet_base() {}

std::ostream& operator<<(std::ostream& os, const packet_base& packet) { return packet.print(os); }

// eliminate object tracking (even if serialized through a pointer)
// at the risk of a programming error creating duplicate objects.
BOOST_CLASS_TRACKING(packet_base, boost::serialization::track_always)

#endif