Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Scripting/3.3/UserScriptBase.cs @ 12438

Last change on this file since 12438 was 10566, checked in by abeham, 11 years ago

#2136:

  • Made Variables class public (could be used in other scripts as well)
  • Renamed ItemName to "C# Script"
File size: 6.2 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.Collections;
24using System.Collections.Generic;
25using System.Dynamic;
26using System.IO;
27using System.Text;
28using HeuristicLab.Common;
29
30namespace HeuristicLab.Scripting {
31  public abstract class UserScriptBase {
32    protected dynamic vars;
33
34    private readonly EventWriter console;
35    protected EventWriter Console {
36      get { return console; }
37    }
38
39    protected UserScriptBase() {
40      console = new EventWriter(this);
41    }
42
43    public abstract void Main();
44
45    private void Execute(VariableStore variableStore) {
46      vars = new Variables(variableStore);
47      Main();
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
56    protected class EventWriter : TextWriter {
57      private readonly UserScriptBase usb;
58
59      public EventWriter(UserScriptBase usb) {
60        this.usb = usb;
61      }
62
63      public override Encoding Encoding {
64        get { return Encoding.UTF8; }
65      }
66
67      #region Write/WriteLine Overrides
68      #region Write
69      public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); }
70      public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); }
71      public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); }
72      public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); }
73      public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); }
74      public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); }
75      public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); }
76      public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); }
77      public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); }
78      public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); }
79      public override void Write(string value) { usb.OnConsoleOutputChanged(value); }
80      public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); }
81      public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }
82      public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }
83      public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); }
84      public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); }
85      public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); }
86      #endregion
87
88      #region WriteLine
89      public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); }
90      public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
91      public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
92      public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }
93      public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }
94      public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
95      public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
96      public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
97      public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
98      public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
99      public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
100      public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
101      public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }
102      public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }
103      public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }
104      public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }
105      public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
106      public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }
107      #endregion
108      #endregion
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.