using System;
using System.Collections.Generic;
namespace Capnp
{
///
/// Entry point for building Cap'n Proto messages.
///
public class MessageBuilder
{
readonly ISegmentAllocator _allocator;
readonly DynamicSerializerState _rootPtrBuilder;
List? _capTable;
MessageBuilder(ISegmentAllocator allocator)
{
_allocator = allocator;
_rootPtrBuilder = new DynamicSerializerState(this);
_rootPtrBuilder.SetStruct(0, 1);
_rootPtrBuilder.Allocate();
}
///
/// Constructs an instance using a custom segment allocator and reserves space for the root pointer.
///
/// Segment allocator implementation type
public static MessageBuilder Create() where T: ISegmentAllocator, new()
{
return new MessageBuilder(new T());
}
///
/// Constructs an instance using the default segment allocator and reserves space for the root pointer.
///
/// Default segment size,
public static MessageBuilder Create(int defaultSegmentSize = 128)
{
return new MessageBuilder(new SegmentAllocator(defaultSegmentSize));
}
///
/// Creates a new object inside the message.
///
/// Serializer state specialization
/// Serializer state instance representing the new object
public TS CreateObject() where TS: SerializerState, new()
{
var ts = new TS();
ts.Bind(this);
return ts;
}
///
/// Gets or sets the root object. The root object must be set exactly once per message.
/// Setting it manually is only required (and allowed) when it was created with .
///
/// Attempt to set null reference
public SerializerState? Root
{
get => _rootPtrBuilder.TryGetPointer(0);
set => _rootPtrBuilder.Link(0, value ?? throw new ArgumentNullException(nameof(value)));
}
///
/// Creates an object and sets it as root object.
///
/// Serializer state specialization (must be a struct)
/// Serializer state instance representing the new object
public TS BuildRoot() where TS: SerializerState, new()
{
if (Root != null)
throw new InvalidOperationException("Root already set");
var root = CreateObject();
if (root.Kind != ObjectKind.Struct)
throw new InvalidOperationException("Root object must be a struct");
Root = root;
return root;
}
///
/// Returns the wire representation of the built message.
///
public WireFrame Frame => new WireFrame(_allocator.Segments);
///
/// Initializes the capability table for using the message builder in RPC context.
///
public void InitCapTable()
{
if (_capTable != null)
throw new InvalidOperationException("Capability table was already initialized");
_capTable = new List();
}
///
/// Returns this message builder's segment allocator.
///
public ISegmentAllocator Allocator => _allocator;
internal List? Caps => _capTable;
}
}