internal ListOfPrimitivesDeserializer(ref DeserializerState state, ListKind kind) :
            base(ref state)
        {
            _kind = kind;

            var binCoder = PrimitiveCoder.Get <T>();
        }
Beispiel #2
0
        /// <summary>
        /// Constructs a state representing a message root object.
        /// </summary>
        /// <param name="frame">the message</param>
        /// <returns></returns>
        public static DeserializerState CreateRoot(WireFrame frame)
        {
            var state = new DeserializerState(frame.Segments);

            state.DecodePointer(0);
            return(state);
        }
            void ICapnpSerializable.Deserialize(DeserializerState arg_)
            {
                var reader = READER.create(arg_);

                SealFor = CapnpSerializable.Create <TOwner>(reader.SealFor);
                applyDefaults();
            }
            public object Create(DeserializerState state)
            {
                var result = new T();

                if (state.Kind != ObjectKind.Nil)
                {
                    result.Deserialize(state);
                }
                return(result);
            }
        static object?CreateFromAny(DeserializerState state)
        {
            switch (state.Kind)
            {
            case ObjectKind.Capability: return(state.RequireCap <Rpc.BareProxy>());

            case ObjectKind.Nil: return(null);

            default: return(state);
            }
        }
 /// <summary>
 /// Constructs a domain object from a given deserializer state.
 /// </summary>
 /// <typeparam name="T">Type of domain object to construct. Must be one of the following:
 /// <list type="bullet">
 /// <item><description>Type implementing <see cref="ICapnpSerializable"/>. The type must must have a public parameterless constructor.</description></item>
 /// <item><description>A capability interface (<seealso cref="Rpc.InvalidCapabilityInterfaceException"/> for further explanation)</description></item>
 /// <item><description><see cref="string"/></description></item>
 /// <item><description>IReadOnlyList&lt;bool&gt;, IReadOnlyList&lt;sbyte&gt;, IReadOnlyList&lt;byte&gt;</description></item>
 /// <item><description>IReadOnlyList&lt;short&gt;, IReadOnlyList&lt;ushort&gt;, IReadOnlyList&lt;int&gt;</description></item>
 /// <item><description>IReadOnlyList&lt;uint&gt;, IReadOnlyList&lt;long&gt;, IReadOnlyList&lt;ulong&gt;</description></item>
 /// <item><description>IReadOnlyList&lt;float&gt;, IReadOnlyList&lt;double&gt;</description></item>
 /// <item><description>IReadOnlyList&lt;T&gt; whereby T is one of the things listed here.</description></item>
 /// </list>
 /// </typeparam>
 /// <param name="state">deserializer state to construct from</param>
 /// <returns>The domain object instance. Nullability note: The returned reference may be null if
 /// <paramref name="state"/> represents the nil object.</returns>
 /// <exception cref="ArgumentException">Cannot construct object of type <typeparamref name="T"/></exception>
 /// <remarks>Note that capability ownership is moved to the domain object</remarks>
 public static T?Create <T>(DeserializerState state)
     where T : class
 {
     try
     {
         return((T?)GetSerializer(typeof(T))(state));
     }
     catch (TargetInvocationException ex)
     {
         throw new ArgumentException("Failed to construct domain object", ex);
     }
 }
        Task <AnswerOrCounterquestion> Save(DeserializerState d_, CancellationToken cancellationToken_)
        {
            using (d_)
            {
                return(Impatient.MaybeTailCall(Impl.Save(CapnpSerializable.Create <Capnp.Persistent <TSturdyRef, TOwner> .SaveParams>(d_), cancellationToken_), r_ =>
                {
                    var s_ = SerializerState.CreateForRpc <Capnp.Persistent <TSturdyRef, TOwner> .SaveResults.WRITER>();
                    r_.serialize(s_);
                    return s_;
                }

                                               ));
            }
        }
        /// <summary>
        /// Attempts to interpret this instance as List(U) whereby U is a struct, applying a selector function to each element.
        /// </summary>
        /// <param name="cons">Selector function</param>
        /// <returns>The desired representation</returns>
        public override IReadOnlyList <U> Cast <U>(Func <DeserializerState, U> cons)
        {
            switch (Marshal.SizeOf <T>())
            {
            case 1: return(PrimitiveCast <byte>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x))));

            case 2: return(PrimitiveCast <ushort>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x))));

            case 4: return(PrimitiveCast <uint>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x))));

            case 8: return(new ListOfULongAsStructView <U>(PrimitiveCast <ulong>(), cons));

            default:
                throw new InvalidProgramException("This program path should not be reachable");
            }
        }
 public object Create(DeserializerState state)
 {
     return(state.RequireList().Cast(_elementSerializer));
 }
 /// <summary>
 /// Constructs a domain object from a given deserializer state.
 /// </summary>
 /// <typeparam name="T">Type of domain object to construct. Must be one of the following:
 /// <list type="bullet">
 /// <item><description>Type implementing <see cref="ICapnpSerializable"/>. The type must must have a public parameterless constructor.</description></item>
 /// <item><description>A capability interface (<seealso cref="Rpc.InvalidCapabilityInterfaceException"/> for further explanation)</description></item>
 /// <item><description><see cref="String"/></description></item>
 /// <item><description><code>IReadOnlyList{Boolean}</code></description></item>
 /// <item><description><code>IReadOnlyList{SByte}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Byte}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Int16}"</code></description></item>
 /// <item><description><code>IReadOnlyList{UInt16}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Int32}"</code></description></item>
 /// <item><description><code>IReadOnlyList{UInt32}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Int64}"</code></description></item>
 /// <item><description><code>IReadOnlyList{UInt64}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Single}"</code></description></item>
 /// <item><description><code>IReadOnlyList{Double}"</code></description></item>
 /// <item><description><code>IReadOnlyList{T}</code> whereby T is one of the things listed here.</description></item>
 /// </list>
 /// </typeparam>
 /// <param name="state">deserializer state to construct from</param>
 /// <returns>The domain object instance. Nullability note: The returned reference will be null if (and only if) <typeparamref name="T"/> is a capability interface and
 /// <paramref name="state"/> represents the nil object (obtained from a null pointer). For all other types, when the state is nil,
 /// the method still constructs a valid but "empty" object instance (such as domain object without any properties set, empty string, empty list etc.)</returns>
 public static T?Create <T>(DeserializerState state)
     where T : class
 {
     return((T?)GetSerializer(typeof(T))(state));
 }
