//------------------------------------------------------------------------------
// <copyright file="LqrSMACIdl.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>
// Declares methods for wrapping IDL objects
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __LQRSMACIDL_H__
#define __LQRSMACIDL_H__

#include "LqrSMACSharedLib.h"
#include <string>
#include "all.hh"

#define USING_SENDCOMMAND_WORKAROUND (false)

namespace LiquidRobotics {
namespace SMACFramework {

template<typename T>
void ThrowCORBAException()
{
	T ex;
	throw ex;
}

template<typename T>
void ThrowCORBAException(const std::string& arg1)
{
	const char* param1 = arg1.c_str();
	T ex(param1);
	throw ex;
}

template<typename T>
void ThrowCORBAException2()
{
	T ex;
	throw ex;
}

template<typename T>
void ThrowCORBAException2(const std::string& arg1)
{
	const char* param1 = arg1.c_str();
	const char* param2 = "";
	T ex(param1, param2);
	throw ex;
}

template<typename T>
void ThrowCORBAException2(const std::string& arg1, const std::string& arg2)
{
	const char* param1 = arg1.c_str();
	const char* param2 = arg2.c_str();
	T ex(param1, param2);
	throw ex;
}

SHARED_LIB_PUBLIC std::string DecodeCORBAException(CORBA::Exception& ex);

// ICommandableInvokerBase factors out the exception processing
class ICommandableInvokerBase
{
public:

	typedef ::CORBA::Any Any;
	typedef ::CORBA::Any_OUT_arg Any_OUT_arg;
	typedef ::LiquidRobotics::SMAC::SendCommandWorkaroundData SendCommandWorkaroundData;
	typedef ::LiquidRobotics::SMAC::SendCommandWorkaroundData_out SendCommandWorkaroundData_out;

	ICommandableInvokerBase();
	virtual ~ICommandableInvokerBase();

	virtual void operator()(const Any& command, const char* requester, const char* transactionID, Any_OUT_arg result);

protected:

	virtual void Invoke(const Any& command, const char* requester, const char* transactionID, Any_OUT_arg result, SendCommandWorkaroundData_out waResult) = 0;
};

// Use ICommandableInvoker to invoke SendCommand workaround
template<typename T>
class ICommandableInvoker : public ICommandableInvokerBase
{
public:

	ICommandableInvoker(T& _ivar)
	: ivar(_ivar)
	{ }

protected:

	virtual void Invoke(const Any& command, const char* requester, const char* transactionID, Any_OUT_arg result, SendCommandWorkaroundData_out waResult)
	{
#if USING_SENDCOMMAND_WORKAROUND
		ivar->SendCommandWorkaround(command, requester, transactionID, result, waResult);
#else
		ivar->SendCommand(command, requester, transactionID, result);
#endif
	}

private:

	T& ivar;
};

// Derive server implementation classes from ICommandableBase to implement standard workaround for exception problem
class ICommandableBase
{
public:

	typedef ::CORBA::Any Any;
	typedef ::CORBA::Any_OUT_arg Any_OUT_arg;
	typedef ::CORBA::Any_var Any_var;
	typedef ::LiquidRobotics::SMAC::SendCommandWorkaroundData SendCommandWorkaroundData;
	typedef ::LiquidRobotics::SMAC::SendCommandWorkaroundData_out SendCommandWorkaroundData_out;

	ICommandableBase();
	virtual ~ICommandableBase();

    virtual void SendCommand(const Any& command, const char* requester, const char* transactionID, Any_OUT_arg result) = 0;
    virtual void SendCommandWorkaround(const Any& command, const char* requester, const char* transactionID, Any_OUT_arg result, SendCommandWorkaroundData_out waData);
};

}
}


#endif /* LQRSMACIDL_H_ */
