예제 #1
0
        static IEnumerable <string> BuildCSharpMembers(CompositeDsdlTypeBase type)
        {
            foreach (var m in type.Fields)
            {
                var fieldType = GetCSharpType(m.Type, type.IsUnion);
                if (fieldType != null)
                {
                    yield return($"[DataMember(Name = \"{m.Name}\")]");

                    yield return($"public {fieldType} {GetCSharpPropertyName(m.Name)} {{ get; set; }}");
                }
            }
        }
예제 #2
0
 static void ResolveNestedTypesCore(CompositeDsdlTypeBase t, IUavcanTypeResolver typeResolver)
 {
     foreach (var f in t.Fields)
     {
         if (f.Type is DsdlTypeReference reference)
         {
             var nestedType = typeResolver.ResolveType(reference.Namespace, reference.Name);
             f.Type = CastNestedType(nestedType);
         }
         else if (f.Type is ArrayDsdlType adt && adt.ElementType is DsdlTypeReference elementTypeReference)
         {
             var nestedType = typeResolver.ResolveType(elementTypeReference.Namespace, elementTypeReference.Name);
             adt.SetElementType(CastNestedType(nestedType));
         }
     }
 }
예제 #3
0
        object PopulateDictionary(
            IDictionary dictionary,
            BitStreamReader reader,
            DictionaryContract contract,
            DsdlProperty containerProperty,
            CompositeDsdlTypeBase scheme,
            bool tailArrayOptimization)
        {
            object underlyingDictionary = dictionary is IWrappedDictionary wrappedDictionary ? wrappedDictionary.UnderlyingDictionary : dictionary;

            if (contract.KeyContract == null)
            {
                contract.KeyContract = GetContractSafe(contract.DictionaryKeyType);
            }

            if (contract.ItemContract == null)
            {
                contract.ItemContract = GetContractSafe(contract.DictionaryValueType);
            }

            foreach (var(field, tao) in EnumerateSchemeFields(reader, scheme, tailArrayOptimization))
            {
                if (field.Type is VoidDsdlType t)
                {
                    ReadAlignment(reader, t);
                    continue;
                }

                var keyValue = EnsureType(reader, field.Name, CultureInfo.InvariantCulture, contract.KeyContract, contract.DictionaryKeyType);

                var itemValue = CreateValueInternal(reader,
                                                    contract.ItemContract,
                                                    null,
                                                    contract,
                                                    containerProperty,
                                                    null,
                                                    field.Type,
                                                    null,
                                                    tao);

                dictionary[keyValue] = itemValue;
            }

            return(underlyingDictionary);
        }
예제 #4
0
        static void PrintObject(object obj, StringBuilder sb, int tabs, CompositeDsdlTypeBase t)
        {
            var dict = obj as IDictionary;

            if (dict == null)
            {
                throw new ArgumentException("Cannot cast object to dictionary.", nameof(obj));
            }

            if (tabs != 0)
            {
                sb.AppendLine("{");
            }

            bool isFirst = true;

            foreach (var field in t.Fields)
            {
                if (!isFirst)
                {
                    sb.Append(",");
                    sb.AppendLine();
                }
                isFirst = false;

                Tab(sb, tabs);
                sb.Append(field.Name);
                sb.Append(": ");

                if (dict.Contains(field.Name))
                {
                    var value = dict[field.Name];
                    Print(value, sb, tabs + 1, field.Type);
                }
            }

            if (tabs != 0)
            {
                sb.AppendLine();
                Tab(sb, tabs - 1);
                sb.Append("}");
            }
        }
예제 #5
0
        void ValidateDsdlTypeCompatibility(ObjectContract contract, CompositeDsdlTypeBase scheme)
        {
            if (scheme == null)
            {
                return;
            }

            foreach (var member in scheme.Fields)
            {
                if (member.Type is VoidDsdlType)
                {
                    continue;
                }

                if (contract.Properties.GetProperty(member.Name, StringComparison.Ordinal) == null)
                {
                    throw new InvalidOperationException($"Cannot find {member.Name} member for type {contract.UnderlyingType.FullName}.");
                }
            }
        }
예제 #6
0
        IEnumerable <(DsdlField Field, bool TAO)> EnumerateSchemeFields(
            BitStreamReader reader,
            CompositeDsdlTypeBase scheme,
            bool tailArrayOptimization)
        {
            if (scheme.IsUnion)
            {
                var unionFieldIndex = ReadUnionFieldIndex(reader, scheme);
                var field           = scheme.Fields[unionFieldIndex];
                yield return(field, false);
            }
            else
            {
                for (int i = 0; i < scheme.Fields.Count - 1; i++)
                {
                    var field = scheme.Fields[i];
                    yield return(field, false);
                }

                if (scheme.Fields.Count > 0)
                {
                    var field = scheme.Fields[scheme.Fields.Count - 1];
                    switch (field.Type)
                    {
                    case ArrayDsdlType _:
                        yield return(field, tailArrayOptimization);

                        break;

                    default:
                        yield return(field, false);

                        break;
                    }
                }
            }
        }
