Skip to content

Commit

Permalink
Use ICloneable in generated code (#2044)
Browse files Browse the repository at this point in the history
- ICloneable was removed from generated code for the first port to .NET Standard 1.x, instead MemberwiseClone was used
- Reenable ICloneable for generated code
- Add ICloneable to built in types
- Fix code where MemberwiseClone is called instead of Clone
  • Loading branch information
mregen committed Apr 27, 2023
1 parent 828cc20 commit 24845eb
Show file tree
Hide file tree
Showing 34 changed files with 475 additions and 423 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<DefineConstants>$(DefineConstants);NET_STANDARD</DefineConstants>
<DefineConstants>$(DefineConstants)</DefineConstants>
<AssemblyName>Quickstarts.Servers</AssemblyName>
<TargetFrameworks>$(LibTargetFrameworks)</TargetFrameworks>
<PackageId>OPCFoundation.NetStandard.Opc.Ua.Quickstarts.Servers</PackageId>
Expand Down
34 changes: 21 additions & 13 deletions Libraries/Opc.Ua.Client.ComplexTypes/Types/BaseComplexType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Opc.Ua.Client.ComplexTypes
/// The base class for all complex types.
/// </summary>
public class BaseComplexType :
IEncodeable, IFormattable,
IEncodeable, IFormattable, ICloneable,
IComplexTypeProperties,
IStructureTypeInfo
{
Expand Down Expand Up @@ -85,26 +85,20 @@ private void Initialize(StreamingContext context)
}
#endregion Constructors

#region Public Properties
/// <inheritdoc/>
public ExpandedNodeId TypeId { get; set; }

/// <inheritdoc/>
public ExpandedNodeId BinaryEncodingId { get; set; }

#region ICloneable
/// <inheritdoc/>
public ExpandedNodeId XmlEncodingId { get; set; }

/// <inheritdoc/>
public virtual StructureType StructureType => StructureType.Structure;
public virtual object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Makes a deep copy of the object.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public new virtual object MemberwiseClone()
public new object MemberwiseClone()
{
Type thisType = this.GetType();
BaseComplexType clone = Activator.CreateInstance(thisType) as BaseComplexType;
Expand All @@ -121,6 +115,20 @@ public new virtual object MemberwiseClone()

return clone;
}
#endregion

#region Public Properties
/// <inheritdoc/>
public ExpandedNodeId TypeId { get; set; }

/// <inheritdoc/>
public ExpandedNodeId BinaryEncodingId { get; set; }

/// <inheritdoc/>
public ExpandedNodeId XmlEncodingId { get; set; }

/// <inheritdoc/>
public virtual StructureType StructureType => StructureType.Structure;

/// <inheritdoc/>
public virtual void Encode(IEncoder encoder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,35 @@ public OptionalFieldsComplexType(ExpandedNodeId typeId) : base(typeId)
}
#endregion Constructors

#region Public Properties

#region ICloneable
/// <inheritdoc/>
public override StructureType StructureType => StructureType.StructureWithOptionalFields;

/// <summary>
/// The encoding mask for the optional fields.
/// </summary>
public UInt32 EncodingMask => m_encodingMask;
public override object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Makes a deep copy of the object.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public override object MemberwiseClone()
public new object MemberwiseClone()
{
OptionalFieldsComplexType clone = (OptionalFieldsComplexType)base.MemberwiseClone();
clone.m_encodingMask = m_encodingMask;
return clone;
}
#endregion

#region Public Properties
/// <inheritdoc/>
public override StructureType StructureType => StructureType.StructureWithOptionalFields;

/// <summary>
/// The encoding mask for the optional fields.
/// </summary>
public UInt32 EncodingMask => m_encodingMask;

/// <inheritdoc/>
public override void Encode(IEncoder encoder)
Expand Down
28 changes: 18 additions & 10 deletions Libraries/Opc.Ua.Client.ComplexTypes/Types/UnionComplexType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,37 @@ public UnionComplexType(ExpandedNodeId typeId) : base(typeId)
}
#endregion Constructors

