//--------------------------------------------------------------------------
// Code generated by the SmartSoft MDSD Toolchain
// The SmartSoft Toolchain has been developed by:
//  
// Service Robotics Research Center
// University of Applied Sciences Ulm
// Prittwitzstr. 10
// 89075 Ulm (Germany)
//
// Information about the SmartSoft MDSD Toolchain is available at:
// www.servicerobotik-ulm.de
//
// Please do not modify this file. It will be re-generated
// running the code generator.
//--------------------------------------------------------------------------
#include "CommLocalizationObjects/CommGpsFixCore.hh"

// serialization/deserialization operators
//#include "CommLocalizationObjects/CommGpsFixACE.hh"

// include the hash.idl containing the hash constant
#include "hash.hh"
#include <assert.h>
#include <cstring>
#include <iostream>

// SmartUtils used in from_xml method
#include "smartKnuthMorrisPratt.hh"

#ifdef ENABLE_HASH
	#include <boost/functional/hash.hpp>
#endif

namespace CommLocalizationObjects 
{
	const char* CommGpsFixCore::getCompiledHash()
	{
		return CommLocalizationObjectsIDL::REPO_HASH;
	}
	
	void CommGpsFixCore::getAllHashValues(std::list<std::string> &hashes)
	{
		// get own hash value
		hashes.push_back(getCompiledHash());
	}
	
	void CommGpsFixCore::checkAllHashValues(std::list<std::string> &hashes)
	{
		// check own hash value
		if (strcmp(getCompiledHash(), hashes.front().c_str()) != 0)
		{
			std::cerr << "###################################################" << std::endl;
			std::cerr << "WARNING: HASHES OF COMMUNICATION OBJECTS MISSMATCH!" << std::endl;
			std::cerr << "CommGpsFixCore hash" << std::endl;
			std::cerr << "Expected: " << getCompiledHash() << std::endl;
			std::cerr << "Received: " << hashes.front() << std::endl;
			std::cerr << "###################################################" << std::endl;
		}
		assert(strcmp(getCompiledHash(), hashes.front().c_str()) == 0);
		hashes.pop_front();
		
	}
	
	#ifdef ENABLE_HASH
	size_t CommGpsFixCore::generateDataHash(const DATATYPE &data)
	{
		size_t seed = 0;
		
		boost::hash_combine(seed, data.gps_timestamp);
		boost::hash_combine(seed, data.is_2D);
		boost::hash_combine(seed, data.is_3D);
		boost::hash_combine(seed, data.satellites_visible);
		boost::hash_combine(seed, data.satellites_used);
		boost::hash_combine(seed, data.latitude);
		boost::hash_combine(seed, data.longitude);
		boost::hash_combine(seed, data.altitude);
		boost::hash_combine(seed, data.speed);
		boost::hash_combine(seed, data.climb);
		boost::hash_combine(seed, data.latitude_uncertainty);
		boost::hash_combine(seed, data.longitude_uncertainty);
		boost::hash_combine(seed, data.altitude_uncertainty);
		boost::hash_combine(seed, data.speed_uncertainty);
		boost::hash_combine(seed, data.climb_uncertainty);
		
		return seed;
	}
	#endif
	
	// default constructor
	CommGpsFixCore::CommGpsFixCore()
	:	idl_CommGpsFix()
	{  
		setGps_timestamp(0.0);
		setIs_2D(false);
		setIs_3D(false);
		setSatellites_visible(0);
		setSatellites_used(0);
		setLatitude(0.0);
		setLongitude(0.0);
		setAltitude(0.0);
		setSpeed(0.0);
		setClimb(0.0);
		setLatitude_uncertainty(0.0);
		setLongitude_uncertainty(0.0);
		setAltitude_uncertainty(0.0);
		setSpeed_uncertainty(0.0);
		setClimb_uncertainty(0.0);
	}
	
	CommGpsFixCore::CommGpsFixCore(const DATATYPE &data)
	:	idl_CommGpsFix(data)
	{  }

	CommGpsFixCore::~CommGpsFixCore()
	{  }
	
