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