mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
Add events sampler
This commit is contained in:
parent
78dc44643f
commit
9109c54143
@ -1,25 +1,26 @@
|
|||||||
using System;
|
using System;
|
||||||
namespace Tester
|
namespace Tester
|
||||||
{
|
{
|
||||||
public class MainClass
|
public class MainClass
|
||||||
{
|
{
|
||||||
public static void Main (string[] args)
|
public static void Main (string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
//Tester.DelegateUser.DelegateTest<int>.DelMain(args);
|
//Tester.DelegateUser.DelegateTest<int>.DelMain(args);
|
||||||
|
|
||||||
//Tester.RefOut.RefOutTest.RefOutMain(args);
|
//Tester.RefOut.RefOutTest.RefOutMain(args);
|
||||||
|
|
||||||
//Tester.PartialUser.PartialMain(args);
|
//Tester.PartialUser.PartialMain(args);
|
||||||
|
|
||||||
//Tester.Delegates.EventHandler.Sample.EHMain();
|
//Tester.Delegates.EventHandler.Sample.EHMain();
|
||||||
|
|
||||||
// Tester.Misc.RenameTest.RNMain("hello world");
|
// Tester.Misc.RenameTest.RNMain("hello world");
|
||||||
//Tester.Misc.Sorter.Example.SorterMain();
|
//Tester.Misc.Sorter.Example.SorterMain();
|
||||||
//Tester.Misc.RegexTest.RegexMain();
|
//Tester.Misc.RegexTest.RegexMain();
|
||||||
Twiglet.Sample.Delegate.DelegateSampler.DelegateSamplerMain();
|
Twiglet.Sample.Delegate.DelegateSampler.DelegateSamplerMain();
|
||||||
Console.WriteLine("Finished");
|
Twiglet.Sample.Event.EventSampler.EventSamplerMain(args);
|
||||||
}
|
Console.WriteLine("Finished");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,7 @@
|
|||||||
<Compile Include="Misc\newmodifier.cs" />
|
<Compile Include="Misc\newmodifier.cs" />
|
||||||
<Compile Include="Misc\Sorter.cs" />
|
<Compile Include="Misc\Sorter.cs" />
|
||||||
<Compile Include="Misc\Regex\RegexTest.cs" />
|
<Compile Include="Misc\Regex\RegexTest.cs" />
|
||||||
|
<Compile Include="WebEvents\EventSampler.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup />
|
<ItemGroup />
|
||||||
|
158
CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs
Executable file
158
CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs
Executable file
@ -0,0 +1,158 @@
|
|||||||
|
// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com)
|
||||||
|
//
|
||||||
|
// The MIT License (MIT)
|
||||||
|
//
|
||||||
|
// 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,
|
||||||
|
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
// subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||||
|
// portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||||
|
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Twiglet.Sample.Event
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sample class to show off CS2J's translations for events
|
||||||
|
/// </summary>
|
||||||
|
public class StorePublisher
|
||||||
|
{
|
||||||
|
private List<string> store = null;
|
||||||
|
|
||||||
|
// The events raised by this Store Publisher
|
||||||
|
public event EventHandler<ClearEventArgs> RaiseClearedEvent;
|
||||||
|
public event EventHandler<StoreEventArgs> RaiseStoredEvent;
|
||||||
|
|
||||||
|
// raise the Clear event
|
||||||
|
protected virtual void OnRaiseClearedEvent(ClearEventArgs e)
|
||||||
|
{
|
||||||
|
if (RaiseClearedEvent != null)
|
||||||
|
{
|
||||||
|
// invoke the waiting subscribers.
|
||||||
|
RaiseClearedEvent(this, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// raise the Store event
|
||||||
|
protected virtual void OnRaiseStoredEvent(StoreEventArgs e)
|
||||||
|
{
|
||||||
|
if (RaiseStoredEvent != null)
|
||||||
|
{
|
||||||
|
// invoke the waiting subscribers.
|
||||||
|
RaiseStoredEvent(this, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear(string requestor)
|
||||||
|
{
|
||||||
|
store = new List<string>();
|
||||||
|
OnRaiseClearedEvent(new ClearEventArgs(requestor));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(string requestor, string data)
|
||||||
|
{
|
||||||
|
if (store == null)
|
||||||
|
{
|
||||||
|
Clear("Add, needed to service " + requestor);
|
||||||
|
}
|
||||||
|
store.Add(data);
|
||||||
|
OnRaiseStoredEvent(new StoreEventArgs(requestor, data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EventArgs derived class to pass info about a request to clear store
|
||||||
|
/// </summary>
|
||||||
|
public class ClearEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public ClearEventArgs(string req) { this.Requestor = req; }
|
||||||
|
public string Requestor { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EventArgs derived class to pass info about a request to add data to store
|
||||||
|
/// </summary>
|
||||||
|
public class StoreEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public StoreEventArgs(string req, string data) { this.Requestor = req; this.Data = data; }
|
||||||
|
public string Requestor { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Info about the type of subscription a subscriber requested
|
||||||
|
/// </summary>
|
||||||
|
public enum SubscriptionType { NewSubscriber, ClearRequest, StoreData }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EventArgs derived class to pass info about a new subscriber
|
||||||
|
/// </summary>
|
||||||
|
public class SubscribeEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public SubscribeEventArgs(string sub, SubscriptionType ty) { this.Subscriber = sub; this.SubnType = ty; }
|
||||||
|
public string Subscriber { get; set; }
|
||||||
|
public SubscriptionType SubnType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StoreSubscriber
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public StoreSubscriber(string n)
|
||||||
|
{
|
||||||
|
Name = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SubscribeToStore(StorePublisher store) {
|
||||||
|
store.RaiseClearedEvent += new EventHandler<ClearEventArgs>(OnClearEvent);
|
||||||
|
store.RaiseStoredEvent += new EventHandler<StoreEventArgs>(OnStoreEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnClearEvent(object sender, ClearEventArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(Name + ": " + e.Requestor + " cleared the Store");
|
||||||
|
}
|
||||||
|
private void OnStoreEvent(object sender, StoreEventArgs e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(Name + ": " + e.Requestor + " added " + e.Data + " to the Store");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EventSampler
|
||||||
|
{
|
||||||
|
public static string MYID = "MAIN";
|
||||||
|
|
||||||
|
public static void EventSamplerMain(string[] args)
|
||||||
|
{
|
||||||
|
|
||||||
|
StorePublisher s = new StorePublisher();
|
||||||
|
s.Add(MYID, "Store Ham");
|
||||||
|
|
||||||
|
// Add two subscribers
|
||||||
|
StoreSubscriber sub1 = new StoreSubscriber("Subscriber A");
|
||||||
|
sub1.SubscribeToStore(s);
|
||||||
|
|
||||||
|
s.Add(MYID, "Store Eggs");
|
||||||
|
|
||||||
|
StoreSubscriber sub2 = new StoreSubscriber("Subscriber B");
|
||||||
|
sub2.SubscribeToStore(s);
|
||||||
|
s.Add(MYID, "Store Milk");
|
||||||
|
|
||||||
|
s.Add("Main", "Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user