Merge pull request #2236 from akoeplinger/add-dataflow
[mono.git] / mcs / class / System.Threading.Tasks.Dataflow / CoreFxSources / Base / DataflowMessageHeader.cs
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
5 //
6 // DataflowMessageHeader.cs
7 //
8 //
9 // A container of data attributes passed between dataflow blocks.
10 //
11 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
12
13 using System.Diagnostics;
14 using System.Diagnostics.Contracts;
15 using System.Threading.Tasks.Dataflow.Internal;
16
17 namespace System.Threading.Tasks.Dataflow
18 {
19     /// <summary>Provides a container of data attributes for passing between dataflow blocks.</summary>
20     [DebuggerDisplay("Id = {Id}")]
21     public struct DataflowMessageHeader : IEquatable<DataflowMessageHeader>
22     {
23         /// <summary>The message ID. Needs to be unique within the source.</summary>
24         private readonly long _id;
25
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)
29         {
30             if (id == default(long)) throw new ArgumentException(SR.Argument_InvalidMessageId, "id");
31             Contract.EndContractBlock();
32
33             _id = id;
34         }
35
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); } }
39
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; } }
43
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)
54         {
55             return this == other;
56         }
57
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)
62         {
63             return obj is DataflowMessageHeader && this == (DataflowMessageHeader)obj;
64         }
65
66         /// <summary>Generates a hash code for the <see cref="DataflowMessageHeader"/> instance.</summary>
67         /// <returns>Hash code.</returns>
68         public override int GetHashCode()
69         {
70             return (int)Id;
71         }
72
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)
78         {
79             return left.Id == right.Id;
80         }
81
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)
87         {
88             return left.Id != right.Id;
89         }
90         #endregion
91     }
92 }