Ejemplo n.º 1
0
        public void WriteStructure <T>(T value)
        {
            FieldInfo[] fis = ArgTypeInspector.GetStructFields(typeof(T));

            if (fis.Length == 0)
            {
                return;
            }

            if (!_skipNextStructPadding)
            {
                WritePad(8);
            }
            _skipNextStructPadding = false;

            object boxed = value;

            if (MessageReader.IsEligibleStruct(typeof(T), fis))
            {
                byte[] buffer = new byte[Marshal.SizeOf(fis[0].FieldType) * fis.Length];

                //unsafe {
                GCHandle valueHandle = GCHandle.Alloc(boxed, GCHandleType.Pinned);
                Marshal.Copy(valueHandle.AddrOfPinnedObject(), buffer, 0, buffer.Length);
                valueHandle.Free();
                //}
                stream.Write(buffer, 0, buffer.Length);
                return;
            }

            foreach (var fi in fis)
            {
                Write(fi.FieldType, fi.GetValue(boxed));
            }
        }
Ejemplo n.º 2
0
        public T ReadValueTupleStruct <T> ()
        {
            if (!_skipNextStructPadding)
            {
                ReadPad(8);
            }
            _skipNextStructPadding = false;

            bool isValueTuple = true;

            FieldInfo[] fis = ArgTypeInspector.GetStructFields(typeof(T), isValueTuple);

            // Empty struct? No need for processing
            if (fis.Length == 0)
            {
                return(default(T));
            }

            object val = Activator.CreateInstance <T> ();

            for (int i = 0; i < fis.Length; i++)
            {
                var fi = fis[i];
                if (i == 7 && isValueTuple)
                {
                    _skipNextStructPadding = true;
                }
                fi.SetValue(val, Read(fi.FieldType));
            }

            return((T)val);
        }
Ejemplo n.º 3
0
        public void WriteValueTupleStructure <T> (T value)
        {
            if (!_skipNextStructPadding)
            {
                WritePad(8);
            }
            _skipNextStructPadding = false;
            FieldInfo[] fis = ArgTypeInspector.GetStructFields(typeof(T), isValueTuple: true);
            if (fis.Length == 0)
            {
                return;
            }

            object boxed = value;

            for (int i = 0; i < fis.Length;)
            {
                var fi = fis[i];
                if (i == 7)
                {
                    boxed = fi.GetValue(boxed);
                    fis   = ArgTypeInspector.GetStructFields(fi.FieldType, isValueTuple: true);
                    i     = 0;
                }
                else
                {
                    Write(fi.FieldType, fi.GetValue(boxed), isCompileTimeType: true);
                    i++;
                }
            }
        }
Ejemplo n.º 4
0
        public T ReadStruct <T> ()
        {
            if (!_skipNextStructPadding)
            {
                ReadPad(8);
            }
            _skipNextStructPadding = false;

            FieldInfo[] fis = ArgTypeInspector.GetStructFields(typeof(T), isValueTuple: false);

            // Empty struct? No need for processing
            if (fis.Length == 0)
            {
                return(default(T));
            }

            if (IsEligibleStruct(typeof(T), fis))
            {
                return((T)MarshalStruct(typeof(T), fis));
            }

            object val = Activator.CreateInstance <T> ();

            foreach (System.Reflection.FieldInfo fi in fis)
            {
                fi.SetValue(val, Read(fi.FieldType));
            }

            return((T)val);
        }
Ejemplo n.º 5
0
        public static Signature GetSig(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.GetTypeInfo().IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            if (type == typeof(bool))
            {
                return(BoolSig);
            }
            else if (type == typeof(byte))
            {
                return(ByteSig);
            }
            else if (type == typeof(double))
            {
                return(DoubleSig);
            }
            else if (type == typeof(short))
            {
                return(Int16Sig);
            }
            else if (type == typeof(int))
            {
                return(Int32Sig);
            }
            else if (type == typeof(long))
            {
                return(Int64Sig);
            }
            else if (type == typeof(ObjectPath))
            {
                return(ObjectPathSig);
            }
            else if (type == typeof(Signature))
            {
                return(SignatureSig);
            }
            else if (type == typeof(string))
            {
                return(StringSig);
            }
            else if (type == typeof(float))
            {
                return(DoubleSig); // SingleSig
            }
            else if (type == typeof(ushort))
            {
                return(UInt16Sig);
            }
            else if (type == typeof(uint))
            {
                return(UInt32Sig);
            }
            else if (type == typeof(ulong))
            {
                return(UInt64Sig);
            }
            else if (type == typeof(object))
            {
                return(VariantSig);
            }
            else if (type == typeof(IDBusObject))
            {
                return(ObjectPathSig);
            }
            else if (type == typeof(void))
            {
                return(Empty);
            }

            if (ArgTypeInspector.IsDBusObjectType(type))
            {
                return(ObjectPathSig);
            }

            var enumerableType = ArgTypeInspector.InspectEnumerableType(type, out Type elementType);

            if (enumerableType != ArgTypeInspector.EnumerableType.NotEnumerable)
            {
                if ((enumerableType == ArgTypeInspector.EnumerableType.EnumerableKeyValuePair) ||
                    (enumerableType == ArgTypeInspector.EnumerableType.GenericDictionary) ||
                    (enumerableType == ArgTypeInspector.EnumerableType.AttributeDictionary))
                {
                    Type keyType   = elementType.GenericTypeArguments[0];
                    Type valueType = elementType.GenericTypeArguments[1];
                    return(Signature.MakeDict(GetSig(keyType), GetSig(valueType)));
                }
                else // Enumerable
                {
                    return(MakeArray(GetSig(elementType)));
                }
            }

            if (ArgTypeInspector.IsStructType(type, out bool isValueTuple))
            {
                Signature sig    = Signature.Empty;
                var       fields = ArgTypeInspector.GetStructFields(type, isValueTuple);
                foreach (FieldInfo fi in fields)
                {
                    sig += GetSig(fi.FieldType);
                }

                return(Signature.MakeStruct(sig));
            }

            throw new ArgumentException($"Cannot (de)serialize Type '{type.FullName}'");
        }