Beispiel #1
0
        void HandleCountSpreadChanged(IDiffSpread <int> spread)
        {
            int oldCount = FIOContainers.Count;
            int newCount = Math.Max(spread[0], 0);

            for (int i = oldCount; i < newCount; i++)
            {
                var attribute = new InputAttribute(string.Format("{0} {1}", FInputAttribute.Name, i + 1))
                {
                    IsPinGroup   = false,
                    Order        = FInputAttribute.Order + FOffsetCounter * 1000 + i,
                    BinOrder     = FInputAttribute.Order + FOffsetCounter * 1000 + i,
                    AutoValidate = false
                };
                var io = FFactory.CreateIOContainer(typeof(IInStream <T>), attribute, false);
                FIOContainers.Add(io);
            }

            for (int i = oldCount - 1; i >= newCount; i--)
            {
                var io = FIOContainers[i];
                FIOContainers.Remove(io);
                io.Dispose();
            }

            FStreams.Length = FIOContainers.Count;
            using (var writer = FStreams.GetWriter())
            {
                foreach (var io in FIOContainers)
                {
                    writer.Write(io.RawIOObject as IInStream <T>);
                }
            }
        }
Beispiel #2
0
        public Spread(IList <T> original)
            : this(original.Count)
        {
            var buffer = new T[original.Count];

            original.CopyTo(buffer, 0);
            using (var writer = FStream.GetWriter())
            {
                writer.Write(buffer, 0, buffer.Length);
            }
        }
Beispiel #3
0
        public bool Sync()
        {
            IsChanged = FAutoValidate ? FNodeIn.PinIsChanged : FNodeIn.Validate();
            if (IsChanged)
            {
                object usI;
                FNodeIn.GetUpstreamInterface(out usI);

                FNodeIn.GetUpStreamSlices(out FLength, out FUpStreamSlices);
                // Check fastest way first: TUpstream == T
                var wrapper = usI as DynamicTypeWrapper;
                if (wrapper != null)
                {
                    usI = wrapper.Value;
                }

                FUpstreamStream = usI as MemoryIOStream <T>;
                if (FUpstreamStream == null)
                {
                    // TUpstream is a subtype of T
                    // Copy the upstream stream through the covariant IEnumerable interface
                    var enumerable = usI as IEnumerable <T>;
                    if (enumerable != null)
                    {
                        FUpstreamStream = enumerable.ToStream();
                    }
                    if (FUpstreamStream == null)
                    {
                        // TUpstream to T needs explicit cast
                        // For example TUpstream is a value type and T is a reference type
                        var objects = usI as IEnumerable;
                        if (objects != null)
                        {
                            FUpstreamStream = objects.Cast <T>().ToStream();
                        }
                        else
                        {
                            // Not connected
                            FUpstreamStream        = FNullStream;
                            FUpstreamStream.Length = FLength;
                            using (var writer = FUpstreamStream.GetWriter())
                                while (!writer.Eos)
                                {
                                    writer.Write(FDefaultValue);
                                }
                        }
                    }
                }
            }
            return(IsChanged);
        }
Beispiel #4
0
 public bool Sync()
 {
     IsChanged = FAutoValidate ? FNodeIn.PinIsChanged : FNodeIn.Validate();
     if (IsChanged)
     {
         object usI;
         FNodeIn.GetUpstreamInterface(out usI);
         // Check fastest way first: TUpstream == T
         FUpstreamStream = usI as MemoryIOStream <T>;
         if (FUpstreamStream != null)
         {
             FNodeIn.GetUpStreamSlices(out FLength, out FUpStreamSlices);
         }
         else
         {
             FLength = FNodeIn.SliceCount;
             // TUpstream is a subtype of T
             var enumerable = usI as IEnumerable <T>;
             if (enumerable != null)
             {
                 FUpstreamStream        = new MemoryIOStream <T>(FLength);
                 FUpstreamStream.Length = FLength;
                 using (var writer = FUpstreamStream.GetWriter())
                     foreach (var item in enumerable)
                     {
                         writer.Write(item);
                     }
             }
             else
             {
                 // Not connected
                 FUpstreamStream        = FNullStream;
                 FUpstreamStream.Length = FLength;
             }
         }
     }
     return(IsChanged);
 }
