Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/ValueParameter.cs @ 17567

Last change on this file since 17567 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 2.6 KB
RevLine 
[2891]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2891]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;
[5843]23using System.Reflection;
[16946]24using HEAL.Attic;
[4722]25using HeuristicLab.Common;
[2891]26using HeuristicLab.Core;
27
28namespace HeuristicLab.Parameters {
29  /// <summary>
[2947]30  /// A parameter whose value is defined in the parameter itself.
[2891]31  /// </summary>
[3822]32  [Item("ValueParameter", "A parameter whose value is defined in the parameter itself.")]
[16723]33  [StorableType("CB9B83B6-DE2B-45CC-AB0B-C551E1A6E0BD")]
[2924]34  public class ValueParameter<T> : OptionalValueParameter<T> where T : class, IItem {
35    public override T Value {
36      get { return base.Value; }
[2891]37      set {
[2948]38        if ((value == null) && (Value != null)) throw new ArgumentNullException();
[2924]39        base.Value = value;
[2891]40      }
41    }
42
[4722]43    [StorableConstructor]
[16723]44    protected ValueParameter(StorableConstructorFlag _) : base(_) { }
[4722]45    protected ValueParameter(ValueParameter<T> original, Cloner cloner) : base(original, cloner) { }
[5843]46
[2948]47    public ValueParameter() : base() { }
[5843]48    public ValueParameter(string name) : base(name) { base.Value = CreateDefaultValue(); }
[2924]49    public ValueParameter(string name, T value) : base(name, value) { }
[5843]50    public ValueParameter(string name, string description) : base(name, description) { base.Value = CreateDefaultValue(); }
[2924]51    public ValueParameter(string name, string description, T value) : base(name, description, value) { }
[16946]52
[6025]53    protected T CreateDefaultValue() {
54      Type type = typeof(T);
55      if (type.IsAbstract) return null;
56      ConstructorInfo defaultConstructor = type.GetConstructor(Type.EmptyTypes);
57      if (defaultConstructor == null) return null;
[4722]58
[6025]59      return (T)defaultConstructor.Invoke(new object[0]);
[5843]60    }
61
[4722]62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ValueParameter<T>(this, cloner);
64    }
[2891]65  }
66}
Note: See TracBrowser for help on using the repository browser.