1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 // DataflowMessageHeader.cs
9 // A container of data attributes passed between dataflow blocks.
11 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13 using System.Diagnostics;
14 using System.Diagnostics.Contracts;
15 using System.Threading.Tasks.Dataflow.Internal;
17 namespace System.Threading.Tasks.Dataflow
19 /// <summary>Provides a container of data attributes for passing between dataflow blocks.</summary>
20 [DebuggerDisplay("Id = {Id}")]
21 public struct DataflowMessageHeader : IEquatable<DataflowMessageHeader>
23 /// <summary>The message ID. Needs to be unique within the source.</summary>
24 private readonly long _id;
26 /// <summary>Initializes the <see cref="DataflowMessageHeader"/> with the specified attributes.</summary>
27 /// <param name="id">The ID of the message. Must be unique within the originating source block. Need not be globally unique.</param>
28 public DataflowMessageHeader(Int64 id)
30 if (id == default(long)) throw new ArgumentException(SR.Argument_InvalidMessageId, "id");
31 Contract.EndContractBlock();
36 /// <summary>Gets the validity of the message.</summary>
37 /// <returns>True if the ID of the message is different from 0. False if the ID of the message is 0</returns>
38 public Boolean IsValid { get { return _id != default(long); } }
40 /// <summary>Gets the ID of the message within the source.</summary>
41 /// <returns>The ID contained in the <see cref="DataflowMessageHeader"/> instance.</returns>
42 public Int64 Id { get { return _id; } }
44 // These overrides are required by the FX API Guidelines.
45 // NOTE: When these overrides are present, the compiler doesn't complain about statements
46 // like 'if (struct == null) ...' which will result in incorrect behavior at runtime.
47 // The product code should not use them. Instead, it should compare the Id properties.
48 // To verify that, every once in a while, comment out this region and build the product.
49 #region Comparison Operators
50 /// <summary>Checks two <see cref="DataflowMessageHeader"/> instances for equality by ID without boxing.</summary>
51 /// <param name="other">Another <see cref="DataflowMessageHeader"/> instance.</param>
52 /// <returns>True if the instances are equal. False otherwise.</returns>
53 public bool Equals(DataflowMessageHeader other)
58 /// <summary>Checks boxed <see cref="DataflowMessageHeader"/> instances for equality by ID.</summary>
59 /// <param name="obj">A boxed <see cref="DataflowMessageHeader"/> instance.</param>
60 /// <returns>True if the instances are equal. False otherwise.</returns>
61 public override bool Equals(object obj)
63 return obj is DataflowMessageHeader && this == (DataflowMessageHeader)obj;
66 /// <summary>Generates a hash code for the <see cref="DataflowMessageHeader"/> instance.</summary>
67 /// <returns>Hash code.</returns>
68 public override int GetHashCode()
73 /// <summary>Checks two <see cref="DataflowMessageHeader"/> instances for equality by ID.</summary>
74 /// <param name="left">A <see cref="DataflowMessageHeader"/> instance.</param>
75 /// <param name="right">A <see cref="DataflowMessageHeader"/> instance.</param>
76 /// <returns>True if the instances are equal. False otherwise.</returns>
77 public static bool operator ==(DataflowMessageHeader left, DataflowMessageHeader right)
79 return left.Id == right.Id;
82 /// <summary>Checks two <see cref="DataflowMessageHeader"/> instances for non-equality by ID.</summary>
83 /// <param name="left">A <see cref="DataflowMessageHeader"/> instance.</param>
84 /// <param name="right">A <see cref="DataflowMessageHeader"/> instance.</param>
85 /// <returns>True if the instances are not equal. False otherwise.</returns>
86 public static bool operator !=(DataflowMessageHeader left, DataflowMessageHeader right)
88 return left.Id != right.Id;