Beispiel #11
0
 internal ListOfCapsDeserializer(ref DeserializerState state) : base(ref state)
 {
     Rpc.CapabilityReflection.ValidateCapabilityInterface(typeof(T));
 }
        /// <summary>
        /// Performs a deep copy of an existing deserializer state into another serializer state.
        /// This implementation does not analyze the source object graph and therefore cannot detect multiple references to the same object.
        /// Such cases will result in object duplication.
        /// </summary>
        /// <param name="from">source state</param>
        /// <param name="to">target state</param>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Target state was already set to a different object type than the source state.</exception>
        /// <exception cref="DeserializationException">Security violation due to amplification attack or stack overflow DoS attack,
        /// or illegal pointer detected during deserialization.</exception>
        public static void DeepCopy(DeserializerState from, SerializerState to)
        {
            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            if (from.Caps != null && to.Caps != null)
            {
                to.Caps.Clear();
                to.Caps.AddRange(from.Caps);
            }

            var ds = to.Rewrap <DynamicSerializerState>();

            IReadOnlyList <DeserializerState> items;

            switch (from.Kind)
            {
            case ObjectKind.Struct:
                ds.SetStruct(from.StructDataCount, from.StructPtrCount);
                ds.Allocate();
                from.StructDataSection.CopyTo(ds.StructDataSection);
                for (int i = 0; i < from.StructPtrCount; i++)
                {
                    DeepCopy(from.StructReadPointer(i), ds.BuildPointer(i));
                }
                break;

            case ObjectKind.ListOfBits:
                ds.SetListOfValues(1, from.ListElementCount);
                from.RawData.CopyTo(ds.RawData);
                break;

            case ObjectKind.ListOfBytes:
                ds.SetListOfValues(8, from.ListElementCount);
                from.RawData.CopyTo(ds.RawData);
                break;

            case ObjectKind.ListOfEmpty:
                ds.SetListOfValues(0, from.ListElementCount);
                break;

            case ObjectKind.ListOfInts:
                ds.SetListOfValues(32, from.ListElementCount);
                from.RawData.CopyTo(ds.RawData);
                break;

            case ObjectKind.ListOfLongs:
                ds.SetListOfValues(64, from.ListElementCount);
                from.RawData.CopyTo(ds.RawData);
                break;

            case ObjectKind.ListOfShorts:
                ds.SetListOfValues(16, from.ListElementCount);
                from.RawData.CopyTo(ds.RawData);
                break;

            case ObjectKind.ListOfPointers:
                ds.SetListOfPointers(from.ListElementCount);
                items = (IReadOnlyList <DeserializerState>)from.RequireList();
                for (int i = 0; i < from.ListElementCount; i++)
                {
                    DeepCopy(items[i], ds.BuildPointer(i));
                }
                break;

            case ObjectKind.ListOfStructs:
                ds.SetListOfStructs(from.ListElementCount, from.StructDataCount, from.StructPtrCount);
                items = (IReadOnlyList <DeserializerState>)from.RequireList();
                for (int i = 0; i < from.ListElementCount; i++)
                {
                    DeepCopy(items[i], ds.ListBuildStruct(i));
                }
                break;

            case ObjectKind.Capability:
                ds.SetCapability(from.CapabilityIndex);
                break;
            }

            to.InheritFrom(ds);
        }
 public static READER create(DeserializerState ctx) => new READER(ctx);
 public READER(DeserializerState ctx)
 {
     this.ctx = ctx;
 }
 public object?Create(DeserializerState state)
 {
     return(state.RequireCap <T>());
 }
 internal ListDeserializer(ref DeserializerState state)
 {
     State = state;
 }
 internal ListOfBitsDeserializer(ref DeserializerState context, bool defaultValue) :
     base(ref context)
 {
     _defaultValue = defaultValue;
 }
 public IReadOnlyList <T> Create(DeserializerState state)
 {
     return(state.RequireList().Cast(_elementSerializer));
 }