Skip to content

Commit

Permalink
Moved IPlcMapper into TagOfT
Browse files Browse the repository at this point in the history
IPlcMapper and ITag are collaborators with TagOfT.
Since this is an example, just move them into the same file.

Move the implementations of mappers into a separate file (ExampleMappers.cs)
  • Loading branch information
timyhac committed Jul 17, 2024
1 parent bd31929 commit 09acecc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,6 @@
namespace CSharpDotNetCore.PlcMapper
{

public interface IPlcMapper<T>
{
/// <summary>
/// You can define different marshalling behaviour for different types
/// The PlcType is injected during PlcMapper instantiation, and
/// will be available to you in your marshalling logic
/// </summary>
PlcType PlcType { get; set; }


/// <summary>
/// Provide an integer value for ElementSize if you
/// want to pass this into the tag constructor
/// </summary>
int? ElementSize { get; }

/// <summary>
/// The dimensions of the array. Null if not an array.
/// </summary>
int[] ArrayDimensions { get; set; }

/// <summary>
/// This is used to convert the number of array elements
/// into the raw element count, which is used by the library.
/// Most of the time, this will be the dimensions multiplied, but occasionally
/// it is not (e.g. BOOL arrays).
/// </summary>
int? GetElementCount();

/// <summary>
/// This is the method that reads/unpacks the underlying value of the tag
/// and returns it as a C# type
/// </summary>
/// <param name="tag">Tag to be Decoded</param>
/// <returns>C# value of tag</returns>
T Decode(Tag tag);

/// <summary>
/// This is the method that transforms the C# type into the underlying value of the tag
/// </summary>
/// <param name="tag">Tag to be encoded to</param>
/// <param name="value">C# value to be transformed</param>
void Encode(Tag tag, T value);
}

public abstract class PlcMapperBase<T> : IPlcMapper<T>, IPlcMapper<T[]>, IPlcMapper<T[,]>, IPlcMapper<T[,,]>
{
public PlcType PlcType { get; set; }
Expand Down Expand Up @@ -377,4 +332,58 @@ public static T[] To1DArray<T>(this T[,,] input)
}

}

public class TagBool : Tag<BoolPlcMapper, bool> { }
public class TagBool1D : Tag<BoolPlcMapper, bool[]> { }
public class TagBool2D : Tag<BoolPlcMapper, bool[,]> { }
public class TagBool3D : Tag<BoolPlcMapper, bool[,,]> { }


public class TagDint : Tag<DintPlcMapper, int> { }
public class TagDint1D : Tag<DintPlcMapper, int[]> { }
public class TagDint2D : Tag<DintPlcMapper, int[,]> { }
public class TagDint3D : Tag<DintPlcMapper, int[,,]> { }


public class TagInt : Tag<IntPlcMapper, short> { }
public class TagInt1D : Tag<IntPlcMapper, short[]> { }
public class TagInt2D : Tag<IntPlcMapper, short[,]> { }
public class TagInt3D : Tag<IntPlcMapper, short[,,]> { }


public class TagLint : Tag<LintPlcMapper, long> { }
public class TagLint1D : Tag<LintPlcMapper, long[]> { }
public class TagLint2D : Tag<LintPlcMapper, long[,]> { }
public class TagLint3D : Tag<LintPlcMapper, long[,,]> { }


public class TagLreal : Tag<LrealPlcMapper, double> { }
public class TagLreal1D : Tag<LrealPlcMapper, double[]> { }
public class TagLreal2D : Tag<LrealPlcMapper, double[,]> { }
public class TagLreal3D : Tag<LrealPlcMapper, double[,,]> { }


public class TagReal : Tag<RealPlcMapper, float> { }
public class TagReal1D : Tag<RealPlcMapper, float[]> { }
public class TagReal2D : Tag<RealPlcMapper, float[,]> { }
public class TagReal3D : Tag<RealPlcMapper, float[,,]> { }


public class TagSint : Tag<SintPlcMapper, sbyte> { }
public class TagSint1D : Tag<SintPlcMapper, sbyte[]> { }
public class TagSint2D : Tag<SintPlcMapper, sbyte[,]> { }
public class TagSint3D : Tag<SintPlcMapper, sbyte[,,]> { }


public class TagString : Tag<StringPlcMapper, string> { }
public class TagString1D : Tag<StringPlcMapper, string[]> { }
public class TagString2D : Tag<StringPlcMapper, string[,]> { }
public class TagString3D : Tag<StringPlcMapper, string[,,]> { }


public class TagTagInfo : Tag<TagInfoPlcMapper, TagInfo[]> { }