예제 #7
0
        private object PopulateObject(
            object newObject,
            BitStreamReader
            reader,
            ObjectContract contract,
            DsdlProperty member,
            CompositeDsdlTypeBase scheme,
            bool tailArrayOptimization)
        {
            foreach (var(field, tao) in EnumerateSchemeFields(reader, scheme, tailArrayOptimization))
            {
                if (field.Type is VoidDsdlType t)
                {
                    ReadAlignment(reader, t);
                    continue;
                }

                // attempt exact case match first
                // then try match ignoring case
                DsdlProperty property = contract.Properties.GetClosestMatchProperty(field.Name);

                if (property == null)
                {
                    throw new SerializationException($"Could not find member '{field.Name}' on object of type '{contract.UnderlyingType.FullName}'");
                }

                if (property.PropertyContract == null)
                {
                    property.PropertyContract = GetContractSafe(property.PropertyType);
                }

                SetPropertyValue(property, contract, member, reader, newObject, field.Type, tao);
            }

            return(newObject);
        }
예제 #8
0
        object CreateObject(
            BitStreamReader reader,
            IContract contract,
            DsdlProperty member,
            ContainerContract containerContract,
            DsdlProperty containerMember,
            object existingValue,
            Type objectType,
            CompositeDsdlTypeBase scheme,
            bool tailArrayOptimization)
        {
            if (HasNoDefinedType(contract))
            {
                return(CreateUnknownObject(reader, scheme));
            }

            switch (contract)
            {
            case ObjectContract objectContract:
            {
                bool   createdFromNonDefaultCreator = false;
                object targetObject;
                // check that if type name handling is being used that the existing value is compatible with the specified type
                if (existingValue != null && objectType != null && objectType.IsAssignableFrom(existingValue.GetType()))
                {
                    targetObject = existingValue;
                }
                else
                {
                    targetObject = CreateNewObject(reader, objectContract, member, containerMember, out createdFromNonDefaultCreator);
                }

                // don't populate if read from non-default creator because the object has already been read
                if (createdFromNonDefaultCreator)
                {
                    return(targetObject);
                }

                return(PopulateObject(targetObject, reader, objectContract, member, scheme, tailArrayOptimization));
            }

            case DictionaryContract dictionaryContract:
            {
                object targetDictionary;

                if (existingValue == null)
                {
                    var dictionary = CreateNewDictionary(reader, dictionaryContract, out bool createdFromNonDefaultCreator);

                    if (createdFromNonDefaultCreator && !dictionaryContract.HasParameterizedCreatorInternal)
                    {
                        throw new SerializationException("Cannot deserialize readonly or fixed size dictionary: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
                    }

                    PopulateDictionary(dictionary, reader, dictionaryContract, member, scheme, tailArrayOptimization);

                    if (createdFromNonDefaultCreator)
                    {
                        ObjectConstructor <object> creator = dictionaryContract.OverrideCreator ?? dictionaryContract.ParameterizedCreator;

                        return(creator(dictionary));
                    }
                    else if (dictionary is IWrappedDictionary wrappedDictionary)
                    {
                        return(wrappedDictionary.UnderlyingDictionary);
                    }

                    targetDictionary = dictionary;
                }
                else
                {
                    targetDictionary = PopulateDictionary(
                        dictionaryContract.ShouldCreateWrapper || !(existingValue is IDictionary) ? dictionaryContract.CreateWrapper(existingValue) : (IDictionary)existingValue,
                        reader,
                        dictionaryContract,
                        member,
                        scheme,
                        tailArrayOptimization);
                }

                return(targetDictionary);
            }
            }

            throw new SerializationException($"Cannot deserialize the current object.");
        }
예제 #9
0
        int ReadUnionFieldIndex(BitStreamReader reader, CompositeDsdlTypeBase t)
        {
            var bitLen = BitSerializer.IntBitLength(t.Fields.Count);

            return((int)BitSerializer.ReadUInt(reader, bitLen));
        }
예제 #10
0
        void WriteUnionFieldIndex(BitStreamWriter writer, int index, CompositeDsdlTypeBase t)
        {
            var bitLen = BitSerializer.IntBitLength(t.Fields.Count);

            BitSerializer.Write(writer, index, bitLen);
        }