Inheritance: ITypeDescriptor, IPersistTypeDescriptor
Example #1
0
            public ITypeDescriptor Create(Type type)
            {
                ITypeDescriptor result;

                if (_type2DescriptorMap.TryGetValue(type, out result))
                {
                    return(result);
                }
                if (_temporaryMap.TryGetValue(type, out result))
                {
                    return(result);
                }
                if (!type.IsSubclassOf(typeof(Delegate)))
                {
                    if (type.IsGenericType)
                    {
                        if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList <>)))
                        {
                            result = new ListTypeDescriptor(_typeSerializers, type);
                        }
                        else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary <,>)))
                        {
                            result = new DictionaryTypeDescriptor(_typeSerializers, type);
                        }
                        else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IIndirect <>)))
                        {
                            return(null);
                        }
                    }
                    else if (type.IsArray)
                    {
                        result = new ListTypeDescriptor(_typeSerializers, type);
                    }
                    else if (type.IsEnum)
                    {
                        result = new EnumTypeDescriptor(_typeSerializers, type);
                    }
                    else if (type.IsValueType)
                    {
                        throw new BTDBException($"Unsupported value type {type.Name}.");
                    }
                    else
                    {
                        result = new ObjectTypeDescriptor(_typeSerializers, type);
                    }
                }
                _temporaryMap[type] = result;
                if (result != null)
                {
                    if (!result.FinishBuildFromType(this))
                    {
                        _temporaryMap.Remove(type);
                        return(null);
                    }
                }
                return(result);
            }
Example #2
0
            public object GetFieldByIdx(int idx, string fieldName, ObjectTypeDescriptor descriptor)
            {
                if (_ownerDescriptor == descriptor)
                {
                    if (idx < 0)
                    {
                        ThrowMemberAccessException(fieldName);
                    }
                    return(_fieldValues[idx]);
                }
                var realidx = _ownerDescriptor.FindFieldIndexWithThrow(fieldName);

                return(_fieldValues[realidx]);
            }
Example #3
0
            public void SetFieldByIdx(int idx, string fieldName, ObjectTypeDescriptor descriptor, object value)
            {
                if (_ownerDescriptor == descriptor)
                {
                    if (idx < 0)
                    {
                        ThrowMemberAccessException(fieldName);
                    }
                    _fieldValues[idx] = value;
                    return;
                }

                var realIndex = _ownerDescriptor.FindFieldIndexWithThrow(fieldName);

                _fieldValues[realIndex] = value;
            }
Example #4
0
            public ITypeDescriptor Create(Type type)
            {
                ITypeDescriptor result;

                if (_type2DescriptorMap.TryGetValue(type, out result))
                {
                    return(result);
                }
                if (_temporaryMap.TryGetValue(type, out result))
                {
                    return(result);
                }
                if (!type.IsSubclassOf(typeof(Delegate)))
                {
                    if (type.IsGenericType)
                    {
                        if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList <>)))
                        {
                            result = new ListTypeDescriptor(_typeSerializers, type);
                        }
                        else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary <,>)))
                        {
                            result = new DictionaryTypeDescriptor(_typeSerializers, type);
                        }
                    }
                    else if (type.IsArray)
                    {
                        result = new ListTypeDescriptor(_typeSerializers, type);
                    }
                    else if (type.IsEnum)
                    {
                        result = new EnumTypeDescriptor(_typeSerializers, type);
                    }
                    else
                    {
                        result = new ObjectTypeDescriptor(_typeSerializers, type);
                    }
                }
                _temporaryMap[type] = result;
                if (result != null)
                {
                    result.FinishBuildFromType(this);
                }
                return(result);
            }
