Example #1
0
        // Enums are tricky. We can probably use expression trees to build these delegates automatically,
        // but it's easy to generate the code for it.

        /// <summary>
        /// Retrieves a codec suitable for an enum field with the given tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="toInt32">A conversion function from <see cref="Int32"/> to the enum type.</param>
        /// <param name="fromInt32">A conversion function from the enum type to <see cref="Int32"/>.</param>
        /// <returns>A codec for the given tag.</returns>
        public static FieldCodec <T> ForEnum <T>(uint tag, Func <T, int> toInt32, Func <int, T> fromInt32)
        {
            return(new FieldCodec <T>(input => fromInt32(
                                          input.ReadEnum()),
                                      (output, value) => output.WriteEnum(toInt32(value)),
                                      value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag));
        }
Example #2
0
 public static FieldCodec <T> ForEnum <T>(uint tag, Func <T, int> toInt32, Func <int, T> fromInt32)
 {
     return(new FieldCodec <T>((CodedInputStream input) => fromInt32(input.ReadEnum()), delegate(CodedOutputStream output, T value)
     {
         output.WriteEnum(toInt32(value));
     }, (T value) => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag));
 }
Example #3
0
        // Enums are tricky. We can probably use expression trees to build these delegates automatically,
        // but it's easy to generate the code for it.

        /// <summary>
        /// Retrieves a codec suitable for an enum field with the given tag.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="toInt32">A conversion function from <see cref="Int32"/> to the enum type.</param>
        /// <param name="fromInt32">A conversion function from the enum type to <see cref="Int32"/>.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>A codec for the given tag.</returns>
        public static FieldCodec <T> ForEnum <T>(uint tag, Func <T, int> toInt32, Func <int, T> fromInt32, T defaultValue)
        {
            return(new FieldCodec <T>((ref ParseContext ctx) => fromInt32(
                                          ctx.ReadEnum()),
                                      (ref WriteContext output, T value) => output.WriteEnum(toInt32(value)),
                                      value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag, defaultValue));
        }
        public void TestNegativeEnumNoTag()
        {
            Assert.AreEqual(10, CodedOutputStream.ComputeInt32Size(-2));
            Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue));

            byte[] bytes = new byte[10];
            CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
            output.WriteEnum((int) SampleEnum.NegativeValue);

            Assert.AreEqual(0, output.SpaceLeft);
            Assert.AreEqual("FE-FF-FF-FF-FF-FF-FF-FF-FF-01", BitConverter.ToString(bytes));
        }