diff options
author | Robert Williams <robert.williams@isode.com> | 2013-04-22 15:23:48 (GMT) |
---|---|---|
committer | Robert Williams <robert.williams@isode.com> | 2013-04-22 15:34:44 (GMT) |
commit | 3335b1dfa9b429d90088184f58daff01e84432f2 (patch) | |
tree | 39368bb28f8d5c8c9620513a10e196ee21e818bf /src/com/isode | |
parent | b50d3949d81d8cece6c71c2b094297317e584308 (diff) | |
download | stroke-3335b1dfa9b429d90088184f58daff01e84432f2.zip stroke-3335b1dfa9b429d90088184f58daff01e84432f2.tar.bz2 |
Add 4 parameter signal/slot object
Direct copy of current signal/slot implementation,
with 4 generic parameters.
Change-Id: I4b2cb37fd134e80e8481950030b6e8721f4f2854
Diffstat (limited to 'src/com/isode')
-rw-r--r-- | src/com/isode/stroke/signals/Signal4.java | 66 | ||||
-rw-r--r-- | src/com/isode/stroke/signals/Slot4.java | 24 |
2 files changed, 90 insertions, 0 deletions
diff --git a/src/com/isode/stroke/signals/Signal4.java b/src/com/isode/stroke/signals/Signal4.java new file mode 100644 index 0000000..6a29641 --- /dev/null +++ b/src/com/isode/stroke/signals/Signal4.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2012-2013, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.signals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.signals.Slot; + + +/** + * An approximation of the boost::signals system with 4 parameters + * @param <T1> Type 1 + * @param <T2> Type 2 + * @param <T3> Type 3 + * @param <T4> Type 4 + */ +public class Signal4<T1, T2, T3, T4> { + private final Map<SignalConnection, Slot4<T1, T2, T3, T4> > binds_ = Collections.synchronizedMap( + new HashMap<SignalConnection, Slot4<T1, T2, T3, T4> >()); + + /** + * Add a slot which will be notified + * @param bind slot, not null + * @return signal connection + */ + public SignalConnection connect(Slot4<T1, T2, T3, T4> bind) { + final SignalConnection connection = new SignalConnection(); + binds_.put(connection, bind); + connection.onDestroyed.connect(new Slot() { + public void call() { + binds_.remove(connection); + } + }); + return connection; + } + + /** + * Notify all slots(listeners) + * @param p1 parameter value 1 + * @param p2 parameter value 2 + * @param p3 parameter value 3 + * @param p4 parameter value 4 + */ + public void emit(T1 p1, T2 p2, T3 p3, T4 p4) { + List<Slot4<T1, T2, T3, T4>> binds = new ArrayList<Slot4<T1, T2, T3, T4>>(); + binds.addAll(binds_.values()); + for (Slot4<T1, T2, T3, T4> bind : binds) { + bind.call(p1, p2, p3, p4); + } + } + + /** + * Remove all slots(listeners) + */ + public void disconnectAll() { + binds_.clear(); + } +} diff --git a/src/com/isode/stroke/signals/Slot4.java b/src/com/isode/stroke/signals/Slot4.java new file mode 100644 index 0000000..02e6fd9 --- /dev/null +++ b/src/com/isode/stroke/signals/Slot4.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 - 2013, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.signals; + +/** + * Bind class for connecting to a signal with 4 parameters. + * @param <T1> Type 1 + * @param <T2> Type 2 + * @param <T3> Type 3 + * @param <T4> Type 4 + */ +public interface Slot4<T1, T2, T3, T4> { + /** + * This method will be called on notification from a signal + * @param p1 parameter value 1 + * @param p2 parameter value 2 + * @param p3 parameter value 3 + * @param p4 parameter value 4 + */ + void call(T1 p1, T2 p2, T3 p3, T4 p4); +} |