/// <summary> /// Adds an object. /// </summary> /// <param name="o"></param> /// <returns>The object's ID. IDs are unique within any any given object type but not across types.</returns> public int Add(object o) { if (o == null) { return(-1); } var type = o.GetType(); if (!KnownTypes.ContainsKey(type.AssemblyQualifiedName)) { KnownTypes.Add(type.AssemblyQualifiedName, type); } if (!KnownObjects.ContainsKey(type)) { KnownObjects.Add(type, new List <object>()); } // if (!KnownObjects[type].Contains(o)) KnownObjects[type].Add(o); if (!KnownIDs.ContainsKey(type)) { KnownIDs.Add(type, new SafeDictionary <object, int>()); } var id = KnownObjects[type].Count - 1; KnownIDs[type].Add(o, id); AddProperties(type); return(id); }
/// <summary> /// Returns the namespaced type name (if neccessary) /// </summary> /// <param name="st">This thing's SingleType</param> /// <param name="type">(optional) the type string</param> /// <returns>duh</returns> public static string GetNamespacedType(SingleType st, string type = null) { if (type == null) type = st.Type; if (st.Package != null && !KnownTypes.ContainsKey(st.rostype) && !type.Contains(st.Package)) return string.Format("Messages.{0}.{1}", st.Package, type); return type; }
static string TypeToCode(Type type) { if (KnownTypes.ContainsKey(type.Name)) { return(KnownTypes[type.Name]); } if (type.IsGenericType) { return(type.Name.Substring(0, type.Name.IndexOf("`")) + "<" + String.Join(", ", type.GetGenericArguments().Select(t => TypeToCode(t)).ToArray()) + ">"); } else { return(type.Name); } }
public static void AddKnownTypes(IReadOnlyList <Type> types) { var key = (short)KnownKeys.Count; foreach (var type in types) { if (KnownTypes.ContainsKey(type)) { continue; } KnownKeys.Add(key, type); KnownTypes.Add(type, key); key++; } }
public void Entity(Type type, bool ignoreUnsupported = false) { type = type ?? throw new ArgumentNullException(nameof(type)); if (!typeof(IOgmEntity).IsAssignableFrom(type)) { throw new ArgumentException($"must be assignable to {typeof(IOgmEntity).FullName}", nameof(type)); } if (type.IsSealed) { throw new ArgumentException("Unable to manage sealed types.", nameof(type)); } if (type.GetMethods().Where(p => p.Name != nameof(Object.GetType) && !p.IsSpecialName).Any(p => !p.IsVirtual)) { throw new ArgumentException("Unable to manage type with non virtual methods", nameof(type)); } List <PropertyInfo> unsupported = type.GetProperties() .Where( p => ( !typeof(IOgmConnection).IsAssignableFrom(type) || (p.Name != nameof(IOgmConnection.Source) && p.Name != nameof(IOgmConnection.Destination)) ) && !IsGraphProperty(p) ).ToList(); if (unsupported.Count > 0 && !ignoreUnsupported) { throw new ArgumentException($"Unable to manage type with non virtual properties or properties no deriving from {typeof(IOgmEntity).FullName} or compatible with {typeof(ICollection<IOgmEntity>).FullName}. Set '{nameof(ignoreUnsupported)}' parameter in order to ignore them."); } if (!KnownTypes.ContainsKey(type)) { KnownTypes.Add(type, new KnownTypeDescriptor()); } KnownTypes[type].IgnoredProperties.AddRange(unsupported); }
private ParameterInfo ResolveParameter(CXType originalType, string name = null, int index = 0) { string renamed; var type = originalType; if (type.kind == CXTypeKind.CXType_FunctionProto) { throw new NotImplementedException(); } if (type.kind == CXTypeKind.CXType_FunctionNoProto) { throw new NotImplementedException(); } var typeKind = CanonizeType(ref type, out var typeDeclCursor); if (typeKind == CXTypeKind.CXType_Pointer) { var pointeeType = clang.getPointeeType(type); if (clang.getFunctionTypeCallingConv(pointeeType) != CXCallingConv.CXCallingConv_Invalid) { var delegateTypeName = originalType.ToString(); var possibleDelegateType = Module.GetType(delegateTypeName); if (possibleDelegateType != null) { return(new ParameterInfo(name, possibleDelegateType, index)); } return(new ParameterInfo(name, IncompleteTypeReference.Get(Module, null, delegateTypeName), index)); } var resolvedParameter = ResolveParameter(pointeeType); return(new ParameterInfo(name, resolvedParameter.Type.MakePointerType(), index)); } if (typeKind == CXTypeKind.CXType_DependentSizedArray) { throw new NotImplementedException(); } if (typeKind == CXTypeKind.CXType_ConstantArray) { var arraySize = (int)clang.getArraySize(type); var elementType = clang.getArrayElementType(type); var resolvedParameter = ResolveParameter(elementType, name); var clrElementType = resolvedParameter.Type; if (clrElementType.IsPointer) { clrElementType = Module.TypeSystem.IntPtr; } var arrayType = resolvedParameter.Type.MakeArrayType(); if (!PrimitiveUnmanagedTypeMap.TryGetValue(clrElementType.GetRuntimeType(), out var unmanagedType)) { throw new NotImplementedException(); } return(new ParameterInfo(name, arrayType, index, ParameterAttributes.None, arraySize)); } if (PrimitiveTypeMap.TryGetValue(typeKind, out var primitiveType)) { if (primitiveType == null) { throw new NotImplementedException(); } var originalTypeName = originalType.ToString(); var typeName = originalTypeName; if (TypeRedirects.TryGetValue(originalTypeName, out renamed)) { typeName = renamed; } if (originalType.kind == CXTypeKind.CXType_Typedef) { if (KnownTypes.ContainsKey(typeName)) { var knownType = Module.GetType(typeName) ?? Module.GetType(originalTypeName) ?? throw new NotImplementedException(); return(new ParameterInfo(name, knownType, index)); } } else { var found = Module.GetType(typeName); if (found != null) { return(new ParameterInfo(name, found, index)); } } return(new ParameterInfo(name, primitiveType.Import(Module), index)); } var typeDeclName = typeDeclCursor.ToString(); if (TypeRedirects.TryGetValue(typeDeclName, out renamed)) { typeDeclName = renamed; } var possibleType = Module.GetType(typeDeclName); if (possibleType != null) { return(new ParameterInfo(name, possibleType, index)); } return(new ParameterInfo(name, IncompleteTypeReference.Get(Module, null, typeDeclName), index)); }
public static bool IsPrimitiveType(SingleType st) { return(st.Package == null && KnownTypes.ContainsKey(st.rostype) && st.rostype != "duration" && st.rostype != "time"); }