public class TagTimer : Tag<TimerPlcMapper, AbTimer> { }

public class TagUdtInfo : Tag<UdtInfoPlcMapper, UdtInfo> { }
}
100 changes: 46 additions & 54 deletions examples/CSharp DotNetCore/PlcMapper/TagOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,51 @@ void EncodeAll()

}

public interface IPlcMapper<T>
{
/// <summary>
/// You can define different marshalling behaviour for different types
/// The PlcType is injected during PlcMapper instantiation, and
/// will be available to you in your marshalling logic
/// </summary>
PlcType PlcType { get; set; }


/// <summary>
/// Provide an integer value for ElementSize if you
/// want to pass this into the tag constructor
/// </summary>
int? ElementSize { get; }

/// <summary>
/// The dimensions of the array. Null if not an array.
/// </summary>
int[] ArrayDimensions { get; set; }

/// <summary>
/// This is used to convert the number of array elements
/// into the raw element count, which is used by the library.
/// Most of the time, this will be the dimensions multiplied, but occasionally
/// it is not (e.g. BOOL arrays).
/// </summary>
int? GetElementCount();

/// <summary>
/// This is the method that reads/unpacks the underlying value of the tag
/// and returns it as a C# type
/// </summary>
/// <param name="tag">Tag to be Decoded</param>
/// <returns>C# value of tag</returns>
T Decode(Tag tag);

/// <summary>
/// This is the method that transforms the C# type into the underlying value of the tag
/// </summary>
/// <param name="tag">Tag to be encoded to</param>
/// <param name="value">C# value to be transformed</param>
void Encode(Tag tag, T value);
}

/// <summary>
/// An interface to represent any generic tag without
/// exposing its value
Expand Down Expand Up @@ -295,58 +340,5 @@ public interface ITag : IDisposable

object Value { get; set; }
}

public class TagBool : Tag<BoolPlcMapper, bool> { }
public class TagBool1D : Tag<BoolPlcMapper, bool[]> { }
public class TagBool2D : Tag<BoolPlcMapper, bool[,]> { }
public class TagBool3D : Tag<BoolPlcMapper, bool[,,]> { }


public class TagDint : Tag<DintPlcMapper, int> { }
public class TagDint1D : Tag<DintPlcMapper, int[]> { }
public class TagDint2D : Tag<DintPlcMapper, int[,]> { }
public class TagDint3D : Tag<DintPlcMapper, int[,,]> { }


public class TagInt : Tag<IntPlcMapper, short> { }
public class TagInt1D : Tag<IntPlcMapper, short[]> { }
public class TagInt2D : Tag<IntPlcMapper, short[,]> { }
public class TagInt3D : Tag<IntPlcMapper, short[,,]> { }


public class TagLint : Tag<LintPlcMapper, long> { }
public class TagLint1D : Tag<LintPlcMapper, long[]> { }
public class TagLint2D : Tag<LintPlcMapper, long[,]> { }
public class TagLint3D : Tag<LintPlcMapper, long[,,]> { }


public class TagLreal : Tag<LrealPlcMapper, double> { }
public class TagLreal1D : Tag<LrealPlcMapper, double[]> { }
public class TagLreal2D : Tag<LrealPlcMapper, double[,]> { }
public class TagLreal3D : Tag<LrealPlcMapper, double[,,]> { }


public class TagReal : Tag<RealPlcMapper, float> { }
public class TagReal1D : Tag<RealPlcMapper, float[]> { }
public class TagReal2D : Tag<RealPlcMapper, float[,]> { }
public class TagReal3D : Tag<RealPlcMapper, float[,,]> { }


public class TagSint : Tag<SintPlcMapper, sbyte> { }
public class TagSint1D : Tag<SintPlcMapper, sbyte[]> { }
public class TagSint2D : Tag<SintPlcMapper, sbyte[,]> { }
public class TagSint3D : Tag<SintPlcMapper, sbyte[,,]> { }


public class TagString : Tag<StringPlcMapper, string> { }
public class TagString1D : Tag<StringPlcMapper, string[]> { }
public class TagString2D : Tag<StringPlcMapper, string[,]> { }
public class TagString3D : Tag<StringPlcMapper, string[,,]> { }


public class TagTagInfo : Tag<TagInfoPlcMapper, TagInfo[]> { }

public class TagTimer : Tag<TimerPlcMapper, AbTimer> { }

public class TagUdtInfo : Tag<UdtInfoPlcMapper, UdtInfo> { }

}

0 comments on commit 09acecc

Please sign in to comment.