using System;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Capnp.Rpc
{
///
/// Provides functionality to construct Proxy and Skeleton instances from capability interfaces and objects implementing capability interfaces.
/// A capability interface is any .NET interface which is annotated with and .
/// There are some intricacies to consider that you usually don't need to care about, since all that stuff will be generated.
///
public static class CapabilityReflection
{
interface IBrokenFactory
{
System.Exception Exception { get; }
}
abstract class ProxyFactory
{
public abstract Proxy NewProxy();
}
class ProxyFactory: ProxyFactory where T: Proxy, new()
{
public override Proxy NewProxy() => new T();
}
class BrokenProxyFactory: ProxyFactory, IBrokenFactory
{
readonly System.Exception _exception;
public BrokenProxyFactory(System.Exception exception)
{
_exception = exception;
}
public System.Exception Exception => _exception;
public override Proxy NewProxy()
{
throw _exception;
}
}
abstract class SkeletonFactory
{
public abstract Skeleton NewSkeleton();
}
class SkeletonFactory: SkeletonFactory where T: Skeleton, new()
{
public override Skeleton NewSkeleton() => new T();
}
class BrokenSkeletonFactory: SkeletonFactory, IBrokenFactory
{
System.Exception _exception;
public BrokenSkeletonFactory(System.Exception exception)
{
_exception = exception;
}
public System.Exception Exception => _exception;
public override Skeleton NewSkeleton()
{
throw _exception;
}
}
class PolySkeletonFactory: SkeletonFactory
{
readonly SkeletonFactory[] _monoFactories;
public PolySkeletonFactory(SkeletonFactory[] monoFactories)
{
_monoFactories = monoFactories;
}
public override Skeleton NewSkeleton()
{
var poly = new PolySkeleton();
foreach (var fac in _monoFactories)
{
poly.AddInterface(fac.NewSkeleton());
}
return poly;
}
}
static ConditionalWeakTable _proxyMap =
new ConditionalWeakTable();
static ConditionalWeakTable _skeletonMap =
new ConditionalWeakTable();
static ConditionalWeakTable