From 52a2e5b301797f8a35ec2a7b40dd624a3504b099 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Fri, 9 Dec 2011 10:51:10 +0100 Subject: [PATCH] update delegate and event sampler for website --- CSharpTranslator/tests/Tester/Tester.csproj | 3 +- .../Tester/WebDelegates/DelegateSampler.cs | 13 ++- .../tests/Tester/WebEvents/EventSampler.cs | 95 ++++++++++++------- 3 files changed, 68 insertions(+), 43 deletions(-) diff --git a/CSharpTranslator/tests/Tester/Tester.csproj b/CSharpTranslator/tests/Tester/Tester.csproj index 20b3b8b..40c54ab 100644 --- a/CSharpTranslator/tests/Tester/Tester.csproj +++ b/CSharpTranslator/tests/Tester/Tester.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,7 +9,6 @@ Exe Tester Tester - v4.0 diff --git a/CSharpTranslator/tests/Tester/WebDelegates/DelegateSampler.cs b/CSharpTranslator/tests/Tester/WebDelegates/DelegateSampler.cs index 8e2be3b..56e224b 100755 --- a/CSharpTranslator/tests/Tester/WebDelegates/DelegateSampler.cs +++ b/CSharpTranslator/tests/Tester/WebDelegates/DelegateSampler.cs @@ -1,6 +1,6 @@ -// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com) +// Copyright (c) 2011 Kevin Glynn (http://www.twigletsoftware.com) // -// The MIT License (MIT) +// The MIT License (Expat) // // 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, @@ -28,7 +28,6 @@ namespace Twiglet.Sample.Delegate /// public class DelegateSampler { - public delegate void xProcessor(ref T value); // /// /// LogWriters take a string and record it. @@ -37,10 +36,10 @@ namespace Twiglet.Sample.Delegate public delegate void LogWriter(string logMessage); /// - /// A chain of delegates can't pass values to each other, (the value - /// returned from the final delegate is returned to the caller, but the - /// values from intermediate delegates are just dropped on the floor). - /// However the delegates can communictae if we use ref parameters. + /// A chain of delegates can't pass values to each other, (only the value + /// returned from the final delegate is returned to the caller, the + /// return values from intermediate delegates are just dropped on the floor). + /// However the delegates can communicate if we use ref parameters. /// /// the variable that we are processing public delegate void Processor(ref T value); diff --git a/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs b/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs index 118eff3..c41cb0c 100755 --- a/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs +++ b/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs @@ -1,6 +1,6 @@ -// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com) +// Copyright (c) 2011 Kevin Glynn (http://www.twigletsoftware.com) // -// The MIT License (MIT) +// The MIT License (Expat) // // 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, @@ -24,15 +24,24 @@ namespace Twiglet.Sample.Event { /// - /// Sample class to show off CS2J's translations for events + /// Sample class to show off CS2J's translations for events. + /// + /// A StorePublisher object stores a list of thingummys. Thingummys + /// can be added to the store and the store can be cleared. (For brevity + /// we omit methods to retrieve thingummys from the store!). + /// + /// The StorePublisher has two events, one fires when items are added, the + /// other fires when the store is cleared. Callers to Add and Clear pass + /// a name, and this name is sent to the event subscribers. + /// /// - public class StorePublisher + public class StorePublisher { - private List store = null; + private List store = null; // The events raised by this Store Publisher public event EventHandler RaiseClearedEvent; - public event EventHandler RaiseStoredEvent; + public event EventHandler> RaiseStoredEvent; // raise the Clear event protected virtual void OnRaiseClearedEvent(ClearEventArgs e) @@ -45,7 +54,7 @@ namespace Twiglet.Sample.Event } // raise the Store event - protected virtual void OnRaiseStoredEvent(StoreEventArgs e) + protected virtual void OnRaiseStoredEvent(StoreEventArgs e) { if (RaiseStoredEvent != null) { @@ -56,18 +65,18 @@ namespace Twiglet.Sample.Event public void Clear(string requestor) { - store = new List(); + store = new List(); OnRaiseClearedEvent(new ClearEventArgs(requestor)); } - public void Add(string requestor, string data) + public void Add(string requestor, T data) { if (store == null) { - Clear("Add, needed to service " + requestor); + Clear("Add"); } store.Add(data); - OnRaiseStoredEvent(new StoreEventArgs(requestor, data)); + OnRaiseStoredEvent(new StoreEventArgs(requestor, data)); } } @@ -84,11 +93,11 @@ namespace Twiglet.Sample.Event /// /// EventArgs derived class to pass info about a request to add data to store /// - public class StoreEventArgs : EventArgs + public class StoreEventArgs : EventArgs { - public StoreEventArgs(string req, string data) { this.Requestor = req; this.Data = data; } + public StoreEventArgs(string req, T data) { this.Requestor = req; this.Data = data; } public string Requestor { get; set; } - public string Data { get; set; } + public T Data { get; set; } } /// @@ -106,51 +115,69 @@ namespace Twiglet.Sample.Event public SubscriptionType SubnType { get; set; } } - public class StoreSubscriber + public class StoreSubscriber { + /// + /// Name identifies this subscriber in the trace. + /// + /// + /// The name. + /// public string Name { get; set; } - + public StoreSubscriber(string n) { Name = n; } - - public void SubscribeToStore(StorePublisher store) { + + /// + /// We subscribe to both StorePublisher events. + /// + /// + /// Store. + /// + public void SubscribeToStore(StorePublisher store) { store.RaiseClearedEvent += new EventHandler(OnClearEvent); - store.RaiseStoredEvent += new EventHandler(OnStoreEvent); + store.RaiseStoredEvent += new EventHandler>(OnStoreEvent); } private void OnClearEvent(object sender, ClearEventArgs e) { Console.WriteLine(Name + ": " + e.Requestor + " cleared the Store"); } - private void OnStoreEvent(object sender, StoreEventArgs e) + private void OnStoreEvent(object sender, StoreEventArgs e) { - Console.WriteLine(Name + ": " + e.Requestor + " added " + e.Data + " to the Store"); + Console.WriteLine(Name + ": " + e.Requestor + " added " + e.Data.ToString() + " to the Store"); } } public static class EventSampler { - public static string MYID = "MAIN"; + public static string MYID = "StoreUser"; public static void EventSamplerMain(string[] args) { + + // Create StorePublisher instance + StorePublisher s = new StorePublisher(); - StorePublisher s = new StorePublisher(); - s.Add(MYID, "Store Ham"); - - // Add two subscribers - StoreSubscriber sub1 = new StoreSubscriber("Subscriber A"); - sub1.SubscribeToStore(s); - + // Add first subscriber + new StoreSubscriber("Subscriber A").SubscribeToStore(s); + + // Generates events for initial clear and the add + s.Add(MYID, "Store Ham"); + + // Generates an add event s.Add(MYID, "Store Eggs"); - - StoreSubscriber sub2 = new StoreSubscriber("Subscriber B"); - sub2.SubscribeToStore(s); + + // Add second subscriber + new StoreSubscriber("Subscriber B").SubscribeToStore(s); + + // Both subscribers are notified of add s.Add(MYID, "Store Milk"); - - s.Add("Main", "Hello World"); + + // Both subscribers are notified of clear + s.Clear(MYID); } }