1 package org.simantics.scl.runtime;
3 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
5 import gnu.trove.map.hash.THashMap;
7 public class SCLContext extends THashMap<String,Object> {
8 private static ThreadLocal<SCLContext> CONTEXT = new ThreadLocal<SCLContext>();
9 private static ThreadLocal<OldContextNode> OLD_CONTEXT = new ThreadLocal<OldContextNode>();
11 private static class OldContextNode {
12 final SCLContext context;
13 final OldContextNode next;
15 public OldContextNode(SCLContext context, OldContextNode next) {
16 this.context = context;
21 public static SCLContext getCurrent() {
22 SCLContext context = CONTEXT.get();
24 context = new SCLContext();
30 public static void push(SCLContext newContext) {
31 SCLContext oldContext = CONTEXT.get();
32 if(oldContext != null)
33 OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get()));
34 CONTEXT.set(newContext);
38 * Creates a new context based on some properties of the current context.
39 * The new context is safe for use in parallel threads.
41 public static SCLContext createDerivedContext() {
42 SCLContext newContext = new SCLContext();
44 SCLContext baseContext = CONTEXT.get();
45 if(baseContext != null) {
46 Object reportingHandler = baseContext.get(SCLReportingHandler.REPORTING_HANDLER);
47 if(reportingHandler != null)
48 newContext.put(SCLReportingHandler.REPORTING_HANDLER, reportingHandler);
53 public static void pop() {
54 OldContextNode node = OLD_CONTEXT.get();
58 CONTEXT.set(node.context);
59 OLD_CONTEXT.set(node.next);
65 public Object get(Object key) {
66 Object result = super.get(key);
67 System.out.println(this + ": " + key + " -> " + result);
72 public Object put(String key, Object value) {
73 System.out.println(this + ": " + key + " <- " + value);
74 return super.put(key, value);