Skip to content

Commit

Permalink
enable nullable context
Browse files Browse the repository at this point in the history
  • Loading branch information
chtenb committed Jan 29, 2024
1 parent 7eae256 commit 4e3cad9
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 143 deletions.
9 changes: 5 additions & 4 deletions Rubjerg.Graphviz.Test/TestInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ public void TestMarshaling()
public void TestOutputEncoding()
{
var root = Utils.CreateUniqueTestGraph();
var nodeA = root.GetOrAddNode("©");
var nodeA = root.GetOrAddNode("A");
nodeA.SetAttribute("label", "");
var dotStr = root.ToDotString();
Assert.IsTrue(dotStr.Contains("©"));
var svgStr = root.ToSvgString();
Assert.IsTrue(svgStr.Contains("©"));
Assert.IsTrue(dotStr.Contains(""));
//var svgStr = root.ToSvgString();
//Assert.IsTrue(svgStr.Contains("©"));
// FIXNOW: test with files
}
}
26 changes: 13 additions & 13 deletions Rubjerg.Graphviz/CGraphThing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public abstract class CGraphThing : GraphvizThing
/// Argument root may be null.
/// In that case, it is assumed this is a RootGraph, and MyRootGraph is set to `this`.
/// </summary>
internal CGraphThing(IntPtr ptr, RootGraph root) : base(ptr)
internal CGraphThing(IntPtr ptr, RootGraph? root) : base(ptr)
{
if (root == null)
if (root is null)
MyRootGraph = (RootGraph)this;
else
MyRootGraph = root;
}

