Skip to content

Commit

Permalink
Make Value type support in serializer more generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Apr 14, 2024
1 parent 5c6f6b6 commit e9edf8c
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 205 deletions.
76 changes: 53 additions & 23 deletions BTDB.SourceGenerator.Sample/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,67 @@
using BTDB.Serialization;
using Microsoft.AspNetCore.Http;

var builder = new ContainerBuilder();
//builder.RegisterType<Klass>();
builder.AutoRegisterTypes().AsSelf();
var container = builder.Build();
container.Resolve<Klass>();
unsafe
{
var h = IAnyHandler.CreateConsumeDispatcher(container);
h(container, "Hello");
}
var builder = new ContainerBuilder();
//builder.RegisterType<Klass>();
builder.AutoRegisterTypes().AsSelf();
var container = builder.Build();
container.Resolve<Klass>();
unsafe
{
var h = IAnyHandler.CreateConsumeDispatcher(container);
h(container, "Hello");
}

unsafe
{
ReflectionMetadata.RegisterCollection(new()
unsafe
{
Type = typeof(Dictionary<int, string>),
ElementKeyType = typeof(int),
ElementValueType = typeof(string),
Creator = &Create1,
AdderKeyValue = &Add1
});
}
ReflectionMetadata.RegisterCollection(new()
{
Type = typeof(Dictionary<int, string>),
ElementKeyType = typeof(int),
ElementValueType = typeof(string),
Creator = &Create1,
AdderKeyValue = &Add1
});
}

static object Create1(uint capacity)
{
return new Dictionary<int, string>((int)capacity);
MyCtx myCtx = default;
AllocateValueType(ref Unsafe.As<MyCtx, byte>(ref myCtx), ref myCtx.Ptr, &WorkWithAllocatedValueType);

static object Create1(uint capacity)
{
return new Dictionary<int, string>((int)capacity);
}

static void Add1(object c, ref byte key, ref byte value)
{
Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
}

static void AllocateValueType(ref byte ctx, ref nint ptr, delegate*<ref byte, void> chain)
{
(int, string) value;
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
ptr = (nint)(&value);
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
chain(ref ctx);
ptr = 0;
}

static void WorkWithAllocatedValueType(ref byte ctx)
{
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
ref var value = ref *((int, string)*)Unsafe.As<byte, MyCtx>(ref ctx).Ptr;
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
value = (42, "Hello");
Console.WriteLine(value);
}
}

static void Add1(object c, ref byte key, ref byte value)
public struct MyCtx
{
Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
public nint Ptr;
}

[Generate]
Expand Down

0 comments on commit e9edf8c

Please sign in to comment.