Exemplo n.º 1
0
        /// <summary>
        /// Creates a codec for a wrapper type of a struct - which must be Int32, Int64, UInt32, UInt64,
        /// Bool, Single or Double.
        /// </summary>
        public static FieldCodec <T?> ForStructWrapper <T>(uint tag) where T : struct
        {
            var nestedCodec = WrapperCodecs.GetCodec <T>();

            return(new FieldCodec <T?>(
                       input => WrapperCodecs.Read <T>(input, nestedCodec),
                       (output, value) => WrapperCodecs.Write <T>(output, value.Value, nestedCodec),
                       (CodedInputStream i, ref T? v) => v = WrapperCodecs.Read <T>(i, nestedCodec),
                       (ref T? v, T? v2) => { if (v2.HasValue)
                                              {
                                                  v = v2;
                                              }
                                              return v.HasValue; },
                       value => value == null ? 0 : WrapperCodecs.CalculateSize <T>(value.Value, nestedCodec),
                       tag, 0,
                       null)); // Default value for the wrapper
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a codec for a wrapper type of a struct - which must be Int32, Int64, UInt32, UInt64,
        /// Bool, Single or Double.
        /// </summary>
        public static FieldCodec <T?> ForStructWrapper <T>(uint tag) where T : struct
        {
            var nestedCodec = WrapperCodecs.GetCodec <T>();

            return(new FieldCodec <T?>(
                       WrapperCodecs.GetReader <T>(),
                       (ref WriteContext output, T? value) => WrapperCodecs.Write <T>(ref output, value.Value, nestedCodec),
                       (ref ParseContext ctx, ref T? v) => v = WrapperCodecs.Read <T>(ref ctx, nestedCodec),
                       (ref T? v, T? v2) => { if (v2.HasValue)
                                              {
                                                  v = v2;
                                              }
                                              return v.HasValue; },
                       value => value == null ? 0 : WrapperCodecs.CalculateSize <T>(value.Value, nestedCodec),
                       tag, 0,
                       null)); // Default value for the wrapper
        }