// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// DataflowMessageHeader.cs
//
//
// A container of data attributes passed between dataflow blocks.
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Threading.Tasks.Dataflow.Internal;
namespace System.Threading.Tasks.Dataflow
{
/// Provides a container of data attributes for passing between dataflow blocks.
[DebuggerDisplay("Id = {Id}")]
public struct DataflowMessageHeader : IEquatable
{
/// The message ID. Needs to be unique within the source.
private readonly long _id;
/// Initializes the with the specified attributes.
/// The ID of the message. Must be unique within the originating source block. Need not be globally unique.
public DataflowMessageHeader(Int64 id)
{
if (id == default(long)) throw new ArgumentException(SR.Argument_InvalidMessageId, "id");
Contract.EndContractBlock();
_id = id;
}
/// Gets the validity of the message.
/// True if the ID of the message is different from 0. False if the ID of the message is 0
public Boolean IsValid { get { return _id != default(long); } }
/// Gets the ID of the message within the source.
/// The ID contained in the instance.
public Int64 Id { get { return _id; } }
// These overrides are required by the FX API Guidelines.
// NOTE: When these overrides are present, the compiler doesn't complain about statements
// like 'if (struct == null) ...' which will result in incorrect behavior at runtime.
// The product code should not use them. Instead, it should compare the Id properties.
// To verify that, every once in a while, comment out this region and build the product.
#region Comparison Operators
/// Checks two instances for equality by ID without boxing.
/// Another instance.
/// True if the instances are equal. False otherwise.
public bool Equals(DataflowMessageHeader other)
{
return this == other;
}
/// Checks boxed instances for equality by ID.
/// A boxed instance.
/// True if the instances are equal. False otherwise.
public override bool Equals(object obj)
{
return obj is DataflowMessageHeader && this == (DataflowMessageHeader)obj;
}
/// Generates a hash code for the instance.
/// Hash code.
public override int GetHashCode()
{
return (int)Id;
}
/// Checks two instances for equality by ID.
/// A instance.
/// A instance.
/// True if the instances are equal. False otherwise.
public static bool operator ==(DataflowMessageHeader left, DataflowMessageHeader right)
{
return left.Id == right.Id;
}
/// Checks two instances for non-equality by ID.
/// A instance.
/// A instance.
/// True if the instances are not equal. False otherwise.
public static bool operator !=(DataflowMessageHeader left, DataflowMessageHeader right)
{
return left.Id != right.Id;
}
#endregion
}
}