Example #5
0
        public ITypeDescriptor CloneAndMapNestedTypes(ITypeDescriptorCallbacks typeSerializers,
                                                      Func <ITypeDescriptor, ITypeDescriptor> map)
        {
            var tds = new ITypeDescriptor[_fields.Count];

            for (var i = 0; i < _fields.Count; i++)
            {
                tds[i] = map(_fields[i].Value);
            }

            if (typeSerializers == _typeSerializers && tds.SequenceEqual(_fields.Select(i => i.Value)))
            {
                return(this);
            }
            var res = new ObjectTypeDescriptor(typeSerializers, Name, Sealed);

            for (var i = 0; i < _fields.Count; i++)
            {
                res._fields.Add(new KeyValuePair <string, ITypeDescriptor>(_fields[i].Key, tds[i]));
            }

            return(res);
        }
Example #6
0
 public TypeNewDescriptorGenerator(ObjectTypeDescriptor objectTypeDescriptor)
 {
     _objectTypeDescriptor = objectTypeDescriptor;
 }
Example #7
0
 public DynamicObject(ObjectTypeDescriptor ownerDescriptor)
 {
     _ownerDescriptor = ownerDescriptor;
     _fieldValues     = new object[_ownerDescriptor._fields.Count];
 }
Example #8
0
 public ITypeDescriptor Create(Type type)
 {
     SerializerTypeInfo result;
     if (_typeOrDescriptor2Info.TryGetValue(type, out result)) return result.Descriptor;
     if (_typeOrDescriptor2InfoNew.TryGetValue(type, out result)) return result.Descriptor;
     ITypeDescriptor desc = null;
     if (!type.IsSubclassOf(typeof(Delegate)))
     {
         if (type.IsGenericType)
         {
             if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList<>)))
             {
                 desc = new ListTypeDescriptor(this, type);
             }
             else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary<,>)))
             {
                 desc = new DictionaryTypeDescriptor(this, type);
             }
         }
         else if (type.IsArray)
         {
             desc = new ListTypeDescriptor(this, type);
         }
         else if (type.IsEnum)
         {
             desc = new EnumTypeDescriptor(this, type);
         }
         else
         {
             desc = new ObjectTypeDescriptor(this, type);
         }
     }
     if (desc == null) throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName());
     result = new SerializerTypeInfo
     {
         Id = 0,
         Descriptor = desc
     };
     _typeOrDescriptor2InfoNew[desc] = result;
     _typeOrDescriptor2InfoNew[type] = result;
     if (!desc.FinishBuildFromType(this))
     {
         throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName());
     }
     return desc;
 }
Example #9
0
 public void ProcessMetadataLog(ByteBuffer buffer)
 {
     var reader = new ByteBufferReader(buffer);
     var typeId = reader.ReadVInt32();
     while (typeId != 0)
     {
         var typeCategory = (TypeCategory)reader.ReadUInt8();
         ITypeDescriptor descriptor;
         switch (typeCategory)
         {
             case TypeCategory.BuildIn:
                 throw new ArgumentOutOfRangeException();
             case TypeCategory.Class:
                 descriptor = new ObjectTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.List:
                 descriptor = new ListTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.Dictionary:
                 descriptor = new DictionaryTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.Enum:
                 descriptor = new EnumTypeDescriptor(this, reader);
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
         while (-typeId - 1 >= _id2InfoNew.Count)
             _id2InfoNew.Add(null);
         if (_id2InfoNew[-typeId - 1] == null)
             _id2InfoNew[-typeId - 1] = new SerializerTypeInfo { Id = typeId, Descriptor = descriptor };
         typeId = reader.ReadVInt32();
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
         {
             var placeHolderDescriptor = d as PlaceHolderDescriptor;
             return placeHolderDescriptor != null ? _id2InfoNew[-placeHolderDescriptor.TypeId - 1].Descriptor : d;
         });
     }
     // This additional cycle is needed to fill names of recursive structures
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d => d);
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         var infoForType = _id2InfoNew[i];
         for (var j = ReservedBuildinTypes; j < _id2Info.Count; j++)
         {
             if (infoForType.Descriptor.Equals(_id2Info[j].Descriptor))
             {
                 _remapToOld[infoForType.Descriptor] = _id2Info[j].Descriptor;
                 _id2InfoNew[i] = _id2Info[j];
                 infoForType = _id2InfoNew[i];
                 break;
             }
         }
         if (infoForType.Id < 0)
         {
             infoForType.Id = _id2Info.Count;
             _id2Info.Add(infoForType);
             _typeOrDescriptor2Info[infoForType.Descriptor] = infoForType;
         }
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
         {
             ITypeDescriptor res;
             return _remapToOld.TryGetValue(d, out res) ? res : d;
         });
     }
     _id2InfoNew.Clear();
     _remapToOld.Clear();
 }
