[3743] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3743] | 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 |
|
---|
[1564] | 22 | using System;
|
---|
[4068] | 23 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
[1564] | 24 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Persistence.Core {
|
---|
[1566] | 27 |
|
---|
[3004] | 28 | /// <summary>
|
---|
| 29 | /// Base class for serialization generators. Provides a common entry point
|
---|
[3016] | 30 | /// <c>Format</c> and dispatches to different abstract methods for
|
---|
[3004] | 31 | /// every token type.
|
---|
[3016] | 32 | /// </summary>
|
---|
| 33 | /// <typeparam name="T">The type of the serialization format.</typeparam>
|
---|
[1564] | 34 | public abstract class GeneratorBase<T> {
|
---|
[1623] | 35 |
|
---|
[3016] | 36 | /// <summary>
|
---|
| 37 | /// Processes a serialization token and formats the specified token.
|
---|
| 38 | /// </summary>
|
---|
| 39 | /// <param name="token">The token.</param>
|
---|
| 40 | /// <returns>An object suitable for serialziation</returns>
|
---|
[1564] | 41 | public T Format(ISerializationToken token) {
|
---|
| 42 | Type type = token.GetType();
|
---|
| 43 | if (type == typeof(BeginToken))
|
---|
| 44 | return Format((BeginToken)token);
|
---|
| 45 | if (type == typeof(EndToken))
|
---|
| 46 | return Format((EndToken)token);
|
---|
| 47 | if (type == typeof(PrimitiveToken))
|
---|
| 48 | return Format((PrimitiveToken)token);
|
---|
| 49 | if (type == typeof(ReferenceToken))
|
---|
| 50 | return Format((ReferenceToken)token);
|
---|
| 51 | if (type == typeof(NullReferenceToken))
|
---|
| 52 | return Format((NullReferenceToken)token);
|
---|
| 53 | if (type == typeof(MetaInfoBeginToken))
|
---|
| 54 | return Format((MetaInfoBeginToken)token);
|
---|
| 55 | if (type == typeof(MetaInfoEndToken))
|
---|
| 56 | return Format((MetaInfoEndToken)token);
|
---|
[3005] | 57 | if (type == typeof(TypeToken))
|
---|
| 58 | return Format((TypeToken)token);
|
---|
[1564] | 59 | throw new ApplicationException("Invalid token of type " + type.FullName);
|
---|
| 60 | }
|
---|
[1623] | 61 |
|
---|
[3016] | 62 | /// <summary>
|
---|
| 63 | /// Formats the specified begin token.
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <param name="beginToken">The begin token.</param>
|
---|
| 66 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 67 | protected abstract T Format(BeginToken beginToken);
|
---|
[3016] | 68 |
|
---|
| 69 | /// <summary>
|
---|
| 70 | /// Formats the specified end token.
|
---|
| 71 | /// </summary>
|
---|
| 72 | /// <param name="endToken">The end token.</param>
|
---|
| 73 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 74 | protected abstract T Format(EndToken endToken);
|
---|
[3016] | 75 |
|
---|
| 76 | /// <summary>
|
---|
| 77 | /// Formats the specified primitive token.
|
---|
| 78 | /// </summary>
|
---|
| 79 | /// <param name="primitiveToken">The primitive token.</param>
|
---|
| 80 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 81 | protected abstract T Format(PrimitiveToken primitiveToken);
|
---|
[3016] | 82 |
|
---|
| 83 | /// <summary>
|
---|
| 84 | /// Formats the specified reference token.
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <param name="referenceToken">The reference token.</param>
|
---|
| 87 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 88 | protected abstract T Format(ReferenceToken referenceToken);
|
---|
[3016] | 89 |
|
---|
| 90 | /// <summary>
|
---|
| 91 | /// Formats the specified null reference token.
|
---|
| 92 | /// </summary>
|
---|
| 93 | /// <param name="nullReferenceToken">The null reference token.</param>
|
---|
| 94 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 95 | protected abstract T Format(NullReferenceToken nullReferenceToken);
|
---|
[3016] | 96 |
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Formats the specified meta info begin token.
|
---|
| 99 | /// </summary>
|
---|
| 100 | /// <param name="metaInfoBeginToken">The meta info begin token.</param>
|
---|
| 101 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 102 | protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
|
---|
[3016] | 103 |
|
---|
| 104 | /// <summary>
|
---|
| 105 | /// Formats the specified meta info end token.
|
---|
| 106 | /// </summary>
|
---|
| 107 | /// <param name="metaInfoEndToken">The meta info end token.</param>
|
---|
| 108 | /// <returns>The token in serialized form.</returns>
|
---|
[1564] | 109 | protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
|
---|
[3016] | 110 |
|
---|
| 111 | /// <summary>
|
---|
| 112 | /// Formats the specified type token.
|
---|
| 113 | /// </summary>
|
---|
| 114 | /// <param name="typeToken">The type token.</param>
|
---|
| 115 | /// <returns>The token in serialized form.</returns>
|
---|
[3005] | 116 | protected abstract T Format(TypeToken typeToken);
|
---|
[1623] | 117 |
|
---|
[1566] | 118 | }
|
---|
[1564] | 119 | } |
---|