#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Persistence.Core.Tokens;
using HeuristicLab.Persistence.Interfaces;
namespace HeuristicLab.Persistence.Core {
///
/// Base class for serialization generators. Provides a common entry point
/// Format and dispatches to different abstract methods for
/// every token type.
///
/// The type of the serialization format.
public abstract class GeneratorBase {
///
/// Processes a serialization token and formats the specified token.
///
/// The token.
/// An object suitable for serialziation
public T Format(ISerializationToken token) {
Type type = token.GetType();
if (type == typeof(BeginToken))
return Format((BeginToken)token);
if (type == typeof(EndToken))
return Format((EndToken)token);
if (type == typeof(PrimitiveToken))
return Format((PrimitiveToken)token);
if (type == typeof(ReferenceToken))
return Format((ReferenceToken)token);
if (type == typeof(NullReferenceToken))
return Format((NullReferenceToken)token);
if (type == typeof(MetaInfoBeginToken))
return Format((MetaInfoBeginToken)token);
if (type == typeof(MetaInfoEndToken))
return Format((MetaInfoEndToken)token);
if (type == typeof(TypeToken))
return Format((TypeToken)token);
throw new ApplicationException("Invalid token of type " + type.FullName);
}
///
/// Formats the specified begin token.
///
/// The begin token.
/// The token in serialized form.
protected abstract T Format(BeginToken beginToken);
///
/// Formats the specified end token.
///
/// The end token.
/// The token in serialized form.
protected abstract T Format(EndToken endToken);
///
/// Formats the specified primitive token.
///
/// The primitive token.
/// The token in serialized form.
protected abstract T Format(PrimitiveToken primitiveToken);
///
/// Formats the specified reference token.
///
/// The reference token.
/// The token in serialized form.
protected abstract T Format(ReferenceToken referenceToken);
///
/// Formats the specified null reference token.
///
/// The null reference token.
/// The token in serialized form.
protected abstract T Format(NullReferenceToken nullReferenceToken);
///
/// Formats the specified meta info begin token.
///
/// The meta info begin token.
/// The token in serialized form.
protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken);
///
/// Formats the specified meta info end token.
///
/// The meta info end token.
/// The token in serialized form.
protected abstract T Format(MetaInfoEndToken metaInfoEndToken);
///
/// Formats the specified type token.
///
/// The type token.
/// The token in serialized form.
protected abstract T Format(TypeToken typeToken);
}
}