//---------------------------------------------------------------------------------------
// <copyright file="EventPropertyValue-SerializableItem.h" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
// 	Declaration/implementation of a template class used by EventProperyValue to serialize
//  arbitrary types as strings.
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------------

#include "LqrSMACSharedLib.h"
#include <sstream>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

namespace LiquidRobotics {
namespace EventLib {

class SerializableItem
{
public:

	typedef std::string string;

	// default construction
	SerializableItem()
	{
	}

	// construction from a value to be passed through CORBA as a string
	template<typename T>
	SerializableItem(const T& val)
	{
		Set(val);
	}

	// destructor
	~SerializableItem()
	{
	}

	static SerializableItem FromString(const string& serializedString)
	{
		return FromString(serializedString.c_str());
	}

	static SerializableItem FromString(const char* serializedString)
	{
		using namespace std;
		using namespace boost::archive;
		using namespace boost::serialization;

		SerializableItem item;
		item.asSerialized = serializedString;

		return item;
	}

	string ToString() const
	{
		return asSerialized;
	}

	string GetSerialString() const
	{
		using namespace std;

		size_t marker = asSerialized.find_first_of("||");

		if (marker == asSerialized.npos)
			throw runtime_error("SerializableItem could not find type name marker");

		return asSerialized.substr(marker+2);
	}

	string GetTypeName() const
	{
		using namespace std;

		size_t marker = asSerialized.find_first_of("||");

		if (marker == asSerialized.npos)
			throw runtime_error("SerializableItem could not find type name marker");

		return asSerialized.substr(0, marker);
	}

	template<typename T>
	void Get(T& val) const
	{
		using namespace std;
		using namespace boost::archive;
		using namespace boost::serialization;

		const string typeName = GetTypeName();

		if (typeName.compare(typeid(T).name()))
		{
			stringstream msg;
			msg << "Trying to extract " << typeid(T).name() << " from a serial stream containing " << typeName;

			throw runtime_error(msg.str());
		}

		stringstream stm(GetSerialString());

		{
			xml_iarchive ia(stm);
			ia >> make_nvp("item", val);
		}
	}

	template<typename T>
	void Set(const T& val)
	{
		using namespace std;
		using namespace boost::archive;
		using namespace boost::serialization;

		stringstream stm;
		stm << typeid(T).name() << "||";

		{
			xml_oarchive oa(stm);
			oa << make_nvp("item", val);
		}

		asSerialized = stm.str();
	}

private:

	string asSerialized;
};

}
}
