Skip to content

Commit 515beff

Browse files
committed
Revert removal, and mark obsolete/deprecated.
Although issue #406 says to **remove** these classes, start the process by deprecating them instead.
1 parent 658baa4 commit 515beff

17 files changed

+1224
-0
lines changed
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using libplctag.DataTypes.Extensions;
9+
using System;
10+
using System.Linq;
11+
12+
namespace libplctag.DataTypes
13+
{
14+
15+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
16+
public class BoolPlcMapper : IPlcMapper<bool>, IPlcMapper<bool[]>, IPlcMapper<bool[,]>, IPlcMapper<bool[,,]>
17+
{
18+
public int? ElementSize => 1;
19+
20+
public PlcType PlcType { get; set; }
21+
public int[] ArrayDimensions { get; set; }
22+
23+
public int? GetElementCount()
24+
{
25+
if (ArrayDimensions == null)
26+
return null;
27+
28+
//TODO: Test -> I'm not confident that the overall bool count is packed as a 1D array and not packed by dimension.
29+
//Multiply dimensions for total elements
30+
var totalElements = ArrayDimensions.Aggregate(1, (x, y) => x * y);
31+
return (int)Math.Ceiling((double)totalElements / 32.0);
32+
}
33+
34+
public int? SetArrayLength(int? elementCount) => (int)Math.Ceiling((double)elementCount.Value / 32.0);
35+
36+
virtual protected bool[] DecodeArray(Tag tag)
37+
{
38+
if (ElementSize is null)
39+
throw new ArgumentNullException($"{nameof(ElementSize)} cannot be null for array decoding");
40+
41+
var buffer = new bool[tag.ElementCount.Value * 32];
42+
for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++)
43+
{
44+
buffer[ii] = tag.GetBit(ii);
45+
}
46+
return buffer;
47+
}
48+
49+
virtual protected void EncodeArray(Tag tag, bool[] values)
50+
{
51+
for (int ii = 0; ii < tag.ElementCount.Value * 32; ii++)
52+
{
53+
tag.SetBit(ii, values[ii]);
54+
}
55+
}
56+
57+
bool IPlcMapper<bool>.Decode(Tag tag) => tag.GetUInt8(0) != 0;
58+
59+
void IPlcMapper<bool>.Encode(Tag tag, bool value) => tag.SetUInt8(0, value == true ? (byte)255 : (byte)0);
60+
61+
bool[] IPlcMapper<bool[]>.Decode(Tag tag) => DecodeArray(tag);
62+
63+
void IPlcMapper<bool[]>.Encode(Tag tag, bool[] value) => EncodeArray(tag, value);
64+
65+
bool[,] IPlcMapper<bool[,]>.Decode(Tag tag) => DecodeArray(tag).To2DArray(ArrayDimensions[0], ArrayDimensions[1]);
66+
67+
void IPlcMapper<bool[,]>.Encode(Tag tag, bool[,] value) => EncodeArray(tag, value.To1DArray());
68+
69+
bool[,,] IPlcMapper<bool[,,]>.Decode(Tag tag) => DecodeArray(tag).To3DArray(ArrayDimensions[0], ArrayDimensions[1], ArrayDimensions[2]);
70+
71+
void IPlcMapper<bool[,,]>.Encode(Tag tag, bool[,,] value) => EncodeArray(tag, value.To1DArray());
72+
73+
}
74+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
10+
namespace libplctag.DataTypes
11+
{
12+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
13+
public class DintPlcMapper : PlcMapperBase<int>
14+
{
15+
public override int? ElementSize => 4;
16+
17+
override public int Decode(Tag tag, int offset) => tag.GetInt32(offset);
18+
19+
override public void Encode(Tag tag, int offset, int value) => tag.SetInt32(offset, value);
20+
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace libplctag.DataTypes.Extensions
13+
{
14+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
15+
public static class ArrayExtensions
16+
{
17+
/// <summary>
18+
/// Extension method to flatten a 2D array to a 1D array
19+
/// </summary>
20+
/// <typeparam name="T">Array Type</typeparam>
21+
/// <param name="input">2D array to be flattened</param>
22+
/// <returns>1D array</returns>
23+
public static T[] To1DArray<T>(this T[,] input)
24+
{
25+
// Step 1: get total size of 2D array, and allocate 1D array.
26+
int size = input.Length;
27+
T[] result = new T[size];
28+
29+
// Step 2: copy 2D array elements into a 1D array.
30+
int write = 0;
31+
for (int i = 0; i <= input.GetUpperBound(0); i++)
32+
{
33+
for (int z = 0; z <= input.GetUpperBound(1); z++)
34+
{
35+
result[write++] = input[i, z];
36+
}
37+
}
38+
// Step 3: return the new array.
39+
return result;
40+
}
41+
42+
/// <summary>
43+
/// Extension method to flatten a 3D array to a 1D array
44+
/// </summary>
45+
/// <typeparam name="T">Array Type</typeparam>
46+
/// <param name="input">3D array to be flattened</param>
47+
/// <returns>1D array</returns>
48+
public static T[] To1DArray<T>(this T[,,] input)
49+
{
50+
// Step 1: get total size of 3D array, and allocate 1D array.
51+
int size = input.Length;
52+
T[] result = new T[size];
53+
54+
// Step 2: copy 3D array elements into a 1D array.
55+
int write = 0;
56+
for (int i = 0; i <= input.GetUpperBound(0); i++)
57+
{
58+
for (int j = 0; j <= input.GetUpperBound(1); j++)
59+
{
60+
for (int k = 0; k < input.GetUpperBound(2); k++)
61+
{
62+
result[write++] = input[i, j, k];
63+
}
64+
}
65+
}
66+
// Step 3: return the new array.
67+
return result;
68+
}
69+
70+
/// <summary>
71+
/// Extension method to reshape a 1D array into a 2D array
72+
/// </summary>
73+
/// <typeparam name="T">Array Type</typeparam>
74+
/// <param name="input">1D array to be reshaped</param>
75+
/// <param name="height">Desired height (first index) of 2D array</param>
76+
/// <param name="width">Desired width (second index) of 2D array</param>
77+
/// <returns>2D array</returns>
78+
public static T[,] To2DArray<T>(this T[] input, int height, int width)
79+
{
80+
T[,] output = new T[height, width];
81+
82+
for (int i = 0; i < height; i++)
83+
{
84+
for (int j = 0; j < width; j++)
85+
{
86+
output[i, j] = input[i * width + j];
87+
}
88+
}
89+
return output;
90+
}
91+
92+
/// <summary>
93+
/// Extension method to reshape a 1D array into a 3D array
94+
/// </summary>
95+
/// <typeparam name="T">Array Type</typeparam>
96+
/// <param name="input">1D array to be reshaped</param>
97+
/// <param name="height">Desired height (first index) of 3D array</param>
98+
/// <param name="width">Desired width (second index) of 3D array</param>
99+
/// <param name="length">Desired length (third index) of 3D array</param>
100+
/// <returns>#D array</returns>
101+
public static T[,,] To3DArray<T>(this T[] input, int height, int width, int length)
102+
{
103+
T[,,] output = new T[height, width, length];
104+
105+
for (int i = 0; i < height; i++)
106+
{
107+
for (int j = 0; j < width; j++)
108+
{
109+
for (int k = 0; k < length; k++)
110+
{
111+
output[i, j, k] = input[i * height * width + j * width + k];
112+
}
113+
}
114+
}
115+
return output;
116+
}
117+
118+
}
119+
}

src/libplctag/DataTypes/IPlcMapper.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
10+
namespace libplctag.DataTypes
11+
{
12+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
13+
public interface IPlcMapper<T>
14+
{
15+
/// <summary>
16+
/// You can define different marshalling behaviour for different types
17+
/// The PlcType is injected during PlcMapper instantiation, and
18+
/// will be available to you in your marshalling logic
19+
/// </summary>
20+
PlcType PlcType { get; set; }
21+
22+
23+
/// <summary>
24+
/// Provide an integer value for ElementSize if you
25+
/// want to pass this into the tag constructor
26+
/// </summary>
27+
int? ElementSize { get; }
28+
29+
/// <summary>
30+
/// The dimensions of the array. Null if not an array.
31+
/// </summary>
32+
int[] ArrayDimensions { get; set; }
33+
34+
/// <summary>
35+
/// This is used to convert the number of array elements
36+
/// into the raw element count, which is used by the library.
37+
/// Most of the time, this will be the dimensions multiplied, but occasionally
38+
/// it is not (e.g. BOOL arrays).
39+
/// </summary>
40+
int? GetElementCount();
41+
42+
/// <summary>
43+
/// This is the method that reads/unpacks the underlying value of the tag
44+
/// and returns it as a C# type
45+
/// </summary>
46+
/// <param name="tag">Tag to be Decoded</param>
47+
/// <returns>C# value of tag</returns>
48+
T Decode(Tag tag);
49+
50+
/// <summary>
51+
/// This is the method that transforms the C# type into the underlying value of the tag
52+
/// </summary>
53+
/// <param name="tag">Tag to be encoded to</param>
54+
/// <param name="value">C# value to be transformed</param>
55+
void Encode(Tag tag, T value);
56+
}
57+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
10+
namespace libplctag.DataTypes
11+
{
12+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
13+
public class IntPlcMapper : PlcMapperBase<short>
14+
{
15+
public override int? ElementSize => 2;
16+
17+
override public short Decode(Tag tag, int offset) => tag.GetInt16(offset);
18+
19+
override public void Encode(Tag tag, int offset, short value) => tag.SetInt16(offset, value);
20+
21+
}
22+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
10+
namespace libplctag.DataTypes
11+
{
12+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
13+
public class LintPlcMapper : PlcMapperBase<long>
14+
{
15+
public override int? ElementSize => 8;
16+
17+
override public long Decode(Tag tag, int offset) => tag.GetInt64(offset);
18+
19+
override public void Encode(Tag tag, int offset, long value) => tag.SetInt64(offset, value);
20+
21+
}
22+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) libplctag.NET contributors
2+
// https://github.com/libplctag/libplctag.NET
3+
//
4+
// This Source Code Form is subject to the terms of the Mozilla Public
5+
// License, v. 2.0. If a copy of the MPL was not distributed with this
6+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
8+
using System;
9+
10+
namespace libplctag.DataTypes
11+
{
12+
[Obsolete("see - https://github.com/libplctag/libplctag.NET/issues/406")]
13+
public class LrealPlcMapper : PlcMapperBase<double>
14+
{
15+
16+
override public int? ElementSize => 8;
17+
18+
override public double Decode(Tag tag, int offset) => tag.GetFloat64(offset);
19+
20+
override public void Encode(Tag tag, int offset, double value)=> tag.SetFloat64(offset, value);
21+
}
22+
}

0 commit comments

Comments
 (0)