Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting/3.3/UserScriptBase.cs @ 10511

Last change on this file since 10511 was 10511, checked in by jkarder, 10 years ago

#2136: added Contains(string) method to check if a variable is present in the VariableStore

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Dynamic;
24using System.IO;
25using System.Text;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Scripting {
29  public abstract class UserScriptBase {
30    protected dynamic vars;
31
32    private readonly EventWriter console;
33    protected EventWriter Console {
34      get { return console; }
35    }
36
37    protected UserScriptBase() {
38      console = new EventWriter(this);
39    }
40
41    public abstract void Main();
42
43    private void Execute(VariableStore variableStore) {
44      vars = new Variables(variableStore);
45      Main();
46    }
47
48    protected internal event EventHandler<EventArgs<string>> ConsoleOutputChanged;
49    private void OnConsoleOutputChanged(string args) {
50      var handler = ConsoleOutputChanged;
51      if (handler != null) handler(null, new EventArgs<string>(args));
52    }
53
54    protected class Variables : DynamicObject {
55      private readonly VariableStore variableStore;
56
57      public Variables(VariableStore variableStore) {
58        this.variableStore = variableStore;
59      }
60
61      public override bool TryGetMember(GetMemberBinder binder, out object result) {
62        return variableStore.TryGetValue(binder.Name, out result);
63      }
64
65      public override bool TrySetMember(SetMemberBinder binder, object value) {
66        variableStore[binder.Name] = value;
67        return true;
68      }
69
70      public bool Contains(string variableName) {
71        return variableStore.ContainsKey(variableName);
72      }
73    }
74
75    protected class EventWriter : TextWriter {
76      private readonly UserScriptBase usb;
77
78      public EventWriter(UserScriptBase usb) {
79        this.usb = usb;
80      }
81
82      public override Encoding Encoding {
83        get { return Encoding.UTF8; }
84      }
85
86      #region Write/WriteLine Overrides
87      #region Write
88      public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); }
89      public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); }
90      public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); }
91      public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); }
92      public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); }
93      public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); }
94      public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); }
95      public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); }
96      public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); }
97      public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); }
98      public override void Write(string value) { usb.OnConsoleOutputChanged(value); }
99      public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); }
100      public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }
101      public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }
102      public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); }
103      public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); }
104      public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); }
105      #endregion
106
107      #region WriteLine
108      public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); }
109      public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
110      public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
111      public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }
112      public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }
113      public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
114      public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
115      public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
116      public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
117      public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
118      public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
119      public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
120      public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }
121      public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }
122      public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }
123      public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }
124      public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
125      public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
126      #endregion
127      #endregion
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.