[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
| 22 | using System;
|
---|
[3306] | 23 | using System.Drawing;
|
---|
[2] | 24 | using System.Text;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
[1853] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Data {
|
---|
[3048] | 30 | [Item("BoolValue", "Represents a boolean value.")]
|
---|
[3017] | 31 | [StorableClass]
|
---|
[3054] | 32 | public class BoolValue : ValueTypeValue<bool>, IComparable, IStringConvertibleValue {
|
---|
[13656] | 33 | public static new Image StaticItemImage
|
---|
| 34 | {
|
---|
| 35 | get { return new Bitmap(25, 25); }
|
---|
[3306] | 36 | }
|
---|
| 37 |
|
---|
[4722] | 38 | protected BoolValue(BoolValue original, Cloner cloner)
|
---|
| 39 | : base(original, cloner) {
|
---|
| 40 | }
|
---|
[3048] | 41 | public BoolValue() : base() { }
|
---|
[4722] | 42 | [StorableConstructor]
|
---|
| 43 | public BoolValue(bool value)
|
---|
| 44 | : base(value) {
|
---|
| 45 | //mkommend: Be aware that the base call refers to the storable ctor => the value is set explicitly in the ctor body.
|
---|
| 46 | // This should not affect the persistence, because first the ctor is called and afterwards the values are set by reflection.
|
---|
| 47 | this.value = value;
|
---|
| 48 | }
|
---|
[2] | 49 |
|
---|
[13827] | 50 | public static BoolValue Parse(string val)
|
---|
| 51 | {
|
---|
| 52 | if (val == "true" || val == "on")
|
---|
| 53 | return new BoolValue(true);
|
---|
| 54 | else
|
---|
| 55 | return new BoolValue(false);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 59 | return new BoolValue(this, cloner);
|
---|
[2] | 60 | }
|
---|
[2665] | 61 |
|
---|
[3054] | 62 | public virtual int CompareTo(object obj) {
|
---|
[3048] | 63 | BoolValue other = obj as BoolValue;
|
---|
[2757] | 64 | if (other != null)
|
---|
| 65 | return Value.CompareTo(other.Value);
|
---|
| 66 | else
|
---|
| 67 | return Value.CompareTo(obj);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[3054] | 70 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 71 | bool val;
|
---|
| 72 | bool valid = bool.TryParse(value, out val);
|
---|
| 73 | errorMessage = string.Empty;
|
---|
| 74 | if (!valid) {
|
---|
| 75 | StringBuilder sb = new StringBuilder();
|
---|
| 76 | sb.Append("Invalid Value (Valid Value Format: \"");
|
---|
| 77 | sb.Append(FormatPatterns.GetBoolFormatPattern());
|
---|
| 78 | sb.Append("\")");
|
---|
| 79 | errorMessage = sb.ToString();
|
---|
| 80 | }
|
---|
| 81 | return valid;
|
---|
[2676] | 82 | }
|
---|
[3054] | 83 | protected virtual string GetValue() {
|
---|
[2665] | 84 | return Value.ToString();
|
---|
| 85 | }
|
---|
[3054] | 86 | protected virtual bool SetValue(string value) {
|
---|
[2694] | 87 | bool val;
|
---|
| 88 | if (bool.TryParse(value, out val)) {
|
---|
| 89 | Value = val;
|
---|
[2665] | 90 | return true;
|
---|
| 91 | } else {
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[3054] | 95 |
|
---|
| 96 | #region IStringConvertibleValue Members
|
---|
| 97 | bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
|
---|
| 98 | return Validate(value, out errorMessage);
|
---|
| 99 | }
|
---|
| 100 | string IStringConvertibleValue.GetValue() {
|
---|
| 101 | return GetValue();
|
---|
| 102 | }
|
---|
| 103 | bool IStringConvertibleValue.SetValue(string value) {
|
---|
| 104 | return SetValue(value);
|
---|
| 105 | }
|
---|
[2676] | 106 | #endregion
|
---|
[2] | 107 | }
|
---|
| 108 | }
|
---|