[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12969] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[1456] | 24 | using HeuristicLab.Persistence.Core;
|
---|
[4068] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1456] | 26 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 27 |
|
---|
[1823] | 28 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1566] | 29 |
|
---|
[3017] | 30 | [StorableClass]
|
---|
[3036] | 31 | internal sealed class EnumSerializer : ICompositeSerializer {
|
---|
[1456] | 32 |
|
---|
[4806] | 33 | [StorableConstructor]
|
---|
| 34 | private EnumSerializer(bool deserializing) { }
|
---|
| 35 | public EnumSerializer() { }
|
---|
| 36 |
|
---|
[1539] | 37 | public int Priority {
|
---|
| 38 | get { return 100; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[1823] | 41 | public bool CanSerialize(Type type) {
|
---|
[1542] | 42 | return type.IsEnum || type == typeof(Enum);
|
---|
[1456] | 43 | }
|
---|
| 44 |
|
---|
[2993] | 45 | public string JustifyRejection(Type type) {
|
---|
| 46 | return "not an enum and not System.Enum";
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[1553] | 49 | public IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
[1684] | 50 | yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
|
---|
[1456] | 51 | }
|
---|
| 52 |
|
---|
[1553] | 53 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
| 54 | return new Tag[] { };
|
---|
[1456] | 55 | }
|
---|
[1553] | 56 |
|
---|
| 57 | public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
|
---|
| 58 | IEnumerator<Tag> it = metaInfo.GetEnumerator();
|
---|
[1625] | 59 | try {
|
---|
| 60 | it.MoveNext();
|
---|
| 61 | return Enum.Parse(t, (string)it.Current.Value);
|
---|
[4068] | 62 | }
|
---|
| 63 | catch (InvalidOperationException e) {
|
---|
[1625] | 64 | throw new PersistenceException("not enough meta information to recstruct enum", e);
|
---|
[4068] | 65 | }
|
---|
| 66 | catch (InvalidCastException e) {
|
---|
[1625] | 67 | throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
|
---|
| 68 | }
|
---|
[1456] | 69 | }
|
---|
[1566] | 70 |
|
---|
| 71 | public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
|
---|
[1625] | 72 | // Enums are already populated during instance creation.
|
---|
[1553] | 73 | }
|
---|
[1456] | 74 | }
|
---|
[1684] | 75 | } |
---|