	void CommGpsFixCore::to_ostream(std::ostream &os) const
	{
	  os << "CommGpsFix(";
	  os << getGps_timestamp() << " ";
	  os << getIs_2D() << " ";
	  os << getIs_3D() << " ";
	  os << getSatellites_visible() << " ";
	  os << getSatellites_used() << " ";
	  os << getLatitude() << " ";
	  os << getLongitude() << " ";
	  os << getAltitude() << " ";
	  os << getSpeed() << " ";
	  os << getClimb() << " ";
	  os << getLatitude_uncertainty() << " ";
	  os << getLongitude_uncertainty() << " ";
	  os << getAltitude_uncertainty() << " ";
	  os << getSpeed_uncertainty() << " ";
	  os << getClimb_uncertainty() << " ";
	  os << ") ";
	}
	
	// convert to xml stream
	void CommGpsFixCore::to_xml(std::ostream &os, const std::string &indent) const {
		os << indent << "<gps_timestamp>" << getGps_timestamp() << "</gps_timestamp>";
		os << indent << "<is_2D>" << getIs_2D() << "</is_2D>";
		os << indent << "<is_3D>" << getIs_3D() << "</is_3D>";
		os << indent << "<satellites_visible>" << getSatellites_visible() << "</satellites_visible>";
		os << indent << "<satellites_used>" << getSatellites_used() << "</satellites_used>";
		os << indent << "<latitude>" << getLatitude() << "</latitude>";
		os << indent << "<longitude>" << getLongitude() << "</longitude>";
		os << indent << "<altitude>" << getAltitude() << "</altitude>";
		os << indent << "<speed>" << getSpeed() << "</speed>";
		os << indent << "<climb>" << getClimb() << "</climb>";
		os << indent << "<latitude_uncertainty>" << getLatitude_uncertainty() << "</latitude_uncertainty>";
		os << indent << "<longitude_uncertainty>" << getLongitude_uncertainty() << "</longitude_uncertainty>";
		os << indent << "<altitude_uncertainty>" << getAltitude_uncertainty() << "</altitude_uncertainty>";
		os << indent << "<speed_uncertainty>" << getSpeed_uncertainty() << "</speed_uncertainty>";
		os << indent << "<climb_uncertainty>" << getClimb_uncertainty() << "</climb_uncertainty>";
	}
	
	// restore from xml stream
	void CommGpsFixCore::from_xml(std::istream &is) {
		static const Smart::KnuthMorrisPratt kmp_gps_timestamp("<gps_timestamp>");
		static const Smart::KnuthMorrisPratt kmp_is_2D("<is_2D>");
		static const Smart::KnuthMorrisPratt kmp_is_3D("<is_3D>");
		static const Smart::KnuthMorrisPratt kmp_satellites_visible("<satellites_visible>");
		static const Smart::KnuthMorrisPratt kmp_satellites_used("<satellites_used>");
		static const Smart::KnuthMorrisPratt kmp_latitude("<latitude>");
		static const Smart::KnuthMorrisPratt kmp_longitude("<longitude>");
		static const Smart::KnuthMorrisPratt kmp_altitude("<altitude>");
		static const Smart::KnuthMorrisPratt kmp_speed("<speed>");
		static const Smart::KnuthMorrisPratt kmp_climb("<climb>");
		static const Smart::KnuthMorrisPratt kmp_latitude_uncertainty("<latitude_uncertainty>");
		static const Smart::KnuthMorrisPratt kmp_longitude_uncertainty("<longitude_uncertainty>");
		static const Smart::KnuthMorrisPratt kmp_altitude_uncertainty("<altitude_uncertainty>");
		static const Smart::KnuthMorrisPratt kmp_speed_uncertainty("<speed_uncertainty>");
		static const Smart::KnuthMorrisPratt kmp_climb_uncertainty("<climb_uncertainty>");
		
		if(kmp_gps_timestamp.search(is)) {
			double gps_timestampItem;
			is >> gps_timestampItem;
			setGps_timestamp(gps_timestampItem);
		}
		if(kmp_is_2D.search(is)) {
			bool is_2DItem;
			is >> is_2DItem;
			setIs_2D(is_2DItem);
		}
		if(kmp_is_3D.search(is)) {
			bool is_3DItem;
			is >> is_3DItem;
			setIs_3D(is_3DItem);
		}
		if(kmp_satellites_visible.search(is)) {
			int satellites_visibleItem;
			is >> satellites_visibleItem;
			setSatellites_visible(satellites_visibleItem);
		}
		if(kmp_satellites_used.search(is)) {
			int satellites_usedItem;
			is >> satellites_usedItem;
			setSatellites_used(satellites_usedItem);
		}
		if(kmp_latitude.search(is)) {
			double latitudeItem;
			is >> latitudeItem;
			setLatitude(latitudeItem);
		}
		if(kmp_longitude.search(is)) {
			double longitudeItem;
			is >> longitudeItem;
			setLongitude(longitudeItem);
		}
		if(kmp_altitude.search(is)) {
			double altitudeItem;
			is >> altitudeItem;
			setAltitude(altitudeItem);
		}
		if(kmp_speed.search(is)) {
			double speedItem;
			is >> speedItem;
			setSpeed(speedItem);
		}
		if(kmp_climb.search(is)) {
			double climbItem;
			is >> climbItem;
			setClimb(climbItem);
		}
		if(kmp_latitude_uncertainty.search(is)) {
			double latitude_uncertaintyItem;
			is >> latitude_uncertaintyItem;
			setLatitude_uncertainty(latitude_uncertaintyItem);
		}
		if(kmp_longitude_uncertainty.search(is)) {
			double longitude_uncertaintyItem;
			is >> longitude_uncertaintyItem;
			setLongitude_uncertainty(longitude_uncertaintyItem);
		}
		if(kmp_altitude_uncertainty.search(is)) {
			double altitude_uncertaintyItem;
			is >> altitude_uncertaintyItem;
			setAltitude_uncertainty(altitude_uncertaintyItem);
		}
		if(kmp_speed_uncertainty.search(is)) {
			double speed_uncertaintyItem;
			is >> speed_uncertaintyItem;
			setSpeed_uncertainty(speed_uncertaintyItem);
		}
		if(kmp_climb_uncertainty.search(is)) {
			double climb_uncertaintyItem;
			is >> climb_uncertaintyItem;
			setClimb_uncertainty(climb_uncertaintyItem);
		}
	}
	
