Last active
January 4, 2024 08:26
-
-
Save Windows10CE/ac54184f87c05049208fdb8658fd27a9 to your computer and use it in GitHub Desktop.
C# iostream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Diagnostics; | |
int a = default; | |
string s = default; | |
_ = Std.In >> __makeref(a) >> __makeref(s); | |
_ = Std.Out << a << s << "\n"; | |
public static unsafe class Std | |
{ | |
public sealed class OutImpl | |
{ | |
public static OutImpl Singleton { get; } = new(); | |
private OutImpl() { } | |
public static OutImpl operator <<(OutImpl self, object o) | |
{ | |
if (object.ReferenceEquals(o, EndL)) o = "\n"; | |
Console.WriteLine(o); | |
return self; | |
} | |
} | |
public sealed class InImpl | |
{ | |
public static InImpl Singleton { get; } = new(); | |
private InImpl() { } | |
private static readonly MethodInfo _parseMethod = typeof(InImpl).GetMethod(nameof(SetToParsed), BindingFlags.Static | BindingFlags.NonPublic)!; | |
public static InImpl operator >>(InImpl self, TypedReference t) | |
{ | |
var line = Console.ReadLine() ?? throw new UnreachableException(); | |
Type type = __reftype(t); | |
if (type.IsAssignableTo(typeof(IParsable<>).MakeGenericType(type))) | |
{ | |
_parseMethod.MakeGenericMethod(type).Invoke(null, [Pointer.Box(&t, typeof(TypedReference*)), line]); | |
return self; | |
} | |
throw new NotSupportedException(); | |
} | |
private static void SetToParsed<T>(TypedReference* t, string toParse) where T : IParsable<T> | |
{ | |
__refvalue(*t, T) = T.Parse(toParse, null); | |
} | |
} | |
public static OutImpl Out => OutImpl.Singleton; | |
public static object EndL { get; } = new(); | |
public static InImpl In => InImpl.Singleton; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment