Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Add Ignore property to MapAttribute, emit structure definitions in correct order #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions create-native-map/src/MapAttribute.cs
Expand Up @@ -38,6 +38,7 @@
internal class MapAttribute : Attribute {
private string nativeType;
private string suppressFlags;
private bool ignore;

public MapAttribute ()
{
Expand All @@ -56,5 +57,10 @@ public MapAttribute (string nativeType)
get {return suppressFlags;}
set {suppressFlags = value;}
}

public bool Ignore {
get {return ignore;}
set {ignore = value;}
}
}

46 changes: 39 additions & 7 deletions create-native-map/src/create-native-map.cs
Expand Up @@ -348,6 +348,7 @@ public static MapAttribute GetMapAttribute (ICustomAttributeProvider element)
? new MapAttribute ()
: new MapAttribute (nativeType);
map.SuppressFlags = GetPropertyValueAsString (o, "SuppressFlags");
map.Ignore = GetPropertyValueAsBool (o, "Ignore");
return map;
}
return null;
Expand All @@ -374,6 +375,12 @@ private static string GetPropertyValueAsString (object o, string property)
return null;
}

private static bool GetPropertyValueAsBool (object o, string property)
{
object v = GetPropertyValue (o, property);
return v == null ? false : (bool) v;
}

private static object GetPropertyValue (object o, string property)
{
PropertyInfo p = o.GetType().GetProperty (property);
Expand Down Expand Up @@ -484,6 +491,14 @@ public static string GetNativeType (FieldInfo field)
return null;
}

public static bool IsIgnored (FieldInfo field)
{
MapAttribute map = GetMapAttribute (field);
if (map != null)
return map.Ignore;
return false;
}

public static string GetFunctionDeclaration (string name, MethodInfo method)
{
StringBuilder sb = new StringBuilder ();
Expand Down Expand Up @@ -663,10 +678,11 @@ protected string GetNativeMemberName (FieldInfo field)
class HeaderFileGenerator : FileGenerator {
StreamWriter sh;
string assembly_file;
Dictionary<string, MethodInfo> methods = new Dictionary <string, MethodInfo> ();
Dictionary<string, Type> structs = new Dictionary <string, Type> ();
Dictionary<string, MethodInfo> delegates = new Dictionary <string, MethodInfo> ();
List<string> decls = new List <string> ();
Dictionary<string, MethodInfo> methods = new Dictionary <string, MethodInfo> ();
Dictionary<string, Type> structs = new Dictionary <string, Type> ();
Dictionary<string, MethodInfo> delegates = new Dictionary <string, MethodInfo> ();
List<string> decls = new List <string> ();
Dictionary<string, object> writtenStructs = new Dictionary <string, object> ();

public override void CreateFile (string assembly_name, string file_prefix)
{
Expand Down Expand Up @@ -894,17 +910,31 @@ private static IEnumerable<string> Sort (ICollection<string> c)

private void WriteStructDeclarations (string s)
{
if (writtenStructs.ContainsKey (s)) {
return;
}
writtenStructs.Add (s, null);
Type t = structs [s];
#if false
if (!t.Assembly.CodeBase.EndsWith (this.assembly_file)) {
return;
}
#endif
sh.WriteLine ("struct {0} {{", MapUtils.GetManagedType (t));
FieldInfo[] fields = t.GetFields (BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic);
// make sure all structures used as fields are written before this structure
foreach (FieldInfo field in fields) {
if (MapUtils.IsIgnored (field))
continue;
string managed_type = field.FieldType.FullName;
if (structs.ContainsKey (managed_type))
WriteStructDeclarations (managed_type);
}
sh.WriteLine ("struct {0} {{", MapUtils.GetManagedType (t));
int max_type_len = 0, max_name_len = 0, max_native_len = 0;
Array.ForEach (fields, delegate (FieldInfo f) {
if (MapUtils.IsIgnored (f))
return;
max_type_len = Math.Max (max_type_len, HeaderFileGenerator.GetType (f.FieldType).Length);
max_name_len = Math.Max (max_name_len, GetNativeMemberName (f).Length);
string native_type = MapUtils.GetNativeType (f);
Expand All @@ -913,6 +943,8 @@ private void WriteStructDeclarations (string s)
});
SortFieldsInOffsetOrder (t, fields);
foreach (FieldInfo field in fields) {
if (MapUtils.IsIgnored (field))
continue;
string fname = GetNativeMemberName (field);
sh.Write ("\t{0,-" + max_type_len + "} {1};",
GetType (field.FieldType), fname);
Expand Down Expand Up @@ -1410,11 +1442,11 @@ private static FieldInfo[] GetFieldsToCopy (Type t)
BindingFlags.Public | BindingFlags.NonPublic);
int count = 0;
for (int i = 0; i < fields.Length; ++i)
if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null)
if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null && !MapUtils.IsIgnored (fields [i]))
++count;
FieldInfo[] rf = new FieldInfo [count];
for (int i = 0, j = 0; i < fields.Length; ++i) {
if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null)
if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null && !MapUtils.IsIgnored (fields [i]))
rf [j++] = fields [i];
}
return rf;
Expand Down