Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting/3.3/CSharpScriptBase.cs @ 11507

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

#2262:

  • IEnumerable<T> extension methods are supported by the variables collection
  • INamedItem variables get the item's name per default
  • variables can be renamed by pressing F2
  • variables are removed faster
  • multiple variables can be selected
  • the code editor supports scrolling while scripts are executed
File size: 6.3 KB
RevLine 
[10731]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.IO;
24using System.Text;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Scripting {
28  public abstract class CSharpScriptBase {
[11436]29    protected Variables variables;
[10731]30    protected dynamic vars;
31
32    private readonly EventWriter console;
33    protected EventWriter Console {
34      get { return console; }
35    }
36
37    protected CSharpScriptBase() {
38      console = new EventWriter(this);
39    }
40
41    public abstract void Main();
42
43    internal void Execute(VariableStore variableStore) {
[11436]44      variables = vars = new Variables(variableStore);
[10731]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 EventWriter : TextWriter {
[10857]55      private readonly CSharpScriptBase script;
[10731]56
[10857]57      public EventWriter(CSharpScriptBase script) {
58        this.script = script;
[10731]59      }
60
61      public override Encoding Encoding {
62        get { return Encoding.UTF8; }
63      }
64
65      #region Write/WriteLine Overrides
66      #region Write
[10857]67      public override void Write(bool value) { script.OnConsoleOutputChanged(value.ToString()); }
68      public override void Write(char value) { script.OnConsoleOutputChanged(value.ToString()); }
69      public override void Write(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer)); }
70      public override void Write(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count)); }
71      public override void Write(decimal value) { script.OnConsoleOutputChanged(value.ToString()); }
72      public override void Write(double value) { script.OnConsoleOutputChanged(value.ToString()); }
73      public override void Write(float value) { script.OnConsoleOutputChanged(value.ToString()); }
74      public override void Write(int value) { script.OnConsoleOutputChanged(value.ToString()); }
75      public override void Write(long value) { script.OnConsoleOutputChanged(value.ToString()); }
76      public override void Write(object value) { script.OnConsoleOutputChanged(value.ToString()); }
77      public override void Write(string value) { script.OnConsoleOutputChanged(value); }
78      public override void Write(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0)); }
79      public override void Write(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }
80      public override void Write(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }
81      public override void Write(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg)); }
82      public override void Write(uint value) { script.OnConsoleOutputChanged(value.ToString()); }
83      public override void Write(ulong value) { script.OnConsoleOutputChanged(value.ToString()); }
[10731]84      #endregion
85
86      #region WriteLine
[10857]87      public override void WriteLine() { script.OnConsoleOutputChanged(Environment.NewLine); }
88      public override void WriteLine(bool value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
89      public override void WriteLine(char value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
90      public override void WriteLine(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }
91      public override void WriteLine(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }
92      public override void WriteLine(decimal value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
93      public override void WriteLine(double value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
94      public override void WriteLine(float value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
95      public override void WriteLine(int value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
96      public override void WriteLine(long value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
97      public override void WriteLine(object value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
98      public override void WriteLine(string value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
99      public override void WriteLine(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }
100      public override void WriteLine(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }
101      public override void WriteLine(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }
102      public override void WriteLine(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }
103      public override void WriteLine(uint value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
104      public override void WriteLine(ulong value) { script.OnConsoleOutputChanged(value + Environment.NewLine); }
[10731]105      #endregion
106      #endregion
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.