Ejemplo n.º 1
0
            public object this[int index] {
                get {
                    INativeType  type    = ((PointerType)NativeType)._type;
                    MemoryHolder address = MemHolder.ReadMemoryHolder(0);

                    return(type.GetValue(address, this, checked (type.Size * index), false));
                }
                set {
                    MemoryHolder address = MemHolder.ReadMemoryHolder(0);

                    INativeType type      = ((PointerType)NativeType)._type;
                    object      keepAlive = type.SetValue(address, checked (type.Size * index), value);
                    if (keepAlive != null)
                    {
                        MemHolder.AddObject(index.ToString(), keepAlive);
                    }
                }
            }
Ejemplo n.º 2
0
            public object this[Slice index] {
                get {
                    if (index.stop == null)
                    {
                        throw PythonOps.ValueError("slice stop is required");
                    }

                    int start = index.start != null ? (int)index.start : 0;
                    int stop  = index.stop != null ? (int)index.stop : 0;
                    int step  = index.step != null ? (int)index.step : 1;

                    if (step < 0 && index.start == null)
                    {
                        throw PythonOps.ValueError("slice start is required for step < 0");
                    }

                    if (start < 0)
                    {
                        start = 0;
                    }
                    INativeType type     = ((PointerType)NativeType)._type;
                    SimpleType  elemType = type as SimpleType;

                    if ((stop < start && step > 0) || (start < stop && step < 0))
                    {
                        if (elemType != null)
                        {
                            if (elemType._type == SimpleTypeKind.Char)
                            {
                                return(Bytes.Empty);
                            }
                            if (elemType._type == SimpleTypeKind.WChar)
                            {
                                return(string.Empty);
                            }
                        }
                        return(new PythonList());
                    }

                    MemoryHolder address = MemHolder.ReadMemoryHolder(0);
                    if (elemType != null)
                    {
                        if (elemType._type == SimpleTypeKind.Char)
                        {
                            Debug.Assert(((INativeType)elemType).Size == 1);
                            var sb = new MemoryStream();

                            for (int i = start; stop > start ? i <stop : i> stop; i += step)
                            {
                                sb.WriteByte(address.ReadByte(i));
                            }

                            return(Bytes.Make(sb.ToArray()));
                        }
                        if (elemType._type == SimpleTypeKind.WChar)
                        {
                            int elmSize = ((INativeType)elemType).Size;
                            var sb      = new StringBuilder();

                            for (int i = start; stop > start ? i <stop : i> stop; i += step)
                            {
                                sb.Append((char)address.ReadInt16(checked (i * elmSize)));
                            }

                            return(sb.ToString());
                        }
                    }

                    PythonList res = new PythonList((stop - start) / step);
                    for (int i = start; stop > start ? i <stop : i> stop; i += step)
                    {
                        res.AddNoLock(
                            type.GetValue(address, this, checked (type.Size * i), false)
                            );
                    }
                    return(res);
                }
            }