/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy 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.
dnSpy 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 dnSpy. If not, see .
*/
using System;
namespace dnSpy.Contracts.Hex.Files {
///
/// A field
///
public abstract class BufferField {
///
/// Constructor
///
/// Data type
protected BufferField(BufferData data) => Data = data ?? throw new ArgumentNullException(nameof(data));
///
/// Gets the name
///
public abstract string Name { get; }
///
/// Gets the data type
///
public BufferData Data { get; }
///
/// Writes the field name
///
/// Formatter
public abstract void WriteName(HexFieldFormatter formatter);
}
///
/// A field in a structure
///
public class StructField : BufferField {
///
/// Gets the field name
///
public sealed override string Name => name;
readonly string name;
///
/// Constructor
///
/// Name
/// Data type
public StructField(string name, BufferData data)
: base(data) => this.name = name ?? throw new ArgumentNullException(nameof(name));
///
/// Writes the field name
///
/// Formatter
public sealed override void WriteName(HexFieldFormatter formatter) => formatter.WriteField(Name);
}
///
/// A field in a structure
///
public class StructField : StructField where TData : BufferData {
///
/// Gets the data type
///
public new TData Data => (TData)base.Data;
///
/// Constructor
///
/// Name
/// Data type
public StructField(string name, TData data)
: base(name, data) {
}
}
///
/// An array field
///
public class ArrayField : BufferField {
readonly uint index;
///
/// Gets the name
///
public sealed override string Name => index.ToString();
///
/// Constructor
///
/// Data type
/// Array index
public ArrayField(BufferData data, uint index)
: base(data) => this.index = index;
///
/// Writes the field name
///
/// Formatter
public sealed override void WriteName(HexFieldFormatter formatter) => formatter.WriteArrayField(index);
}
///
/// An array field
///
public class ArrayField : ArrayField where TData : BufferData {
///
/// Gets the data type
///
public new TData Data => (TData)base.Data;
///
/// Constructor
///
/// Data type
/// Array index
public ArrayField(TData data, uint index)
: base(data, index) {
}
}
}