Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10550 was 10512, checked in by jkarder, 11 years ago

#2136:

  • the Variables class now implements IEnumerable<T>
  • added Keys and Values properties to the Variables class
File size: 7.3 KB
RevLine 
[10506]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;
[10512]23using System.Collections;
24using System.Collections.Generic;
[10332]25using System.Dynamic;
26using System.IO;
27using System.Text;
28using HeuristicLab.Common;
29
[10506]30namespace HeuristicLab.Scripting {
[10401]31  public abstract class UserScriptBase {
[10358]32    protected dynamic vars;
[10332]33
[10358]34    private readonly EventWriter console;
35    protected EventWriter Console {
36      get { return console; }
37    }
38
[10401]39    protected UserScriptBase() {
[10358]40      console = new EventWriter(this);
41    }
42
[10332]43    public abstract void Main();
44
45    private void Execute(VariableStore variableStore) {
46      vars = new Variables(variableStore);
[10506]47      Main();
[10332]48    }
49
50    protected internal event EventHandler<EventArgs<string>> ConsoleOutputChanged;
51    private void OnConsoleOutputChanged(string args) {
52      var handler = ConsoleOutputChanged;
53      if (handler != null) handler(null, new EventArgs<string>(args));
54    }
55
[10512]56    protected class Variables : DynamicObject, IEnumerable<KeyValuePair<string, object>> {
[10332]57      private readonly VariableStore variableStore;
58
[10512]59      public ICollection<string> Keys {
60        get { return variableStore.Keys; }
61      }
62
63      public ICollection<object> Values {
64        get { return variableStore.Values; }
65      }
66
[10332]67      public Variables(VariableStore variableStore) {
68        this.variableStore = variableStore;
69      }
70
71      public override bool TryGetMember(GetMemberBinder binder, out object result) {
72        return variableStore.TryGetValue(binder.Name, out result);
73      }
74
75      public override bool TrySetMember(SetMemberBinder binder, object value) {
76        variableStore[binder.Name] = value;
77        return true;
78      }
[10511]79
80      public bool Contains(string variableName) {
81        return variableStore.ContainsKey(variableName);
82      }
[10512]83
84      public IEnumerator<KeyValuePair<string, object>> GetEnumerator() {
85        return variableStore.GetEnumerator();
86      }
87
88      IEnumerator IEnumerable.GetEnumerator() {
89        return GetEnumerator();
90      }
[10332]91    }
92
[10358]93    protected class EventWriter : TextWriter {
[10401]94      private readonly UserScriptBase usb;
[10332]95
[10401]96      public EventWriter(UserScriptBase usb) {
97        this.usb = usb;
[10332]98      }
99
100      public override Encoding Encoding {
101        get { return Encoding.UTF8; }
102      }
103
104      #region Write/WriteLine Overrides
105      #region Write
[10401]106      public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); }
107      public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); }
108      public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); }
109      public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); }
110      public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); }
111      public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); }
112      public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); }
113      public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); }
114      public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); }
115      public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); }
116      public override void Write(string value) { usb.OnConsoleOutputChanged(value); }
117      public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); }
118      public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }
119      public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }
120      public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); }
121      public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); }
122      public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); }
[10332]123      #endregion
124
125      #region WriteLine
[10401]126      public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); }
127      public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
128      public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
129      public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }
130      public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }
131      public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
132      public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
133      public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
134      public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
135      public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
136      public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
137      public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
138      public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }
139      public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }
140      public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }
141      public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }
142      public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
143      public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
[10332]144      #endregion
145      #endregion
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.