Example #10
0
        public void LoadTypeDescriptors(AbstractBufferedReader reader)
        {
            var typeId      = reader.ReadVUInt32();
            var firstTypeId = typeId;

            while (typeId != 0)
            {
                if (typeId < firstTypeId)
                {
                    firstTypeId = typeId;
                }
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(_typeSerializers, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (typeId >= _id2DescriptorMap.Count)
                {
                    _id2DescriptorMap.Add(null);
                }
                if (_id2DescriptorMap[(int)typeId] == null)
                {
                    _id2DescriptorMap[(int)typeId] = new InfoForType {
                        Id = (int)typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVUInt32();
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2DescriptorMap[(int)placeHolderDescriptor.TypeId].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                var infoForType = _id2DescriptorMap[(int)i];
                var descriptor  = _typeSerializers.MergeDescriptor(infoForType.Descriptor);
                infoForType.Descriptor             = descriptor;
                _typeOrDescriptor2Info[descriptor] = infoForType;
            }
        }

        ITypeDescriptor NestedDescriptorReader(AbstractBufferedReader reader)
        {
            var typeId = reader.ReadVUInt32();

            if (typeId < _id2DescriptorMap.Count)
            {
                var infoForType = _id2DescriptorMap[(int)typeId];
                if (infoForType != null)
                {
                    return(infoForType.Descriptor);
                }
            }
            return(new PlaceHolderDescriptor(typeId));
        }
Example #11
0
 public ITypeDescriptor Create(Type type)
 {
     ITypeDescriptor result;
     if (_type2DescriptorMap.TryGetValue(type, out result)) return result;
     if (_temporaryMap.TryGetValue(type, out result)) return result;
     if (!type.IsSubclassOf(typeof(Delegate)))
     {
         if (type.IsGenericType)
         {
             if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList<>)))
             {
                 result = new ListTypeDescriptor(_typeSerializers, type);
             }
             else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary<,>)))
             {
                 result = new DictionaryTypeDescriptor(_typeSerializers, type);
             }
             else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IIndirect<>)))
             {
                 return null;
             }
         }
         else if (type.IsArray)
         {
             result = new ListTypeDescriptor(_typeSerializers, type);
         }
         else if (type.IsEnum)
         {
             result = new EnumTypeDescriptor(_typeSerializers, type);
         }
         else
         {
             result = new ObjectTypeDescriptor(_typeSerializers, type);
         }
     }
     _temporaryMap[type] = result;
     if (result != null)
     {
         if (!result.FinishBuildFromType(this))
         {
             _temporaryMap.Remove(type);
             return null;
         }
     }
     return result;
 }
Example #12
0
 public TypeNewDescriptorGenerator(ObjectTypeDescriptor objectTypeDescriptor)
 {
     _objectTypeDescriptor = objectTypeDescriptor;
 }
Example #13
0
 public void SetFieldByIdx(int idx, string fieldName, ObjectTypeDescriptor descriptor, object value)
 {
     if (_ownerDescriptor == descriptor)
     {
         if (idx < 0)
             ThrowMemberAccessException(fieldName);
         _fieldValues[idx] = value;
         return;
     }
     var realidx = _ownerDescriptor.FindFieldIndexWithThrow(fieldName);
     _fieldValues[realidx] = value;
 }
Example #14
0
 public DynamicObject(ObjectTypeDescriptor ownerDescriptor)
 {
     _ownerDescriptor = ownerDescriptor;
     _fieldValues = new object[_ownerDescriptor._fields.Count];
 }