-----------------------------------------------------------------------------
-- Event.lua
--
-- Event is a lua class that can be passed around as a kind of message.  It
-- always has an event name that is passed at construction, but other items
-- can be added.  The idea is to use it as a wrapper for various kinds of
-- data passed between callbacks methods and coroutines, but it could be
-- used for other purposes.
--
-- Construction with event name only:
--
--	local evt = Event("timerExpired")
--
-- Construction with event name and other fields:
--
--	local evt = Event("serialData", { data="123456", otherTag="A",
--				timeOfEvent=thetime })
--
-- Checking the event name, which is a private member:
--
--	if (evt.getEventName() == "timerExpired") then HandleTimerExpired() end
--
-- Making a copy of an event object:
--
--	local evt2 = evt.duplicate()
--
-- Printing out the members of the event, including the private event name:
--
--	print(evt) -- or --
--	print(evt.toString()) -- or --
--	print("The event is "..tostring(evt))
--
-----------------------------------------------------------------------------

require "UtilFunc"

function Event(evtName, o)
	local meta = { }
	meta.__index = meta
	meta.__tostring = 	function(obj)
					return obj.toString()
				end
	o = o or { }
	setmetatable(o, meta)
	local self = { eventName = evtName }
	local duplicate =	function()
					local result = Event(evtName)
					local k
					local v
					for k,v in pairs(o) do
						if type(v) ~= "function" then
							result[k] = v
						end
					end
					return result
				end
	local getEventName = 	function()
					return self.eventName
				end
	local toString =	function()
					local result = "(Event2) "..o.getEventName().." "..UtilFunc.ShowTable(o,false)
					return result
				end

	o.duplicate = duplicate
	o.getEventName = getEventName
	o.toString = toString
	return o
end

