//------------------------------------------------------------------------------
// <copyright file="EventBase.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>
// 	Declarations for EventBase class, which is a common base class for all event
//  classes.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#ifndef __EVENTLIB_EVENTBASE_H__
#define __EVENTLIB_EVENTBASE_H__

#include "EventSettings.h"
#include "LqrSMACDeclareException.h"
#include "LqrSMACSharedLib.h"
#include "IEventBase.h"
#include <map>
#include <stdexcept>
#include <boost/shared_ptr.hpp>

namespace LiquidRobotics {

//! Classes deployed in the EventLib shared library (related to CORBA events or using CORBA events)
namespace EventLib {

class SHARED_LIB_PUBLIC EventBase : public IEventBase
{
public:

	DECLARE_EXCEPTION(InvalidField,std::runtime_error);
	DECLARE_EXCEPTION(IORNotSet,std::runtime_error);
	DECLARE_EXCEPTION(MissingRequiredField,std::runtime_error);
	DECLARE_EXCEPTION(PropertyNotFound,std::runtime_error);
	DECLARE_EXCEPTION(PropertyValueNotSupported,std::runtime_error);

	typedef LiquidRobotics::SMACFramework::IObjectBroker IObjectBroker;
	typedef boost::shared_ptr<EventBase> Ptr;

	EventBase(IObjectBroker& _broker);
	EventBase(const EventSettings& _eventSettings, IObjectBroker& _broker);
	EventBase(const EventName& _eventName, const EventType& _eventType, IObjectBroker& _broker);
	EventBase(const EventName& _eventName, const EventType& _eventType, const IOR& _senderIOR, IObjectBroker& _broker);
	EventBase(const EventBase& right);

	EventBase& operator=(const EventBase& other);

	virtual PropertyValuePtr CreateValue();

	virtual ostream& Describe(ostream& stm) const;
	virtual string Describe() const;
	virtual ostream& DescribeGeneric(ostream& stm) const;
	virtual string DescribeGeneric() const;

	virtual IEventBase::Ptr Duplicate() const;

	virtual const EventName& GetEventName() const;
	virtual void SetEventName(const EventName& newValue);

	virtual const EventSettings& GetEventSettings() const;
	virtual void SetEventSettings(const EventSettings& newValue);

	virtual const EventType& GetEventType() const;
	virtual void SetEventType(const EventType& newValue);

	virtual bool ContainsSenderIOR() const;
	virtual const IOR& GetSenderIOR() const;
	virtual void SetSenderIOR(const IOR& newValue);

	virtual void ClearProperties();
	virtual void ClearProperty(const PropertyNameType& propertyName);
	virtual bool ContainsProperty(const PropertyNameType& propertyName, bool throwOnUnsupportedType = false) const;
	virtual PropertyValueType& GetProperty(const PropertyNameType& propertyName, bool throwOnUnsupportedType = false);
	virtual const PropertyValueType& GetProperty(const PropertyNameType& propertyName, bool throwOnUnsupportedType = false) const;
	virtual PropertyNameArrayPtr GetPropertyNames() const;
	virtual void SetProperty(const PropertyNameType& propertyName, const PropertyValueType& newValue);

	virtual CORBA_StructuredEvent_var CreateStructuredEvent() const;
	virtual void InitFromStructuredEvent(const CORBA_StructuredEvent& structEvent);

protected:

	void Copy(const EventBase& other);

private:

	typedef std::map<PropertyNameType, PropertyValueType::Ptr> PropertyLookup;

	EventSettings eventSettings;
	IOR senderIOR;

protected:

	IObjectBroker& broker;

private:

	PropertyLookup properties;

	static const string SenderIORTag;
};


}
}

#endif
