using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Capnp
{
///
/// SerializerState specialization for a list of capabilities.
///
/// Capability interface
public class ListOfCapsSerializer :
SerializerState,
IReadOnlyList
where T : class
{
///
/// Constructs an instance.
///
/// does not quality as capability interface.
/// The implementation might attempt to throw this exception earlier in the static constructor (during type load). Currently it doesn't
/// because there is a significant risk of messing something up and ending with a hard-to-debug .
public ListOfCapsSerializer()
{
Rpc.CapabilityReflection.ValidateCapabilityInterface(typeof(T));
}
///
/// Gets or sets the capability at given element index.
///
/// Element index
/// Proxy object of capability at given element index
/// List was not initialized, or attempting to overwrite an already set element.
/// is out of range.
public T this[int index]
{
get => Rpc.CapabilityReflection.CreateProxy(DecodeCapPointer(index)) as T;
set
{
if (!IsAllocated)
throw new InvalidOperationException("Call Init() first");
if (index < 0 || index >= RawData.Length)
throw new IndexOutOfRangeException("index out of range");
uint id = ProvideCapability(value);
WirePointer ptr = default;
ptr.SetCapability(id);
RawData[index] = id;
}
}
///
/// Initializes this list with a specific size. The list can be initialized only once.
///
/// List element count
/// The list was already initialized
/// is negative or greater than 2^29-1
public void Init(int count)
{
SetListOfPointers(count);
}
///
/// Initializes the list with given content.
///
/// List content. Can be null in which case the list is simply not initialized.
/// The list was already initialized
/// More than 2^29-1 items.
public void Init(IReadOnlyList caps)
{
if (caps == null)
{
return;
}
Init(caps.Count);
for (int i = 0; i < caps.Count; i++)
{
this[i] = caps[i];
}
}
///
/// This list's element count.
///
public int Count => ListElementCount;
IEnumerable Enumerate()
{
int count = Count;
for (int i = 0; i < count; i++)
yield return this[i];
}
///
/// Implements
///
///
public IEnumerator GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}