	/*
	void CommGpsFixCore::get(ACE_Message_Block *&msg) const
	{
		// start with a default internal buffer size(will automatically grow if needed)
		ACE_OutputCDR cdr(ACE_DEFAULT_CDR_BUFSIZE);
		
		CommLocalizationObjectsIDL::HashList hashes;
		getAllHashValues(hashes);
		cdr << static_cast<ACE_CDR::Long>(hashes.size());
		for(CommLocalizationObjectsIDL::HashList::const_iterator it=hashes.begin(); it!=hashes.end(); it++)
		{
			cdr << ACE_CString(it->c_str());
		}
		
		// Here the actual serialization takes place using the OutputCDR serialization operator<<
		// (see CommGpsFixACE.hh)
		cdr << idl_CommGpsFix;
		
	#ifdef ENABLE_HASH
		ACE_CDR::ULong data_hash = generateDataHash(idl_CommGpsFix);
		cdr << data_hash;
		// std::cout << "CommGpsFixCore: current data hash: " << data_hash << std::endl;
	#endif
		
		// return a shallow copy of the serialized message 
		// (no data is actually copied, only the internal reference counter is incremented)
		// in order to prevent memory leaks the caller of this get(msg) method must
		// manually free the memory by calling the release() method of the message block msg
		msg = cdr.begin()->duplicate();
	}
	
	void CommGpsFixCore::set(const ACE_Message_Block *msg)
	{
		ACE_InputCDR cdr(msg);
	
		CommLocalizationObjectsIDL::HashList hashes;
		ACE_CDR::Long hashes_size;
		cdr >> hashes_size;
		for(int i=0; i<hashes_size; ++i) 
		{
			ACE_CString hash;
			cdr >> hash;
			hashes.push_back(hash.c_str());
		}
		checkAllHashValues(hashes);
		
		// Here the actual de-serialization takes place using the InputCDR serialization operator>>
		// (see CommGpsFixACE.hh)
		cdr >> idl_CommGpsFix;
		
	#ifdef ENABLE_HASH
		ACE_CDR::Long data_hash;
		cdr >> data_hash;
		ACE_CDR::Long own_hash = generateDataHash(idl_CommGpsFix);
		assert(data_hash == own_hash);
		// std::cout << "CommGpsFixCore: own data hash: " << own_hash << "; received data hash: " << data_hash << std::endl;
	#endif
	}
	*/
} /* namespace CommLocalizationObjects */