protected static string NameString(string name)
protected static string? NameString(string? name)
{
// Because graphviz does not properly export empty strings to dot, this opens a can of worms.
// So we disallow it, and map it onto null.
Expand All @@ -40,7 +40,7 @@ protected static string NameString(string name)
/// Identifier for this object. Used to distinghuish multi edges.
/// Edges can be nameless, and in that case this method returns null.
/// </summary>
public string GetName()
public string? GetName()
{
return NameString(Rjagnameof(_ptr));
}
Expand All @@ -53,7 +53,7 @@ public bool HasAttribute(string name)
/// <summary>
/// Set attribute, and introduce it with the given default if it is not introduced yet.
/// </summary>
public void SafeSetAttribute(string name, string value, string deflt)
public void SafeSetAttribute(string name, string? value, string? deflt)
{
_ = deflt ?? throw new ArgumentNullException(nameof(deflt));
Agsafeset(_ptr, name, value, deflt);
Expand All @@ -62,7 +62,7 @@ public void SafeSetAttribute(string name, string value, string deflt)
/// <summary>
/// Set attribute, and introduce it with the empty string if it does not exist yet.
/// </summary>
public void SetAttribute(string name, string value)
public void SetAttribute(string name, string? value)
{
Agsafeset(_ptr, name, value, "");
}
Expand All @@ -71,18 +71,18 @@ public void SetAttribute(string name, string value)
/// Get the attribute value for this object, or the default value of the attribute if no explicit value was set.
/// If the attribute was not introduced, return null.
/// </summary>
public string GetAttribute(string name)
public string? GetAttribute(string name)
{
return Agget(_ptr, name);
}

/// <summary>
/// Get the attribute if it was introduced and contains a non-empty value, otherwise return deflt.
/// </summary>
public string SafeGetAttribute(string name, string deflt)
public string? SafeGetAttribute(string name, string? deflt)
{
if (HasAttribute(name))
return GetAttribute(name);
return GetAttribute(name)!;
return deflt;
}

Expand All @@ -103,13 +103,13 @@ public Dictionary<string, string> GetAttributes()
IntPtr sym = Agnxtattr(MyRootGraph._ptr, kind, IntPtr.Zero);
while (sym != IntPtr.Zero)
{
string key = ImsymKey(sym);
if (HasAttribute(key))
string? key = ImsymKey(sym);
if (key is not null && HasAttribute(key))
{
string value = GetAttribute(key);
string? value = GetAttribute(key);
if (!string.IsNullOrEmpty(value))
{
attributes[key] = value;
attributes[key] = value!;
}
}
sym = Agnxtattr(MyRootGraph._ptr, kind, sym);
Expand Down
10 changes: 5 additions & 5 deletions Rubjerg.Graphviz/Edge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Edge : CGraphThing
/// </summary>
internal Edge(IntPtr ptr, RootGraph rootgraph) : base(ptr, rootgraph) { }

internal static Edge Get(Graph graph, Node tail, Node head, string name)
internal static Edge? Get(Graph graph, Node tail, Node head, string? name)
{
name = NameString(name);
IntPtr ptr = Agedge(graph._ptr, tail._ptr, head._ptr, name, 0);
Expand All @@ -21,7 +21,7 @@ internal static Edge Get(Graph graph, Node tail, Node head, string name)
return new Edge(ptr, graph.MyRootGraph);
}

internal static Edge GetOrCreate(Graph graph, Node tail, Node head, string name)
internal static Edge GetOrCreate(Graph graph, Node tail, Node head, string? name)
{
name = NameString(name);
IntPtr ptr = Agedge(graph._ptr, tail._ptr, head._ptr, name, 1);
Expand Down Expand Up @@ -92,7 +92,7 @@ public void SetLogicalTail(SubGraph ltail)
throw new InvalidOperationException("ltail must be a cluster");
if (!MyRootGraph.IsCompound())
throw new InvalidOperationException("rootgraph must be compound for lheads/ltails to be used");
string ltailname = ltail.GetName();
string? ltailname = ltail.GetName();
SetAttribute("ltail", ltailname);
}

Expand All @@ -106,7 +106,7 @@ public void SetLogicalHead(SubGraph lhead)
throw new InvalidOperationException("ltail must be a cluster");
if (!MyRootGraph.IsCompound())
throw new InvalidOperationException("rootgraph must be compound for lheads/ltails to be used");
string lheadname = lhead.GetName();
string? lheadname = lhead.GetName();
SetAttribute("lhead", lheadname);
}

Expand All @@ -128,7 +128,7 @@ public static string ConvertUidToPortName(string id)

// Because there are two valid pointers to each edge, we have to override the default equals behaviour
// which simply compares the wrapped pointers.
public override bool Equals(GraphvizThing obj)
public override bool Equals(GraphvizThing? obj)
{
if (obj is Edge)
return Ageqedge(_ptr, obj._ptr);
Expand Down
36 changes: 18 additions & 18 deletions Rubjerg.Graphviz/ForeignFunctionInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public static int GvFreeLayout(IntPtr gvc, IntPtr graph)
return gvFreeLayout(gvc, graph);
}
}
public static int GvRender(IntPtr gvc, IntPtr graph, string format, IntPtr @out)
public static int GvRender(IntPtr gvc, IntPtr graph, string? format, IntPtr @out)
{
lock (_mutex)
{
return MarshalToUtf8(format, formatPtr => gvRender(gvc, graph, formatPtr, @out));
}
}
public static int GvRenderFilename(IntPtr gvc, IntPtr graph, string format, string filename)
public static int GvRenderFilename(IntPtr gvc, IntPtr graph, string? format, string? filename)
{
lock (_mutex)
{
Expand All @@ -58,7 +58,7 @@ public static int GvRenderFilename(IntPtr gvc, IntPtr graph, string format, stri
gvRenderFilename(gvc, graph, formatPtr, filenamePtr)));
}
}
public static IntPtr Agnode(IntPtr graph, string name, int create)
public static IntPtr Agnode(IntPtr graph, string? name, int create)
{
lock (_mutex)
{
Expand Down Expand Up @@ -159,7 +159,7 @@ public static void AgsetHtml(IntPtr obj, string name, string value)
}
}

