1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

update delegate and event sampler for website

This commit is contained in:
Kevin Glynn 2011-12-09 10:51:10 +01:00
parent d1b05c7411
commit 52a2e5b301
3 changed files with 68 additions and 43 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -9,7 +9,6 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>Tester</RootNamespace> <RootNamespace>Tester</RootNamespace>
<AssemblyName>Tester</AssemblyName> <AssemblyName>Tester</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags> <FileUpgradeFlags>
</FileUpgradeFlags> </FileUpgradeFlags>
<UpgradeBackupLocation> <UpgradeBackupLocation>

View File

@ -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 // 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, // and associated documentation files (the "Software"), to deal in the Software without restriction,
@ -28,7 +28,6 @@ namespace Twiglet.Sample.Delegate
/// </summary> /// </summary>
public class DelegateSampler public class DelegateSampler
{ {
public delegate void xProcessor<T>(ref T value);
// //
/// <summary> /// <summary>
/// LogWriters take a string and record it. /// LogWriters take a string and record it.
@ -37,10 +36,10 @@ namespace Twiglet.Sample.Delegate
public delegate void LogWriter(string logMessage); public delegate void LogWriter(string logMessage);
/// <summary> /// <summary>
/// A chain of delegates can't pass values to each other, (the value /// A chain of delegates can't pass values to each other, (only the value
/// returned from the final delegate is returned to the caller, but the /// returned from the final delegate is returned to the caller, the
/// values from intermediate delegates are just dropped on the floor). /// return values from intermediate delegates are just dropped on the floor).
/// However the delegates can communictae if we use ref parameters. /// However the delegates can communicate if we use ref parameters.
/// </summary> /// </summary>
/// <param name="value">the variable that we are processing</param> /// <param name="value">the variable that we are processing</param>
public delegate void Processor<T>(ref T value); public delegate void Processor<T>(ref T value);

View File

@ -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 // 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, // and associated documentation files (the "Software"), to deal in the Software without restriction,
@ -24,15 +24,24 @@ namespace Twiglet.Sample.Event
{ {
/// <summary> /// <summary>
/// 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.
///
/// </summary> /// </summary>
public class StorePublisher public class StorePublisher<T>
{ {
private List<string> store = null; private List<T> store = null;
// The events raised by this Store Publisher // The events raised by this Store Publisher
public event EventHandler<ClearEventArgs> RaiseClearedEvent; public event EventHandler<ClearEventArgs> RaiseClearedEvent;
public event EventHandler<StoreEventArgs> RaiseStoredEvent; public event EventHandler<StoreEventArgs<T>> RaiseStoredEvent;
// raise the Clear event // raise the Clear event
protected virtual void OnRaiseClearedEvent(ClearEventArgs e) protected virtual void OnRaiseClearedEvent(ClearEventArgs e)
@ -45,7 +54,7 @@ namespace Twiglet.Sample.Event
} }
// raise the Store event // raise the Store event
protected virtual void OnRaiseStoredEvent(StoreEventArgs e) protected virtual void OnRaiseStoredEvent(StoreEventArgs<T> e)
{ {
if (RaiseStoredEvent != null) if (RaiseStoredEvent != null)
{ {
@ -56,18 +65,18 @@ namespace Twiglet.Sample.Event
public void Clear(string requestor) public void Clear(string requestor)
{ {
store = new List<string>(); store = new List<T>();
OnRaiseClearedEvent(new ClearEventArgs(requestor)); OnRaiseClearedEvent(new ClearEventArgs(requestor));
} }
public void Add(string requestor, string data) public void Add(string requestor, T data)
{ {
if (store == null) if (store == null)
{ {
Clear("Add, needed to service " + requestor); Clear("Add");
} }
store.Add(data); store.Add(data);
OnRaiseStoredEvent(new StoreEventArgs(requestor, data)); OnRaiseStoredEvent(new StoreEventArgs<T>(requestor, data));
} }
} }
@ -84,11 +93,11 @@ namespace Twiglet.Sample.Event
/// <summary> /// <summary>
/// EventArgs derived class to pass info about a request to add data to store /// EventArgs derived class to pass info about a request to add data to store
/// </summary> /// </summary>
public class StoreEventArgs : EventArgs public class StoreEventArgs<T> : 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 Requestor { get; set; }
public string Data { get; set; } public T Data { get; set; }
} }
/// <summary> /// <summary>
@ -106,51 +115,69 @@ namespace Twiglet.Sample.Event
public SubscriptionType SubnType { get; set; } public SubscriptionType SubnType { get; set; }
} }
public class StoreSubscriber public class StoreSubscriber<T>
{ {
/// <summary>
/// Name identifies this subscriber in the trace.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; } public string Name { get; set; }
public StoreSubscriber(string n) public StoreSubscriber(string n)
{ {
Name = n; Name = n;
} }
public void SubscribeToStore(StorePublisher store) { /// <summary>
/// We subscribe to both StorePublisher events.
/// </summary>
/// <param name='store'>
/// Store.
/// </param>
public void SubscribeToStore(StorePublisher<T> store) {
store.RaiseClearedEvent += new EventHandler<ClearEventArgs>(OnClearEvent); store.RaiseClearedEvent += new EventHandler<ClearEventArgs>(OnClearEvent);
store.RaiseStoredEvent += new EventHandler<StoreEventArgs>(OnStoreEvent); store.RaiseStoredEvent += new EventHandler<StoreEventArgs<T>>(OnStoreEvent);
} }
private void OnClearEvent(object sender, ClearEventArgs e) private void OnClearEvent(object sender, ClearEventArgs e)
{ {
Console.WriteLine(Name + ": " + e.Requestor + " cleared the Store"); Console.WriteLine(Name + ": " + e.Requestor + " cleared the Store");
} }
private void OnStoreEvent(object sender, StoreEventArgs e) private void OnStoreEvent(object sender, StoreEventArgs<T> 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 class EventSampler
{ {
public static string MYID = "MAIN"; public static string MYID = "StoreUser";
public static void EventSamplerMain(string[] args) public static void EventSamplerMain(string[] args)
{ {
// Create StorePublisher instance
StorePublisher<string> s = new StorePublisher<string>();
StorePublisher s = new StorePublisher(); // Add first subscriber
s.Add(MYID, "Store Ham"); new StoreSubscriber<string>("Subscriber A").SubscribeToStore(s);
// Add two subscribers // Generates events for initial clear and the add
StoreSubscriber sub1 = new StoreSubscriber("Subscriber A"); s.Add(MYID, "Store Ham");
sub1.SubscribeToStore(s);
// Generates an add event
s.Add(MYID, "Store Eggs"); s.Add(MYID, "Store Eggs");
StoreSubscriber sub2 = new StoreSubscriber("Subscriber B"); // Add second subscriber
sub2.SubscribeToStore(s); new StoreSubscriber<string>("Subscriber B").SubscribeToStore(s);
// Both subscribers are notified of add
s.Add(MYID, "Store Milk"); s.Add(MYID, "Store Milk");
s.Add("Main", "Hello World"); // Both subscribers are notified of clear
s.Clear(MYID);
} }
} }