c backend refact, implementing serilizer
[PxpRpc.git] / c / include / pxprpc_pp.hpp
blobadb4dd611849e84f7848d21c0fcfa6feb02943ba
3 #pragma once
6 extern "C"{
7 #include <pxprpc.h>
10 #include <functional>
12 namespace pxprpc{
16 static void __wrap_readParameter(struct pxprpc_callable *self,struct pxprpc_request *r,void (*doneCallback)(struct pxprpc_request *r));
19 static void __wrap_call(struct pxprpc_callable *self,struct pxprpc_request *r,void (*onResult)(struct pxprpc_request *r,struct pxprpc_object *result));
22 static void __wrap_writeResult(struct pxprpc_callable *self,struct pxprpc_request *r);
24 pxprpc_server_api *servapi;
26 extern void init(){
27 pxprpc_server_query_interface(&servapi);
30 static uint32_t __wrap_object_addRef(struct pxprpc_object *self);
31 static uint32_t __wrap_object_release(struct pxprpc_object *self);
33 class RpcObject{
34 struct pxprpc_object cObj;
35 public:
36 virtual ~RpcObject(){
38 virtual int addRef(){
39 this->cObj.count++;
40 return this->cObj.count;
42 virtual int release(){
43 this->cObj.count--;
44 if(this->cObj.count<=0){
45 delete this;
46 return 0;
47 }else{
48 return this->cObj.count;
51 RpcObject(){
52 this->cObj.object1=this;
53 this->cObj.count=0;
54 this->cObj.addRef=&__wrap_object_addRef;
55 this->cObj.release=&__wrap_object_release;
57 virtual struct pxprpc_object *cObject(){
58 return &cObj;
62 class RpcRawBytes:public RpcObject{
63 public:
64 virtual ~RpcRawBytes(){
66 uint8_t *data;
67 int size;
68 RpcRawBytes(uint8_t *data,int size):RpcObject(){
69 this->data=data;
70 this->size=size;
72 virtual struct pxprpc_object *cObject(){
73 auto r1=pxprpc::servapi->new_bytes_object(this->size);
74 pxprpc::servapi->fill_bytes_object(r1,this->data,this->size);
75 delete this;
76 return r1;
81 static uint32_t __wrap_object_addRef(struct pxprpc_object *self){
82 auto rpcObj=static_cast<RpcObject *>(self->object1);
83 rpcObj->addRef();
86 static uint32_t __wrap_object_release(struct pxprpc_object *self){
87 auto rpcObj=static_cast<RpcObject *>(self->object1);
88 rpcObj->release();
91 static void __NamedFunctionPPIoRead(void *p);
93 class NamedFunctionPP;
94 class __IoReadArg{
95 public:
96 std::function<void(struct pxprpc_abstract_io *,const char *)> doneCallback;
97 struct pxprpc_abstract_io *io1;
98 NamedFunctionPP *callable;
102 class NamedFunctionPP{
104 protected:
105 struct pxprpc_callable callable;
106 struct pxprpc_namedfunc mCNamedFunc;
107 std::string name;
108 public:
109 virtual void readParameter(struct pxprpc_request *r,std::function<void()> doneCallback)=0;
110 virtual void call(struct pxprpc_request *r,std::function<void(RpcObject *)> onResult)=0;
111 virtual void writeResult(struct pxprpc_request *r)=0;
112 void readFromIo(struct pxprpc_abstract_io *io1,uint8_t *buf,int length,
113 std::function<void(struct pxprpc_abstract_io *,const char *)> doneCallback){
114 auto arg1=new __IoReadArg();
115 arg1->callable=this;
116 arg1->doneCallback=doneCallback;
117 arg1->io1=io1;
118 io1->read(io1,length,buf,&__NamedFunctionPPIoRead,arg1);
120 NamedFunctionPP(std::string funcName){
121 this->callable.userData=this;
122 this->callable.readParameter=__wrap_readParameter;
123 this->callable.call=__wrap_call;
124 this->callable.writeResult=__wrap_writeResult;
125 this->mCNamedFunc.callable=&this->callable;
126 this->name=funcName;
127 this->mCNamedFunc.name=this->name.c_str();
129 virtual struct pxprpc_namedfunc *cNamedFunc(){
130 return &mCNamedFunc;
132 virtual ~NamedFunctionPP(){
136 static void __NamedFunctionPPIoRead(void *p){
137 auto *arg0=static_cast<__IoReadArg *>(p);
138 arg0->doneCallback(arg0->io1,
139 arg0->io1->get_error(arg0->io1,(void *)arg0->io1->read));
140 delete arg0;
147 static void __wrap_readParameter(struct pxprpc_callable *self,struct pxprpc_request *r,void (*doneCallback)(struct pxprpc_request *r)){
148 NamedFunctionPP *selfpp=static_cast<NamedFunctionPP *>(self->userData);
149 selfpp->readParameter(r,[doneCallback,r]()->void{doneCallback(r);});
153 static void __wrap_call(struct pxprpc_callable *self,struct pxprpc_request *r,void (*onResult)(struct pxprpc_request *r,struct pxprpc_object *result)){
154 NamedFunctionPP *selfpp=static_cast<NamedFunctionPP *>(self->userData);
155 selfpp->call(r,[onResult,r](pxprpc::RpcObject *resultObj)->void{
156 onResult(r,resultObj->cObject());
160 static void __wrap_writeResult(struct pxprpc_callable *self,struct pxprpc_request *r){
161 NamedFunctionPP *selfpp=static_cast<NamedFunctionPP *>(self->userData);
162 selfpp->writeResult(r);