From 5209469fd703d3005f6e495c10cdad2da441fd57 Mon Sep 17 00:00:00 2001 From: Amrykid Date: Wed, 27 Jul 2011 17:39:43 -0500 Subject: Updated events.nim to use TEventEmitter instead of PEventEmitter. --- lib/pure/events.nim | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/events.nim b/lib/pure/events.nim index 00d6797d2..8ba637f9e 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -16,43 +16,42 @@ type TEventArgs* = object of TObject ## Base object for event arguments ## that are passed to callback functions. - TEventHandler = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] - PEventHandler* = ref TEventHandler ## An eventhandler for an event. + TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] + #PEventHandler* = ref TEventHandler ## An eventhandler for an event. type - TEventEmitter = object {.pure, final.} - s: seq[PEventHandler] - PEventEmitter* = ref TEventEmitter ## An object that fires events and - ## holds event handlers for an object. + TEventEmitter* = object {.pure, final.} + s: seq[TEventHandler] + #PEventEmitter* = ref TEventEmitter ## An object that fires events and holds event handlers for an object. EInvalidEvent* = object of EInvalidValue -proc newEventHandler*(name: string): PEventHandler = +proc newEventHandler*(name: string): TEventHandler = ## Initializes an EventHandler with the specified name and returns it. - new(result) + #new(result) result.handlers = @[] result.name = name -proc addHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) = +proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = ## Adds the callback to the specified event handler. handler.handlers.add(func) -proc removeHandler*(handler: PEventHandler, func: proc(e: TEventArgs)) = +proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) = ## Removes the callback from the specified event handler. for i in countup(0, len(handler.handlers) -1): if func == handler.handlers[i]: handler.handlers.del(i) break -proc clearHandlers*(handler: PEventHandler) = +proc clearHandlers*(handler: var TEventHandler) = ## Clears all of the callbacks from the event handler. setLen(handler.handlers, 0) -proc getEventhandler(emitter: PEventEmitter, event: string): int = +proc getEventhandler(emitter: var TEventEmitter, event: string): int = for k in 0..high(emitter.s): if emitter.s[k].name == event: return k return -1 -proc on*(emitter: PEventEmitter, event: string, func: proc(e: TEventArgs)) = +proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) = ## Assigns a event handler with the specified callback. If the event ## doesn't exist, it will be created. var i = getEventHandler(emitter, event) @@ -63,12 +62,12 @@ proc on*(emitter: PEventEmitter, event: string, func: proc(e: TEventArgs)) = else: addHandler(emitter.s[i], func) -proc emit*(emitter: PEventEmitter, eventhandler: PEventHandler, +proc emit*(emitter: var TEventEmitter, eventhandler: var TEventHandler, args: TEventArgs) = ## Fires an event handler with specified event arguments. for func in items(eventhandler.handlers): func(args) -proc emit*(emitter: PEventEmitter, event: string, args: TEventArgs) = +proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) = ## Fires an event handler with specified event arguments. var i = getEventHandler(emitter, event) if i >= 0: @@ -76,8 +75,9 @@ proc emit*(emitter: PEventEmitter, event: string, args: TEventArgs) = else: raise newException(EInvalidEvent, "invalid event: " & event) -proc newEventEmitter*(): PEventEmitter = +proc newEventEmitter*(): TEventEmitter = ## Creates and returns a new EventEmitter. - new(result) + #new(result) + var result : TEventEmitter result.s = @[] - + return result \ No newline at end of file -- cgit 1.4.1-2-gfad0 From ecd62b955b0a2417506e69bfd898af1b135b8f49 Mon Sep 17 00:00:00 2001 From: Amrykid Date: Wed, 27 Jul 2011 17:41:32 -0500 Subject: Small edit to documentation. --- lib/pure/events.nim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/events.nim b/lib/pure/events.nim index 8ba637f9e..30ab324d6 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -16,13 +16,11 @@ type TEventArgs* = object of TObject ## Base object for event arguments ## that are passed to callback functions. - TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] - #PEventHandler* = ref TEventHandler ## An eventhandler for an event. + TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] ## An eventhandler for an event. type - TEventEmitter* = object {.pure, final.} + TEventEmitter* = object {.pure, final.} ## An object that fires events and holds event handlers for an object. s: seq[TEventHandler] - #PEventEmitter* = ref TEventEmitter ## An object that fires events and holds event handlers for an object. EInvalidEvent* = object of EInvalidValue proc newEventHandler*(name: string): TEventHandler = -- cgit 1.4.1-2-gfad0 From 79dafe08e33b82fb1955c981840ff7b3790c224e Mon Sep 17 00:00:00 2001 From: Amrykid Date: Wed, 27 Jul 2011 18:04:13 -0500 Subject: Renamed new* to init*. Added example code for documentation. Fixed a small documentation issue. Rewrote newEventEmitter... Updated test. --- lib/pure/events.nim | 33 ++++++++++++++++++++++++--------- tests/accept/run/tevents.nim | 4 ++-- 2 files changed, 26 insertions(+), 11 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/events.nim b/lib/pure/events.nim index 30ab324d6..d054dd217 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -11,11 +11,28 @@ ## ## This module implements an event system that is not dependant on external ## graphical toolkits. It was originally called ``NimEE`` because -## it was inspired by Ptyhon's PyEE module. - +## it was inspired by Python's PyEE module. There are two ways you can use events: one is a python-inspired way; the other is more of a C-style way. +## .. code-block:: Nimrod +## var ee = initEventEmitter() +## var genericargs : TEventArgs +## proc handleevent(e : TEventArgs) = +## echo("Handled!") +## +## # Python way +## ee.on("EventName", handleevent) +## ee.emit("EventName", genericargs) +## +## # C/Java way +## # Declare a type +## type +## TSomeObject = object of TObject +## SomeEvent : TEventHandler +## var myobj : TSomeObject +## myobj.SomeEvent = initEventHandler("SomeEvent") +## myobj.SomeEvent.addHandler(handleevent) +## ee.emit(myobj.SomeEvent, genericargs) type - TEventArgs* = object of TObject ## Base object for event arguments - ## that are passed to callback functions. + TEventArgs* = object of TObject ## Base object for event arguments that are passed to callback functions. TEventHandler* = tuple[name: string, handlers: seq[proc(e:TEventArgs)]] ## An eventhandler for an event. type @@ -23,7 +40,7 @@ type s: seq[TEventHandler] EInvalidEvent* = object of EInvalidValue -proc newEventHandler*(name: string): TEventHandler = +proc initEventHandler*(name: string): TEventHandler = ## Initializes an EventHandler with the specified name and returns it. #new(result) result.handlers = @[] @@ -73,9 +90,7 @@ proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) = else: raise newException(EInvalidEvent, "invalid event: " & event) -proc newEventEmitter*(): TEventEmitter = +proc initEventEmitter*(): TEventEmitter = ## Creates and returns a new EventEmitter. #new(result) - var result : TEventEmitter - result.s = @[] - return result \ No newline at end of file + result.s = @[] \ No newline at end of file diff --git a/tests/accept/run/tevents.nim b/tests/accept/run/tevents.nim index a53ae7ac8..fb94b1f79 100644 --- a/tests/accept/run/tevents.nim +++ b/tests/accept/run/tevents.nim @@ -18,7 +18,7 @@ proc handleprintevent2*(e: TEventArgs) = var args: TPrintEventArgs = TPrintEventArgs(e) write(stdout, "HandlePrintEvent2: Output -> printing for " & args.user) -var ee = newEventEmitter() +var ee = initEventEmitter() var eventargs: TPrintEventArgs eventargs.user = "ME\n" @@ -37,7 +37,7 @@ type PrintEvent: TEventHandler var obj: TSomeObject -obj.PrintEvent = newEventHandler("print") +obj.PrintEvent = initEventHandler("print") obj.PrintEvent.addHandler(handleprintevent2) ee.emit(obj.PrintEvent, eventargs) -- cgit 1.4.1-2-gfad0 From 4f54b93bb977220be4389316b5f55a1ec78f1bf0 Mon Sep 17 00:00:00 2001 From: Amrykid Date: Wed, 27 Jul 2011 18:12:32 -0500 Subject: Removed 'unnecessary' whitespace. --- lib/pure/events.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/events.nim b/lib/pure/events.nim index d054dd217..413256657 100644 --- a/lib/pure/events.nim +++ b/lib/pure/events.nim @@ -14,8 +14,8 @@ ## it was inspired by Python's PyEE module. There are two ways you can use events: one is a python-inspired way; the other is more of a C-style way. ## .. code-block:: Nimrod ## var ee = initEventEmitter() -## var genericargs : TEventArgs -## proc handleevent(e : TEventArgs) = +## var genericargs: TEventArgs +## proc handleevent(e: TEventArgs) = ## echo("Handled!") ## ## # Python way @@ -26,8 +26,8 @@ ## # Declare a type ## type ## TSomeObject = object of TObject -## SomeEvent : TEventHandler -## var myobj : TSomeObject +## SomeEvent: TEventHandler +## var myobj: TSomeObject ## myobj.SomeEvent = initEventHandler("SomeEvent") ## myobj.SomeEvent.addHandler(handleevent) ## ee.emit(myobj.SomeEvent, genericargs) -- cgit 1.4.1-2-gfad0