Beispiel #5
0
        static void ConvertAllAtOnce(Stream srcStream, MemoryIOStream <double> dstStream, double[] buffer, ValueTypeFormat format, ByteOrder byteOrder)
        {
            var binaryReader    = new BinaryReader(srcStream);
            var sizeOfT         = ValueTypeFormatUtils.SizeOf(format);
            var dstStreamLength = (int)srcStream.Length / sizeOfT;

            dstStream.Length = dstStreamLength;
            using (var dstStreamWriter = dstStream.GetWriter())
            {
                while (!dstStreamWriter.Eos)
                {
                    switch (format)
                    {
                    case ValueTypeFormat.Boolean:
                        var booleanBuffer = binaryReader.ReadBooleans(buffer.Length, byteOrder);
                        for (int i = 0; i < booleanBuffer.Length; i++)
                        {
                            buffer[i] = (double)Convert.ChangeType(booleanBuffer[i], TypeCode.Double);
                        }
                        dstStreamWriter.Write(buffer, 0, booleanBuffer.Length);
                        break;

                    case ValueTypeFormat.SByte:
                        var sbyteBuffer = binaryReader.ReadBytes(buffer.Length);
                        for (int i = 0; i < sbyteBuffer.Length; i++)
                        {
                            buffer[i] = (double)(SByte)sbyteBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, sbyteBuffer.Length);
                        break;

                    case ValueTypeFormat.Byte:
                        var byteBuffer = binaryReader.ReadBytes(buffer.Length);
                        for (int i = 0; i < byteBuffer.Length; i++)
                        {
                            buffer[i] = (double)byteBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, byteBuffer.Length);
                        break;

                    case ValueTypeFormat.Int16:
                        var int16Buffer = binaryReader.ReadInt16s(buffer.Length, byteOrder);
                        for (int i = 0; i < int16Buffer.Length; i++)
                        {
                            buffer[i] = (double)int16Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int16Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt16:
                        var uint16Buffer = binaryReader.ReadUInt16s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint16Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint16Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint16Buffer.Length);
                        break;

                    case ValueTypeFormat.Int32:
                        var int32Buffer = binaryReader.ReadInt32s(buffer.Length, byteOrder);
                        for (int i = 0; i < int32Buffer.Length; i++)
                        {
                            buffer[i] = (double)int32Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int32Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt32:
                        var uint32Buffer = binaryReader.ReadUInt32s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint32Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint32Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint32Buffer.Length);
                        break;

                    case ValueTypeFormat.Int64:
                        var int64Buffer = binaryReader.ReadInt64s(buffer.Length, byteOrder);
                        for (int i = 0; i < int64Buffer.Length; i++)
                        {
                            buffer[i] = (double)int64Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, int64Buffer.Length);
                        break;

                    case ValueTypeFormat.UInt64:
                        var uint64Buffer = binaryReader.ReadUInt64s(buffer.Length, byteOrder);
                        for (int i = 0; i < uint64Buffer.Length; i++)
                        {
                            buffer[i] = (double)uint64Buffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, uint64Buffer.Length);
                        break;

                    case ValueTypeFormat.Single:
                        var singleBuffer = binaryReader.ReadSingles(buffer.Length, byteOrder);
                        for (int i = 0; i < singleBuffer.Length; i++)
                        {
                            buffer[i] = (double)singleBuffer[i];
                        }
                        dstStreamWriter.Write(buffer, 0, singleBuffer.Length);
                        break;

                    case ValueTypeFormat.Double:
                        var itemRead = binaryReader.Read(buffer, 0, buffer.Length, byteOrder);
                        dstStreamWriter.Write(buffer, 0, itemRead);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
        }
Beispiel #6
0
        static void ConvertOneByOne(Stream srcStream, MemoryIOStream <double> dstStream, IInStream <ValueTypeFormat> formatStream, ByteOrder byteOrder)
        {
            var binaryReader    = new BinaryReader(srcStream);
            int dstStreamLength = 0;

            using (var writer = dstStream.GetWriter())
                using (var formatStreamReader = formatStream.GetCyclicReader())
                {
                    while (srcStream.Position < srcStream.Length)
                    {
                        var    format = formatStreamReader.Read();
                        double result;
                        switch (format)
                        {
                        case ValueTypeFormat.Boolean:
                            result = (Double)Convert.ChangeType(binaryReader.ReadBoolean(byteOrder), typeof(Double));
                            break;

                        case ValueTypeFormat.SByte:
                            result = (Double)binaryReader.ReadSByte();
                            break;

                        case ValueTypeFormat.Byte:
                            result = (Double)binaryReader.ReadByte();
                            break;

                        case ValueTypeFormat.Int16:
                            result = (Double)binaryReader.ReadInt16(byteOrder);
                            break;

                        case ValueTypeFormat.UInt16:
                            result = (Double)binaryReader.ReadUInt16(byteOrder);
                            break;

                        case ValueTypeFormat.Int32:
                            result = (Double)binaryReader.ReadInt32(byteOrder);
                            break;

                        case ValueTypeFormat.UInt32:
                            result = (Double)binaryReader.ReadUInt32(byteOrder);
                            break;

                        case ValueTypeFormat.Int64:
                            result = (Double)binaryReader.ReadInt64(byteOrder);
                            break;

                        case ValueTypeFormat.UInt64:
                            result = (Double)binaryReader.ReadUInt64(byteOrder);
                            break;

                        case ValueTypeFormat.Single:
                            result = (Double)binaryReader.ReadSingle(byteOrder);
                            break;

                        case ValueTypeFormat.Double:
                            result = binaryReader.ReadDouble(byteOrder);
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                        writer.Write(result);
                        dstStreamLength++;
                    }
                }
            dstStream.Length = dstStreamLength;
        }
Beispiel #7
0
        // Called sequentially for all VL plugins on main thread
        public void SyncPinsAndRestoreState(BuildResult buildResult, HotSwapper swapper)
        {
            if (buildResult == FBuildResult)
            {
                return; // Same build result, nothing to do
            }
            if (buildResult == null)
            {
                return; // Something went seriously wrong during compile phase
            }
            if (buildResult.ClrMethodToCall == null)
            {
                FBuildResult = buildResult;
                return; // Something went wrong during compile phase
            }
            if (buildResult.Compilation.Age < FBuildResult.Compilation?.Age)
            {
                return; // Dismiss outdated builds
            }
            if (buildResult.ClrMethodToCall == FBuildResult.ClrMethodToCall)
            {
                // Same CLR method, nothing to do except for remember the new result
                // Holding on to the old would introduce a memory leak as old solutions and compilation could not get garbage collected
                FBuildResult = buildResult;
                return;
            }
            try
            {
                // Synchronize pins
                SyncPins(buildResult);

                // Restore the state
                if (FInstances.Length > 0)
                {
                    var instanceType = buildResult.InstanceType;
                    using (var reader = FInstances.GetReader())
                        using (var writer = FInstances.GetWriter())
                        {
                            while (!reader.Eos)
                            {
                                var oldInstance = reader.Read();
                                if (oldInstance != null)
                                {
                                    var newInstance = swapper.Swap(oldInstance, buildResult.Scope, buildResult.Factory);
                                    writer.Write(newInstance);
                                }
                                else
                                {
                                    var newInstance = instanceType != null?buildResult.Factory.CreateInstance(instanceType, FInstanceId) : null;

                                    writer.Write(newInstance);
                                }
                            }
                        }
                }
            }
            catch (Exception exception)
            {
                LogException(exception);
                throw;
            }
            finally
            {
                FBuildResult = buildResult;
            }
        }
Beispiel #8
0
 public IStreamWriter <T> GetWriter()
 {
     CopyOnWrite();
     return(FIOStream.GetWriter());
 }
        void ConvertOneByOne(Stream srcStream, MemoryIOStream<double> dstStream, IInStream<ValueTypeFormat> formatStream, ByteOrder byteOrder)
        {
            var binaryReader = new BinaryReader(srcStream);
            int dstStreamLength = 0;
        	int counter = 0;
        	int maxCount = 0;

            using (var writer = dstStream.GetWriter())
            using (var formatStreamReader = formatStream.GetCyclicReader())
            {
                while (srcStream.Position < srcStream.Length && counter <= maxCount)
                {
                    var format = formatStreamReader.Read();
                    double result;
                    switch (format)
                    {
                        case ValueTypeFormat.Boolean:
                            result = (Double)Convert.ChangeType(binaryReader.ReadBoolean(byteOrder), typeof(Double));
                            break;
                        case ValueTypeFormat.SByte:
                            result = (Double)binaryReader.ReadSByte();
                            break;
                        case ValueTypeFormat.Byte:
                            result = (Double)binaryReader.ReadByte();
                            break;
                        case ValueTypeFormat.Int16:
                            result = (Double)binaryReader.ReadInt16(byteOrder);
                            break;
                        case ValueTypeFormat.UInt16:
                            result = (Double)binaryReader.ReadUInt16(byteOrder);
                            break;
                        case ValueTypeFormat.Int32:
                            result = (Double)binaryReader.ReadInt32(byteOrder);
                            break;
                        case ValueTypeFormat.UInt32:
                            result = (Double)binaryReader.ReadUInt32(byteOrder);
                            break;
                        case ValueTypeFormat.Int64:
                            result = (Double)binaryReader.ReadInt64(byteOrder);
                            break;
                        case ValueTypeFormat.UInt64:
                            result = (Double)binaryReader.ReadUInt64(byteOrder);
                            break;
                        case ValueTypeFormat.Single:
                            result = (Double)binaryReader.ReadSingle(byteOrder);
                            break;
                        case ValueTypeFormat.Double:
                            result = binaryReader.ReadDouble(byteOrder);
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                    //if (result == -1) stop = true;
                	if ( counter == 0 ) maxCount = (int)result;
                	else{
	                	writer.Write(result);
	                    dstStreamLength++;
                	}
                	counter++;
                }
            	if(srcStream.Position == srcStream.Length) FEof[0] = true;
            }
            dstStream.Length = dstStreamLength;
        }