#region Public Properties
/// <summary>
/// The union selector determines which property is valid.
/// A value of 0 means all properties are invalid, x=1..n means the
/// xth property is valid.
/// </summary>
public UInt32 SwitchField => m_switchField;

#region ICloneable
/// <inheritdoc/>
public override StructureType StructureType => StructureType.Union;
public override object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Makes a deep copy of the object.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public override object MemberwiseClone()
public new object MemberwiseClone()
{
UnionComplexType clone = (UnionComplexType)base.MemberwiseClone();
clone.m_switchField = m_switchField;
return clone;
}
#endregion

#region Public Properties
/// <summary>
/// The union selector determines which property is valid.
/// A value of 0 means all properties are invalid, x=1..n means the
/// xth property is valid.
/// </summary>
public UInt32 SwitchField => m_switchField;

/// <inheritdoc/>
public override StructureType StructureType => StructureType.Union;

/// <inheritdoc/>
public override void Encode(IEncoder encoder)
Expand Down
8 changes: 7 additions & 1 deletion Libraries/Opc.Ua.Client/MonitoredItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Opc.Ua.Client
[KnownType(typeof(DataChangeFilter))]
[KnownType(typeof(EventFilter))]
[KnownType(typeof(AggregateFilter))]
public class MonitoredItem
public class MonitoredItem : ICloneable
{
#region Constructors
/// <summary>
Expand Down Expand Up @@ -662,6 +662,12 @@ public void SaveValueInCache(IEncodeable newValue)
#endregion

#region ICloneable Members
/// <inheritdoc/>
public virtual object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Creates a deep copy of the object.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ public void FetchNamespaceTables()
ResponseHeader responseHeader = this.Read(
null,
0,
TimestampsToReturn.Both,
TimestampsToReturn.Neither,
nodesToRead,
out DataValueCollection values,
out DiagnosticInfoCollection diagnosticInfos);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public Subscription(Subscription template, bool copyEventHandlers)
m_maxMessageCount = template.m_maxMessageCount;
m_sequentialPublishing = template.m_sequentialPublishing;
m_republishAfterTransfer = template.m_republishAfterTransfer;
m_defaultItem = (MonitoredItem)template.m_defaultItem.MemberwiseClone();
m_defaultItem = (MonitoredItem)template.m_defaultItem.Clone();
m_handle = template.m_handle;
m_disableMonitoredItemCache = template.m_disableMonitoredItemCache;
m_transferId = template.m_transferId;
Expand Down
16 changes: 12 additions & 4 deletions Libraries/Opc.Ua.PubSub/PublishedData/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;

namespace Opc.Ua.PubSub.PublishedData
{
/// <summary>
/// Entity that holds DataSet structure that is published/received bu the PubSub
/// </summary>
public class DataSet
public class DataSet : ICloneable
{
#region Constructor
/// <summary>
Expand Down Expand Up @@ -77,7 +79,13 @@ public DataSet(string name = null)
public Field[] Fields { get; set; }
#endregion

#region MemberwiseClone method
#region ICloneable method
/// <inheritdoc/>
public virtual object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Create a deep copy of current DataSet
/// </summary>
Expand All @@ -88,7 +96,7 @@ public new object MemberwiseClone()
{
if (copy != null)
{
copy.DataSetMetaData = DataSetMetaData.MemberwiseClone() as DataSetMetaDataType;
copy.DataSetMetaData = DataSetMetaData.Clone() as DataSetMetaDataType;
}
}

Expand All @@ -99,7 +107,7 @@ public new object MemberwiseClone()
copy.Fields = new Field[Fields.Length];
for (int i = 0; i < Fields.Length; i++)
{
copy.Fields[i] = Fields[i].MemberwiseClone() as Field;
copy.Fields[i] = Fields[i].Clone() as Field;
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions Libraries/Opc.Ua.PubSub/PublishedData/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;

namespace Opc.Ua.PubSub.PublishedData
{
/// <summary>
/// Base class for a DataSet field
/// </summary>
public class Field
public class Field : ICloneable
{
/// <summary>
/// Get/Set Value
Expand All @@ -54,7 +56,13 @@ public class Field
/// </summary>
public FieldMetaData FieldMetaData { get; internal set; }

#region MemberwiseClone method
#region ICloneable method
/// <inheritdoc/>
public virtual object Clone()
{
return this.MemberwiseClone();
}

/// <summary>
/// Create a deep copy of current DataSet
/// </summary>
Expand All @@ -65,15 +73,15 @@ public new object MemberwiseClone()
{
if (copy != null)
{
copy.Value = Value.MemberwiseClone() as DataValue;
copy.Value = Value.Clone() as DataValue;
}
}

if (FieldMetaData != null)
{
if (copy != null)
{
copy.FieldMetaData = FieldMetaData.MemberwiseClone() as FieldMetaData;
copy.FieldMetaData = FieldMetaData.Clone() as FieldMetaData;
}
}
return copy;
Expand Down
15 changes: 7 additions & 8 deletions Libraries/Opc.Ua.PubSub/WriterGroupPublishState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public bool HasMetaDataChanged(DataSetWriterDataType writer, DataSetMetaDataType
if (version == null)
{
// keep a copy of ConfigurationVersion
state.ConfigurationVersion = metadata.ConfigurationVersion.MemberwiseClone() as ConfigurationVersionDataType;
state.ConfigurationVersion = metadata.ConfigurationVersion.Clone() as ConfigurationVersionDataType;
state.LastMetaDataUpdate = DateTime.UtcNow;
return true;
}
Expand All @@ -113,15 +113,15 @@ public bool HasMetaDataChanged(DataSetWriterDataType writer, DataSetMetaDataType
version.MinorVersion != metadata.ConfigurationVersion.MinorVersion)
{
// keep a copy of ConfigurationVersion
state.ConfigurationVersion = metadata.ConfigurationVersion.MemberwiseClone() as ConfigurationVersionDataType;
state.ConfigurationVersion = metadata.ConfigurationVersion.Clone() as ConfigurationVersionDataType;
state.LastMetaDataUpdate = DateTime.UtcNow;
return true;
}
}

return false;
}

/// <summary>
/// Checks if the DataSet has changed and null
/// </summary>
Expand All @@ -135,7 +135,7 @@ public DataSet ExcludeUnchangedFields(DataSetWriterDataType writer, DataSet data

if (lastDataSet == null)
{
state.LastDataSet = dataset.MemberwiseClone() as DataSet;
state.LastDataSet = Utils.Clone(dataset) as DataSet;
return dataset;
}

Expand Down Expand Up @@ -190,11 +190,11 @@ public void OnMessagePublished(DataSetWriterDataType writer, DataSet dataset)
{

state.ConfigurationVersion =
dataset.DataSetMetaData.ConfigurationVersion.MemberwiseClone() as ConfigurationVersionDataType;
dataset.DataSetMetaData.ConfigurationVersion.Clone() as ConfigurationVersionDataType;

if (state.LastDataSet == null)
{
state.LastDataSet = dataset.MemberwiseClone() as DataSet;
state.LastDataSet = Utils.Clone(dataset) as DataSet;
return;
}

Expand All @@ -204,14 +204,13 @@ public void OnMessagePublished(DataSetWriterDataType writer, DataSet dataset)

if (field != null)
{
state.LastDataSet.Fields[ii] = field.MemberwiseClone() as Field;
state.LastDataSet.Fields[ii] = Utils.Clone(field) as Field;
}
}

}
}
}

#endregion

#region Private Methods
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Server/Server/ServerInternalData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ private void CreateServerObject()
}

/// <summary>
/// Updates the Server.Auditing flag.
/// Reads the Server.Auditing flag.
/// </summary>
private ServiceResult OnReadAuditing(
ISystemContext context,
Expand Down
Loading

0 comments on commit 24845eb

Please sign in to comment.