summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2011-07-01 09:19:49 (GMT)
committerKevin Smith <git@kismith.co.uk>2011-07-01 09:19:49 (GMT)
commit2da71a8a85486a494343f1662d64fb5ae5a2a44e (patch)
tree23992f9f2a00bac23b345e5c2cc9c1194efc25be /src/com/isode/stroke/signals/Signal.java
downloadstroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.zip
stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.tar.bz2
Initial import
Diffstat (limited to 'src/com/isode/stroke/signals/Signal.java')
-rw-r--r--src/com/isode/stroke/signals/Signal.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/com/isode/stroke/signals/Signal.java b/src/com/isode/stroke/signals/Signal.java
new file mode 100644
index 0000000..cfa8665
--- /dev/null
+++ b/src/com/isode/stroke/signals/Signal.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2010, 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.Map;
+
+/**
+ * An approximation of the boost::signals system, although a little more warty.
+ */
+public class Signal {
+
+ private final Map<SignalConnection, Slot> binds_ = Collections.synchronizedMap(new HashMap<SignalConnection, Slot>());
+
+ public SignalConnection connect(Slot bind) {
+ final SignalConnection connection = new SignalConnection();
+ binds_.put(connection, bind);
+ connection.onDestroyed.connectWithoutReturn(new Slot() {
+
+ public void call() {
+ binds_.remove(connection);
+ }
+ });
+ return connection;
+ }
+
+ public SignalConnection connect(final Signal target) {
+ return connect(new Slot() {
+ public void call() {
+ target.emit();
+ }
+ });
+ }
+
+ void connectWithoutReturn(Slot bind) {
+ binds_.put(null, bind);
+ }
+
+ public void emit() {
+ ArrayList<Slot> binds = new ArrayList<Slot>();
+ binds.addAll(binds_.values());
+ for (Slot bind : binds) {
+ bind.call();
+ }
+ }
+
+ public void disconnectAll() {
+ binds_.clear();
+ }
+}