public static void Agsafeset(IntPtr obj, string name, string val, string deflt)
public static void Agsafeset(IntPtr obj, string name, string? val, string? deflt)
{
lock (_mutex)
{
Expand All @@ -169,7 +169,7 @@ public static void Agsafeset(IntPtr obj, string name, string val, string deflt)
agsafeset(obj, namePtr, valPtr, defltPtr))));
}
}
public static void AgsafesetHtml(IntPtr obj, string name, string val, string deflt)
public static void AgsafesetHtml(IntPtr obj, string name, string? val, string? deflt)
{
lock (_mutex)
{
Expand Down Expand Up @@ -224,7 +224,7 @@ public static IntPtr Aghead(IntPtr node)
return rj_aghead(node);
}
}
public static IntPtr Agedge(IntPtr graph, IntPtr tail, IntPtr head, string name, int create)
public static IntPtr Agedge(IntPtr graph, IntPtr tail, IntPtr head, string? name, int create)
{
lock (_mutex)
{
Expand Down Expand Up @@ -287,7 +287,7 @@ public static int Agcontains(IntPtr graph, IntPtr obj)
return agcontains(graph, obj);
}
}
public static IntPtr Agsubg(IntPtr graph, string name, int create)
public static IntPtr Agsubg(IntPtr graph, string? name, int create)
{
lock (_mutex)
{
Expand Down Expand Up @@ -350,7 +350,7 @@ public static IntPtr EdgeLabel(IntPtr node)
return edge_label(node);
}
}
public static string Rjagmemwrite(IntPtr graph)
public static string? Rjagmemwrite(IntPtr graph)
{
lock (_mutex)
{
Expand All @@ -365,14 +365,14 @@ public static IntPtr GraphLabel(IntPtr node)
return graph_label(node);
}
}
public static string Agget(IntPtr obj, string name)
public static string? Agget(IntPtr obj, string name)
{
lock (_mutex)
{
return MarshalToUtf8(name, namePtr => MarshalFromUtf8(agget(obj, namePtr), false));
}
}
public static string Rjagnameof(IntPtr obj)
public static string? Rjagnameof(IntPtr obj)
{
lock (_mutex)
{
Expand All @@ -386,7 +386,7 @@ public static void CloneAttributeDeclarations(IntPtr graphfrom, IntPtr graphto)
clone_attribute_declarations(graphfrom, graphto);
}
}
public static string ImsymKey(IntPtr sym)
public static string? ImsymKey(IntPtr sym)
{
lock (_mutex)
{
Expand Down Expand Up @@ -421,7 +421,7 @@ public static double LabelHeight(IntPtr label)
return label_height(label);
}
}
public static string LabelText(IntPtr label)
public static string? LabelText(IntPtr label)
{
lock (_mutex)
{
Expand All @@ -435,7 +435,7 @@ public static double LabelFontsize(IntPtr label)
return label_fontsize(label);
}
}
public static string LabelFontname(IntPtr label)
public static string? LabelFontname(IntPtr label)
{
lock (_mutex)
{
Expand Down Expand Up @@ -491,7 +491,7 @@ public static IntPtr Rjagmemread(string input)
return MarshalToUtf8(input, rj_agmemread);
}
}
public static IntPtr Rjagopen(string name, int graphtype)
public static IntPtr Rjagopen(string? name, int graphtype)
{
lock (_mutex)
{
Expand Down Expand Up @@ -709,7 +709,7 @@ public enum TestEnum
[DllImport("GraphvizWrapper.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr return_copyright();

public static string EchoString(string str)
public static string? EchoString(string? str)
{
return MarshalToUtf8(str, ptr =>
{
Expand All @@ -718,8 +718,8 @@ public static string EchoString(string str)
return MarshalFromUtf8(returnPtr, true);
});
}
public static string ReturnEmptyString() => MarshalFromUtf8(return_empty_string(), false);
public static string ReturnHello() => MarshalFromUtf8(return_hello(), false);
public static string ReturnCopyRight() => MarshalFromUtf8(return_copyright(), false);
public static string? ReturnEmptyString() => MarshalFromUtf8(return_empty_string(), false);
public static string? ReturnHello() => MarshalFromUtf8(return_hello(), false);
public static string? ReturnCopyRight() => MarshalFromUtf8(return_copyright(), false);
#endregion
}
Loading

0 comments on commit 4e3cad9

Please sign in to comment.