//------------------------------------------------------------------------------------
// <copyright file="EventPropertyValue-inline.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>
// 	Implementation of inline methods for EventPropertyValue class, which wraps a CORBA
//  Any instance to  store one of several basic types and which interoperates with STL
//  vector and STL string.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------------

#ifndef __EVENTLIB_EVENTPROPERTYVALUE_INLINE_H__
#define __EVENTLIB_EVENTPROPERTYVALUE_INLINE_H__

#include "EventPropertyValue.h"
#include "LqrSMACIObjectBroker-inline.h"
#include <sstream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {
namespace EventLib {

template<typename T>
void EventPropertyValue::AppendBytes(const T& val, ByteArray& output, bool swapBytes)
{
	typedef ByteArray::byte byte;
	unsigned ctr = sizeof(T);

	if (swapBytes)
	{
		const byte* ptr = (const byte*) &val;
		while(ctr--) output.push_back(*ptr++);
	}
	else
	{
		const byte* ptr = ((const byte*) &val)+sizeof(T);
		while(ctr--) output.push_back(*--ptr);
	}
}

template<typename TV>
void EventPropertyValue::AppendCollectionBytes(const TV& val, ByteArray& output, bool swapBytes)
{
	typedef ByteArray::byte byte;

	if (sizeof(TV::value_type) == 1)
	{
		BOOST_FOREACH(const typename TV::value_type& item, val)
			output.push_back((byte) item);
	}
	else
	{
		BOOST_FOREACH(const typename TV::value_type& item, val)
			AppendBytes(val, output, swapBytes);
	}
}

template<typename TRet,typename TTemp>
TRet EventPropertyValue::GetScalar(StorageType storageType) const
{
	using namespace std;
	TTemp temp;
	bool extractionOK = (theValue >>= temp);

	if (!extractionOK)
	{
		stringstream stm;

		stm << "GetScalar: failed to extract type " << DescribeStorageType(storageType)
			<< " from property containing type " << DescribeStorageType();

		throw ExtractionFailed(stm.str());
	}

	return (TRet) temp;
}

template<typename TRet,typename TTemp,typename TInter>
TRet EventPropertyValue::GetScalar(StorageType storageType) const
{
	using namespace std;
	TTemp temp;
	TInter inter(temp);
	bool extractionOK = (theValue >>= inter);

	if (!extractionOK)
	{
		stringstream stm;

		stm << "GetScalar: failed to extract type " << DescribeStorageType(storageType)
			<< " from property containing type " << DescribeStorageType();

		throw ExtractionFailed(stm.str());
	}

	return (TRet) temp;
}

template<typename TRet,typename TTemp>
void EventPropertyValue::SetScalar(TRet newValue)
{
	theValue = new ::CORBA::Any;
	theValue <<= (TTemp) newValue;
}

template<typename TRet,typename TTemp,typename TInter>
void EventPropertyValue::SetScalar(TRet newValue)
{
	TTemp temp = (TTemp) newValue;
	TInter inter(temp);

	theValue = new ::CORBA::Any;
	theValue <<= inter;
}

}
}

#endif
