示例#1
0
        public U this [T key] {
            get {
                unsafe {
                    Type retType = typeof(Tuple <U, bool>);

                    fixed(byte *thisPtr = SwiftData)
                    {
                        var thisIntPtr = new IntPtr(thisPtr);

                        byte *retBuff = stackalloc byte [StructMarshal.Marshaler.Sizeof(retType)];
                        var   retPtr  = new IntPtr(retBuff);

                        byte *keyBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(T))];
                        var   keyBufferPtr = new IntPtr(keyBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(T), key, keyBufferPtr);

                        DictPI.DictGet(retPtr, thisIntPtr, keyBufferPtr,
                                       StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                       StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                       StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
                        var retTuple = StructMarshal.Marshaler.ToNet <Tuple <U, bool> > (retPtr);

                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), keyBufferPtr);
                        if (retTuple.Item2)
                        {
                            return(retTuple.Item1);
                        }
                        throw new KeyNotFoundException($"key {key} not found");
                    }
                }
            }

            set {
                unsafe
                {
                    fixed(byte *thisPtr = SwiftData)
                    {
                        var   thisIntPtr   = new IntPtr(thisPtr);
                        byte *keyBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(T))];
                        var   keyBufferPtr = new IntPtr(keyBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(T), key, keyBufferPtr);
                        byte *valBuffer    = stackalloc byte [StructMarshal.Marshaler.Sizeof(typeof(U))];
                        var   valBufferPtr = new IntPtr(valBuffer);

                        StructMarshal.Marshaler.ToSwift(typeof(U), value, valBufferPtr);
                        DictPI.DictSet(thisIntPtr, keyBufferPtr, valBufferPtr,
                                       StructMarshal.Marshaler.Metatypeof(typeof(T)),
                                       StructMarshal.Marshaler.Metatypeof(typeof(U)),
                                       StructMarshal.Marshaler.ProtocolWitnessof(typeof(ISwiftHashable), typeof(T)));
                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), keyBufferPtr);
                        StructMarshal.Marshaler.ReleaseSwiftPointer(typeof(T), valBufferPtr);
                    }
                }
            }
        }