//This file is automatically generated. DO NOT EDIT!
using System;
using RobotRaconteur;
using System.Collections.Generic;
using System.Threading.Tasks;

#pragma warning disable 0108

namespace com.robotraconteur.testing.TestService2
{
public class com__robotraconteur__testing__TestService2Factory : ServiceFactory
{
    public override string DefString()
{
    const string s="service com.robotraconteur.testing.TestService2\n\noption version 0.8\n\nexception testexception3\n\nstruct ostruct2\nfield double[] a1\nend struct\n\n\nobject baseobj\nproperty double d1\nproperty double[] d2\n\nfunction double func3(double d1, double d2)\n\nevent ev1()\n\nobjref subobj o5\n\npipe double[] p1\n\ncallback void cb2(double d1, double d2)\n\nwire double[] w1\n\nmemory double[] m1\n\n\nend object\n\nobject subobj\n\nfunction double add_val(double v)\n\nend object\n\n";
    return s;
    }
    public override string GetServiceName() {return "com.robotraconteur.testing.TestService2";}
    public ostruct2_stub ostruct2_stubentry;
    public com__robotraconteur__testing__TestService2Factory()
{
    ostruct2_stubentry=new ostruct2_stub(this);
    }
    public override IStructureStub FindStructureStub(string objecttype)
    {
    if (objecttype=="ostruct2")    return ostruct2_stubentry;
    throw new DataTypeException("Cannot find appropriate structure stub");
    }
    public override IPodStub FindPodStub(string objecttype)
    {
    throw new DataTypeException("Cannot find appropriate pod stub");
    }
    public override INamedArrayStub FindNamedArrayStub(string objecttype)
    {
    throw new DataTypeException("Cannot find appropriate pod stub");
    }
    public override ServiceStub CreateStub(WrappedServiceStub innerstub) {
    string objecttype=innerstub.RR_objecttype.GetServiceDefinition().Name + "." + innerstub.RR_objecttype.Name;    string objshort;
    if (CompareNamespace(objecttype, out objshort)) {
    switch (objshort) {
    case "baseobj":
    return new baseobj_stub(innerstub);
    case "subobj":
    return new subobj_stub(innerstub);
    default:
    break;
    }
    } else {
    string ext_service_type=RobotRaconteurNode.SplitQualifiedName(objecttype).Item1;
    return RobotRaconteurNode.s.GetServiceType(ext_service_type).CreateStub(innerstub);
    }
    throw new ServiceException("Could not create stub");
    }
    public override ServiceSkel CreateSkel(object obj) {
    string objtype = RobotRaconteurNode.GetTypeString(ServiceSkelUtil.FindParentInterface(obj.GetType()));
    string objshort;
    if (CompareNamespace(objtype, out objshort)) {
    switch(objshort) {
    case "baseobj":
    return new baseobj_skel((baseobj)obj);
    case "subobj":
    return new subobj_skel((subobj)obj);
    default:
    break;
    }
    } else {
    string ext_service_type=RobotRaconteurNode.SplitQualifiedName(objtype).Item1;
    return RobotRaconteurNode.s.GetServiceFactory(ext_service_type).CreateSkel(obj);
    }
    throw new ServiceException("Could not create skel");
    }
    public override RobotRaconteurException DownCastException(RobotRaconteurException rr_exp){
    if (rr_exp==null) return rr_exp;
    string rr_type=rr_exp.Error;
    if (!rr_type.Contains(".")) return rr_exp;
    string rr_stype;
    if (CompareNamespace(rr_type, out rr_stype)) {
    if (rr_stype=="testexception3") return new testexception3(rr_exp.Message,rr_exp.ErrorSubName,rr_exp.ErrorParam);
    } else {
    return RobotRaconteurNode.s.DownCastException(rr_exp); 
    }
    return rr_exp;
    }
}

public class ostruct2_stub : IStructureStub {
    public ostruct2_stub(com__robotraconteur__testing__TestService2Factory d) {def=d;}
    private com__robotraconteur__testing__TestService2Factory def;
    public MessageElementNestedElementList PackStructure(object s1) {
    using(vectorptr_messageelement m=new vectorptr_messageelement())
    {
    if (s1 ==null) return null;
    ostruct2 s = (ostruct2)s1;
    MessageElementUtil.AddMessageElementDispose(m,MessageElementUtil.PackArray<double>("a1",s.a1));
    return new MessageElementNestedElementList(DataTypes.structure_t,"com.robotraconteur.testing.TestService2.ostruct2",m);
    }
    }
    public T UnpackStructure<T>(MessageElementNestedElementList m) {
    if (m == null ) return default(T);
    ostruct2 s=new ostruct2();
    using(vectorptr_messageelement mm=m.Elements)
    {
    s.a1 =MessageElementUtil.UnpackArray<double>(MessageElement.FindElement(mm,"a1"));
    T st; try {st=(T)((object)s);} catch (InvalidCastException) {throw new DataTypeMismatchException("Wrong structuretype");}
    return st;
    }
    }
}

public interface async_baseobj
{
    Task<double> async_get_d1(int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
    Task async_set_d1(double value, int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
    Task<double[]> async_get_d2(int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
    Task async_set_d2(double[] value, int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
    Task<double> async_func3(double d1, double d2,int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
    Task<subobj> async_get_o5(int timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
}
public class baseobj_stub : ServiceStub , baseobj, async_baseobj{
    private CallbackClient<Action<double, double>> rr_cb2;
    private Pipe<double[]> rr_p1;
    private Wire<double[]> rr_w1;
    private ArrayMemory<double> rr_m1;
    public baseobj_stub(WrappedServiceStub innerstub) : base(innerstub) {
    rr_cb2=new CallbackClient<Action<double, double>>("cb2");
    rr_p1=new Pipe<double[]>(innerstub.GetPipe("p1"));
    rr_w1=new Wire<double[]>(innerstub.GetWire("w1"));
    rr_m1=new ArrayMemoryClient<double>(innerstub.GetArrayMemory("m1"));
    }
    public double d1 {
    get {
    return (MessageElementUtil.UnpackScalar<double>(rr_innerstub.PropertyGet("d1")));
    }
    set {
    using(MessageElement m=MessageElementUtil.PackScalar<double>("value",value))
    {
    rr_innerstub.PropertySet("d1", m);
    }
    }
    }
    public double[] d2 {
    get {
    return MessageElementUtil.UnpackArray<double>(rr_innerstub.PropertyGet("d2"));
    }
    set {
    using(MessageElement m=MessageElementUtil.PackArray<double>("value",value))
    {
    rr_innerstub.PropertySet("d2", m);
    }
    }
    }
    public double func3(double d1, double d2) {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement())
    {
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d1",d1));
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d2",d2));
    using(MessageElement rr_me=rr_innerstub.FunctionCall("func3",rr_param))
    {
    return (MessageElementUtil.UnpackScalar<double>(rr_me));
    }
    }
    }
    public event Action ev1;
    public override void DispatchEvent(string rr_membername, vectorptr_messageelement rr_m) {
    switch (rr_membername) {
    case "ev1":
    {
    if (ev1 != null) { 
    ev1();
    }
    return;
    }
    default:
    break;
    }
    }
    public subobj get_o5() {
    return (subobj)FindObjRefTyped("o5","com.robotraconteur.testing.TestService2.subobj");
    }
    public Pipe<double[]> p1 {
    get { return rr_p1;  }
    set { throw new InvalidOperationException();}
    }
    public Callback<Action<double, double>> cb2 {
    get { return rr_cb2;  }
    set { throw new InvalidOperationException();}
    }
    public Wire<double[]> w1 {
    get { return rr_w1;  }
    set { throw new InvalidOperationException();}
    }
    public override MessageElement CallbackCall(string rr_membername, vectorptr_messageelement rr_m) {
    switch (rr_membername) {
    case "cb2":
    {
    double d1=(MessageElementUtil.UnpackScalar<double>(vectorptr_messageelement_util.FindElement(rr_m,"d1")));
    double d2=(MessageElementUtil.UnpackScalar<double>(vectorptr_messageelement_util.FindElement(rr_m,"d2")));
    this.cb2.Function(d1, d2);
    return new MessageElement("return",(int)0);
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public ArrayMemory<double> m1 { 
    get { return rr_m1; }
    }
    public virtual async Task<double> async_get_d1(int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(var rr_value = await rr_async_PropertyGet("d1",rr_timeout)) {
    var rr_ret=(MessageElementUtil.UnpackScalar<double>(rr_value));
    return rr_ret;
    } }
    public virtual async Task async_set_d1(double value, int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(MessageElement mm=MessageElementUtil.PackScalar<double>("value",value))
    {
    await rr_async_PropertySet("d1",mm,rr_timeout);
    }
    }
    public virtual async Task<double[]> async_get_d2(int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(var rr_value = await rr_async_PropertyGet("d2",rr_timeout)) {
    var rr_ret=MessageElementUtil.UnpackArray<double>(rr_value);
    return rr_ret;
    } }
    public virtual async Task async_set_d2(double[] value, int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(MessageElement mm=MessageElementUtil.PackArray<double>("value",value))
    {
    await rr_async_PropertySet("d2",mm,rr_timeout);
    }
    }
    public virtual async Task<double> async_func3(double d1, double d2,int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement())
    {
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d1",d1));
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d2",d2));
    using(var rr_return = await rr_async_FunctionCall("func3",rr_param,rr_timeout)) {
    var rr_ret=(MessageElementUtil.UnpackScalar<double>(rr_return));
    return rr_ret;
    } } }
    public Task<subobj> async_get_o5(int timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    return AsyncFindObjRefTyped<subobj>("o5","com.robotraconteur.testing.TestService2.subobj",timeout);
    }
}
public interface async_subobj
{
    Task<double> async_add_val(double v,int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE);
}
public class subobj_stub : ServiceStub , subobj, async_subobj{
    public subobj_stub(WrappedServiceStub innerstub) : base(innerstub) {
    }
    public double add_val(double v) {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement())
    {
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("v",v));
    using(MessageElement rr_me=rr_innerstub.FunctionCall("add_val",rr_param))
    {
    return (MessageElementUtil.UnpackScalar<double>(rr_me));
    }
    }
    }
    public override void DispatchEvent(string rr_membername, vectorptr_messageelement rr_m) {
    switch (rr_membername) {
    default:
    break;
    }
    }
    public override MessageElement CallbackCall(string rr_membername, vectorptr_messageelement rr_m) {
    switch (rr_membername) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public virtual async Task<double> async_add_val(double v,int rr_timeout=RobotRaconteurNode.RR_TIMEOUT_INFINITE)
    {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement())
    {
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("v",v));
    using(var rr_return = await rr_async_FunctionCall("add_val",rr_param,rr_timeout)) {
    var rr_ret=(MessageElementUtil.UnpackScalar<double>(rr_return));
    return rr_ret;
    } } }
}
public class baseobj_skel : ServiceSkel {
    protected baseobj obj;
    protected async_baseobj async_obj;
    public baseobj_skel(object o) : base(o)    {
    obj=(baseobj)o;
    async_obj = o as async_baseobj;
    }
    public override void ReleaseCastObject() { 
    obj=null;
    async_obj=null;
    base.ReleaseCastObject();
    }
    public override MessageElement CallGetProperty(string membername, WrappedServiceSkelAsyncAdapter async_adapter) {
    switch (membername) {
    case "d1":
    {
    if (async_obj!=null)    {
    async_adapter.MakeAsync();
    async_obj.async_get_d1().ContinueWith(t => async_adapter.EndTask<double>(t,async_ret => MessageElementUtil.PackScalar<double>("return",async_ret)));
    return null;
    }
    double ret=obj.d1;
    return MessageElementUtil.PackScalar<double>("return",ret);
    }
    case "d2":
    {
    if (async_obj!=null)    {
    async_adapter.MakeAsync();
    async_obj.async_get_d2().ContinueWith(t => async_adapter.EndTask<double[]>(t,async_ret => MessageElementUtil.PackArray<double>("return",async_ret)));
    return null;
    }
    double[] ret=obj.d2;
    return MessageElementUtil.PackArray<double>("return",ret);
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override void CallSetProperty(string membername, MessageElement m, WrappedServiceSkelAsyncAdapter async_adapter) {
    switch (membername) {
    case "d1":
    {
    if (async_obj!=null)    {
    async_adapter.MakeAsync();
    async_obj.async_set_d1((MessageElementUtil.UnpackScalar<double>(m))).ContinueWith(t => async_adapter.EndTask(t));
    return;
    }
    obj.d1=(MessageElementUtil.UnpackScalar<double>(m));
    return;
    }
    case "d2":
    {
    if (async_obj!=null)    {
    async_adapter.MakeAsync();
    async_obj.async_set_d2(MessageElementUtil.UnpackArray<double>(m)).ContinueWith(t => async_adapter.EndTask(t));
    return;
    }
    obj.d2=MessageElementUtil.UnpackArray<double>(m);
    return;
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override MessageElement CallFunction(string rr_membername, vectorptr_messageelement rr_m, WrappedServiceSkelAsyncAdapter rr_async_adapter) {
    switch (rr_membername) {
    case "func3":
    {
    double d1=(MessageElementUtil.UnpackScalar<double>(vectorptr_messageelement_util.FindElement(rr_m,"d1")));
    double d2=(MessageElementUtil.UnpackScalar<double>(vectorptr_messageelement_util.FindElement(rr_m,"d2")));
    if (async_obj!=null)    {
    rr_async_adapter.MakeAsync();
    async_obj.async_func3(d1, d2).ContinueWith(t => rr_async_adapter.EndTask<double>(t,async_ret => MessageElementUtil.PackScalar<double>("return",async_ret)));
    return null;
    }
    double rr_ret=this.obj.func3(d1, d2);
    return MessageElementUtil.PackScalar<double>("return",rr_ret);
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override object GetSubObj(string name, string ind) {
    switch (name) {
    case "o5": {
    return obj.get_o5();
    }
    default:
    break;
    }
    throw new MemberNotFoundException("");
    }
    public override void RegisterEvents(object rrobj1) {
    obj=(baseobj)rrobj1;
    obj.ev1+=rr_ev1;
    }
    public override void UnregisterEvents(object rrobj1) {
    obj=(baseobj)rrobj1;
    obj.ev1-=rr_ev1;
    }
    public void rr_ev1() {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement()) {
    this.innerskel.WrappedDispatchEvent("ev1",rr_param);
    }
    }
    public override object GetCallbackFunction(uint rr_endpoint, string rr_membername) {
    switch (rr_membername) {
    case "cb2": {
    return new Action<double, double>( delegate(double d1, double d2) {
    using(vectorptr_messageelement rr_param=new vectorptr_messageelement())
    {
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d1",d1));
    MessageElementUtil.AddMessageElementDispose(rr_param,MessageElementUtil.PackScalar<double>("d2",d2));
    using(MessageElement rr_me=this.innerskel.WrappedCallbackCall("cb2",rr_endpoint,rr_param))
    {
    }
    }
    });
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override void InitPipeServers(object rrobj1) {
    obj=(baseobj)rrobj1;
    obj.p1=new Pipe<double[]>(innerskel.GetPipe("p1"));
    }
    public override void InitCallbackServers(object rrobj1) {
    obj=(baseobj)rrobj1;
    obj.cb2=new CallbackServer<Action<double, double>>("cb2",this);
    }
    public override void InitWireServers(object rrobj1) {
    obj=(baseobj)rrobj1;
    obj.w1=new Wire<double[]>(innerskel.GetWire("w1"));
    }
    public override WrappedArrayMemoryDirector GetArrayMemory(string name) {
    switch (name) {
    case "m1": {
    WrappedArrayMemoryDirectorNET<double> dir=new  WrappedArrayMemoryDirectorNET<double>(obj.m1);
    return dir;
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedMultiDimArrayMemoryDirector GetMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedPodArrayMemoryDirector GetPodArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedPodMultiDimArrayMemoryDirector GetPodMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedNamedArrayMemoryDirector GetNamedArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedNamedMultiDimArrayMemoryDirector GetNamedMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override string RRType { get { return "com.robotraconteur.testing.TestService2.baseobj"; } }
}
public class subobj_skel : ServiceSkel {
    protected subobj obj;
    protected async_subobj async_obj;
    public subobj_skel(object o) : base(o)    {
    obj=(subobj)o;
    async_obj = o as async_subobj;
    }
    public override void ReleaseCastObject() { 
    obj=null;
    async_obj=null;
    base.ReleaseCastObject();
    }
    public override MessageElement CallGetProperty(string membername, WrappedServiceSkelAsyncAdapter async_adapter) {
    switch (membername) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override void CallSetProperty(string membername, MessageElement m, WrappedServiceSkelAsyncAdapter async_adapter) {
    switch (membername) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override MessageElement CallFunction(string rr_membername, vectorptr_messageelement rr_m, WrappedServiceSkelAsyncAdapter rr_async_adapter) {
    switch (rr_membername) {
    case "add_val":
    {
    double v=(MessageElementUtil.UnpackScalar<double>(vectorptr_messageelement_util.FindElement(rr_m,"v")));
    if (async_obj!=null)    {
    rr_async_adapter.MakeAsync();
    async_obj.async_add_val(v).ContinueWith(t => rr_async_adapter.EndTask<double>(t,async_ret => MessageElementUtil.PackScalar<double>("return",async_ret)));
    return null;
    }
    double rr_ret=this.obj.add_val(v);
    return MessageElementUtil.PackScalar<double>("return",rr_ret);
    }
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override object GetSubObj(string name, string ind) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("");
    }
    public override void RegisterEvents(object rrobj1) {
    obj=(subobj)rrobj1;
    }
    public override void UnregisterEvents(object rrobj1) {
    obj=(subobj)rrobj1;
    }
    public override object GetCallbackFunction(uint rr_endpoint, string rr_membername) {
    switch (rr_membername) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member not found");
    }
    public override void InitPipeServers(object rrobj1) {
    obj=(subobj)rrobj1;
    }
    public override void InitCallbackServers(object rrobj1) {
    obj=(subobj)rrobj1;
    }
    public override void InitWireServers(object rrobj1) {
    obj=(subobj)rrobj1;
    }
    public override WrappedArrayMemoryDirector GetArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedMultiDimArrayMemoryDirector GetMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedPodArrayMemoryDirector GetPodArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedPodMultiDimArrayMemoryDirector GetPodMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedNamedArrayMemoryDirector GetNamedArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override WrappedNamedMultiDimArrayMemoryDirector GetNamedMultiDimArrayMemory(string name) {
    switch (name) {
    default:
    break;
    }
    throw new MemberNotFoundException("Member Not Found");
    }
    public override string RRType { get { return "com.robotraconteur.testing.TestService2.subobj"; } }
}
public class baseobj_default_impl : baseobj{
    protected Callback<Action<double, double>> rrvar_cb2;
    public virtual double d1 {get; set;} = default(double);
    public virtual double[] d2 {get; set;} = new double[0];
    public virtual double func3(double d1, double d2) {
    throw new NotImplementedException();    }
    public virtual event Action ev1;
    protected virtual void rrfire_ev1() {
    ev1?.Invoke();
    }
    public virtual subobj get_o5() {
    throw new NotImplementedException();
    }
    public virtual Pipe<double[]> p1 {
    get { throw new NotImplementedException(); }
    set { throw new InvalidOperationException();}
    }
    public virtual Callback<Action<double, double>> cb2 {
    get { return rrvar_cb2;  }
    set {
    if (rrvar_cb2!=null) throw new InvalidOperationException("Callback already set");
    rrvar_cb2= value;
    }
    }
    public virtual Wire<double[]> w1 {
    get { throw new NotImplementedException(); }
    set { throw new NotImplementedException();}
    }
    public virtual ArrayMemory<double> m1 { 
    get { throw new NotImplementedException(); }
    }
}
public class subobj_default_impl : subobj{
    public virtual double add_val(double v) {
    throw new NotImplementedException();    }
}
public static class RRExtensions{
}
}
