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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
24 | using HeuristicLab.Persistence.Interfaces;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Persistence.Core {
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Base class for serialization generators. Provides a common entry point
|
---|
30 | /// <c>Format</c> and dispatches to different abstract methods for
|
---|
31 | /// every token type.
|
---|
32 | /// </summary>
|
---|
33 | /// <typeparam name="T">The type of the serialization format.</typeparam>
|
---|
34 | public abstract class GeneratorBase<T> {
|
---|
35 |
|
---|
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>
|
---|
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);
|
---|
57 | if (type == typeof(TypeToken))
|
---|
58 | return Format((TypeToken)token);
|
---|
59 | throw new ApplicationException("Invalid token of type " + type.FullName);
|
---|
60 | }
|
---|
61 |
|
---|
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>
|
---|
67 | protected abstract T Format(BeginToken beginToken);
|
---|
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>
|
---|
74 | protected abstract T Format(EndToken endToken);
|
---|
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>
|
---|
81 | protected abstract T Format(PrimitiveToken primitiveToken);
|
---|
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>
|
---|
88 | protected abstract T Format(ReferenceToken referenceToken);
|
---|
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>
|
---|
95 | protected abstract T Format(NullReferenceToken nullReferenceToken);
|
---|
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>
|
---|
102 | protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
|
---|
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>
|
---|
109 | protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
|
---|
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>
|
---|
116 | protected abstract T Format(TypeToken typeToken);
|
---|
117 |
|
---|
118 | }
|
---|
119 | } |
---|