summary refs log tree commit diff stats
path: root/tests/accept
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-08-01 00:21:16 +0200
committerAraq <rumpf_a@web.de>2011-08-01 00:21:16 +0200
commitdd99fe61c02c79a42965cb0c24f2e8e24eec3886 (patch)
tree6e6573c36d216c3a557440f1e1103bded48c590e /tests/accept
parent3e806a374a1b42246e947965677c4a1520f3db57 (diff)
downloadNim-dd99fe61c02c79a42965cb0c24f2e8e24eec3886.tar.gz
documentation for 'var T' as return type
Diffstat (limited to 'tests/accept')
-rw-r--r--tests/accept/run/teventemitter.nim10
1 files changed, 6 insertions, 4 deletions
diff --git a/tests/accept/run/teventemitter.nim b/tests/accept/run/teventemitter.nim
index 763f41943..68970b967 100644
--- a/tests/accept/run/teventemitter.nim
+++ b/tests/accept/run/teventemitter.nim
@@ -5,16 +5,18 @@ type
 type
   TEventEmitter = object of TObject
     events*: TTable[string, TDoublyLinkedList[proc(e: TEventArgs)]]
+        
+proc emit*(emitter: TEventEmitter, event: string, args: TEventArgs) =
+  for func in nodes(emitter.events[event]):
+    func.value(args) #call function with args.
+
 proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs)) =
   if not hasKey(emitter.events, event):
     var list: TDoublyLinkedList[proc(e: TEventArgs)]
     add(emitter.events,event,list) #if not, add it.
   #append(emitter.events[event], func)
   #adds the function to the event's list. I get a error here too.
-        
-proc emit*(emitter: TEventEmitter, event: string, args: TEventArgs) =
-  for func in nodes(emitter.events[event]):
-    func.value(args) #call function with args.
+
 proc initEmitter(emitter: TEventEmitter) =
   emitter.events = initTable[string, TDoublyLinkedList[proc(e: TEventArgs)]]()
 
{ color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#
#
#            Nimrod's Runtime Library
#        (c) Copyright 2011 Alex Mitchell
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## :Author: Alex Mitchell
##
## 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.

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.

type
  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): TEventHandler =
  ## Initializes an EventHandler with the specified name and returns it.
  #new(result)
  result.handlers = @[]
  result.name = name

proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs)) =
  ## Adds the callback to the specified event handler.
  handler.handlers.add(func)

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: var TEventHandler) =
  ## Clears all of the callbacks from the event handler.
  setLen(handler.handlers, 0)

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: 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)
  if i < 0:
    var eh = newEventHandler(event)
    addHandler(eh, func)
    emitter.s.add(eh)
  else:
    addHandler(emitter.s[i], func)
  
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: var TEventEmitter, event: string, args: TEventArgs) =
  ## Fires an event handler with specified event arguments.
  var i = getEventHandler(emitter, event)
  if i >= 0:
    emit(emitter, emitter.s[i], args)
  else:
    raise newException(EInvalidEvent, "invalid event: " & event)

proc newEventEmitter*(): TEventEmitter =
  ## Creates and returns a new EventEmitter.
  #new(result)
  var result : TEventEmitter
  result.s = @[]
  return result