Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs @ 6737

Last change on this file since 6737 was 6737, checked in by epitzer, 13 years ago

#1530 Split type and serializer tokens and include special handling for CachedTypeSerializer (this should also fix #1527)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using HeuristicLab.Persistence.Core.Tokens;
24using HeuristicLab.Persistence.Interfaces;
25
26namespace 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      if (type == typeof(SerializerToken))
60        return Format((SerializerToken)token);
61      throw new ApplicationException("Invalid token of type " + type.FullName);
62    }
63
64    /// <summary>
65    /// Formats the specified begin token.
66    /// </summary>
67    /// <param name="beginToken">The begin token.</param>
68    /// <returns>The token in serialized form.</returns>
69    protected abstract T Format(BeginToken beginToken);
70
71    /// <summary>
72    /// Formats the specified end token.
73    /// </summary>
74    /// <param name="endToken">The end token.</param>
75    /// <returns>The token in serialized form.</returns>
76    protected abstract T Format(EndToken endToken);
77
78    /// <summary>
79    /// Formats the specified primitive token.
80    /// </summary>
81    /// <param name="primitiveToken">The primitive token.</param>
82    /// <returns>The token in serialized form.</returns>
83    protected abstract T Format(PrimitiveToken primitiveToken);
84
85    /// <summary>
86    /// Formats the specified reference token.
87    /// </summary>
88    /// <param name="referenceToken">The reference token.</param>
89    /// <returns>The token in serialized form.</returns>
90    protected abstract T Format(ReferenceToken referenceToken);
91
92    /// <summary>
93    /// Formats the specified null reference token.
94    /// </summary>
95    /// <param name="nullReferenceToken">The null reference token.</param>
96    /// <returns>The token in serialized form.</returns>
97    protected abstract T Format(NullReferenceToken nullReferenceToken);
98
99    /// <summary>
100    /// Formats the specified meta info begin token.
101    /// </summary>
102    /// <param name="metaInfoBeginToken">The meta info begin token.</param>
103    /// <returns>The token in serialized form.</returns>
104    protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
105
106    /// <summary>
107    /// Formats the specified meta info end token.
108    /// </summary>
109    /// <param name="metaInfoEndToken">The meta info end token.</param>
110    /// <returns>The token in serialized form.</returns>
111    protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
112
113    /// <summary>
114    /// Formats the specified type token.
115    /// </summary>
116    /// <param name="typeToken">The type token.</param>
117    /// <returns>The token in serialized form.</returns>
118    protected abstract T Format(TypeToken typeToken);
119
120    protected abstract T Format(SerializerToken serializerToken);
121  }
122}
Note: See TracBrowser for help on using the repository browser.