@@ -19,12 +19,58 @@ std::map<std::string, const char *> SID_USRTAG;
1919
2020PYBIND11_EMBEDDED_MODULE (aiges_embed, module ) {
2121 module .def (" callback" , &callBack, py::return_value_policy::automatic_reference);
22- py::class_<ResponseData> responseData (module , " ResponseData" );
22+ py::class_<ResponseData> responseData (module , " ResponseData" , py::buffer_protocol () );
2323 responseData.def (py::init<>())
24+ .def (py::init<std::string, unsigned int , int , int >())
25+ .def (py::init ([](const py::buffer &b) {
26+ py::buffer_info info = b.request ();
27+ if (info.format != py::format_descriptor<unsigned char >::format () || info.ndim != 1 ) {
28+ throw std::runtime_error (" Incompatible buffer format! Please Pass Bytes..." );
29+ }
30+ auto *v = new ResponseData ();
31+ v->len = info.shape [0 ];
32+ v->data = info.ptr ;
33+ // memcpy( v->data, info.ptr, info.shape[0] );
34+ return (v);
35+ }))
36+ .def (" setData" , [](ResponseData &r, const py::buffer &b) {
37+ py::buffer_info info = b.request ();
38+ if (info.format != py::format_descriptor<unsigned char >::format () || info.ndim != 1 ) {
39+ throw std::runtime_error (" Incompatible buffer format! Please Pass Bytes.." );
40+ }
41+ if (r.data == nullptr ) {
42+ void *p = malloc (info.shape [0 ]);
43+ if (p == nullptr ) {
44+ throw std::runtime_error (" Can't Allocate memory!" );
45+ }
46+ r.data = p;
47+ }
48+ r.len = info.shape [0 ];
49+ r.data = info.ptr ;
50+ // memcpy(r.data, info.ptr, info.shape[0]);
51+ })
52+ /* / Bare bones interface */
53+ .def (" setDataType" ,
54+ [](ResponseData &r, int i) {
55+ r.type = i;
56+ })
57+ /* / Provide buffer access */
58+ .def_buffer ([](ResponseData &r) -> py::buffer_info {
59+ return (py::buffer_info (
60+ r.data , /* Pointer to buffer */
61+ sizeof (unsigned char ), /* Size of one scalar */
62+ py::format_descriptor<unsigned char >::format (), /* Python struct-style format descriptor */
63+ 1 , /* Number of dimensions */
64+ {size_t (r.len )}, /* Buffer dimensions */
65+ { /* Strides (in bytes) for each index */
66+ sizeof (unsigned char )}
67+ ));
68+ })
2469 .def_readwrite (" key" , &ResponseData::key, py::return_value_policy::automatic_reference)
25- .def_readwrite (" data" , &ResponseData::data, py::return_value_policy::automatic_reference)
70+ .def_property_readonly (" data" ,
71+ py::cpp_function (&ResponseData::get_data, py::return_value_policy::automatic_reference))
2672 .def_readwrite (" status" , &ResponseData::status, py::return_value_policy::automatic_reference)
27- .def_readwrite (" len" , &ResponseData::len , py::return_value_policy::automatic_reference)
73+ .def_property_readonly (" len" , py::cpp_function ( &ResponseData::get_len , py::return_value_policy::automatic_reference) )
2874 .def_readwrite (" type" , &ResponseData::type, py::return_value_policy::automatic_reference);
2975
3076 py::class_<Response> response (module , " Response" );
@@ -263,19 +309,20 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
263309 tmpData->type = DataType (itemData.type );
264310 tmpData->desc = nullptr ;
265311 // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
266- void *pr;
267- pr = malloc (itemData.len );
268- if (pr == nullptr ) {
269- int ret = -1 ;
270- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
271- return ret;
272- }
273- ptr = PyBytes_AsString (itemData.data .ptr ());
274- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
275- memcpy (pr, ptr, itemData.len );
312+ // void *pr;
313+ // pr = malloc(itemData.len);
314+ // if (pr == nullptr) {
315+ // int ret = -1;
316+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
317+ // return ret;
318+ // }
319+ // ptr = PyBytes_AsString(itemData.data.ptr());
320+ // Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
321+ // printf("GetSIze data len: %d", itemData.len);
322+ // memcpy(pr, ptr, itemData.len);
276323 // char *data_ = new char[itemData.data.length()+1];
277324 // strdup(.c_str());
278- tmpData->data = pr ;
325+ tmpData->data = itemData. data ;
279326 tmpData->status = DataStatus (itemData.status );
280327 if (idx == 0 ) {
281328 headPtr = tmpData;
@@ -284,10 +331,9 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
284331 curPtr->next = tmpData;
285332 curPtr = tmpData;
286333 }
287- spdlog::debug (" get result,key:{},data:{},len:{},size:{}, type:{},status:{},sid:{}" ,
288- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
334+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
335+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
289336 tmpData->status , sid);
290-
291337 }
292338 *respData = headPtr;
293339 }
@@ -416,7 +462,6 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
416462 resp = r.cast <Response *>();
417463 pDataList headPtr;
418464 pDataList curPtr;
419- char *ptr;
420465 // 先判断python有没有抛出错误. response中的 errorCode
421466 if (resp->errCode != 0 ) {
422467 spdlog::get (" stderr_console" )->error (" find error from python: {}" , resp->errCode );
@@ -437,20 +482,18 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
437482 tmpData->len = itemData.len ;
438483 tmpData->type = DataType (itemData.type );
439484 tmpData->desc = nullptr ;
440- // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
441- void *pr;
442- pr = malloc (itemData.len );
443- if (pr == nullptr ) {
444- int ret = -1 ;
445- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
446- return ret;
447- }
448- ptr = PyBytes_AsString (itemData.data .ptr ());
449- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
450- memcpy (pr, ptr, size);
485+ // // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
486+ // void *pr;
487+ // pr = malloc(itemData.len);
488+ // if (pr == nullptr) {
489+ // int ret = -1;
490+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
491+ // return ret;
492+ // }
493+ // memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
451494 // char *data_ = new char[itemData.data.length()+1];
452495 // strdup(.c_str());
453- tmpData->data = pr ;
496+ tmpData->data = itemData. data ;
454497 tmpData->status = DataStatus (itemData.status );
455498 if (idx == 0 ) {
456499 headPtr = tmpData;
@@ -459,8 +502,8 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
459502 curPtr->next = tmpData;
460503 curPtr = tmpData;
461504 }
462- spdlog::debug (" callback result,key:{},data:{},len:{},size, {},type:{},status:{},sid:{}" ,
463- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
505+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
506+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
464507 tmpData->status , sid);
465508 }
466509 *respData = headPtr;
@@ -559,20 +602,20 @@ int callBack(Response *resp, std::string sid) {
559602 tmpData->type = DataType (itemData.type );
560603 tmpData->desc = nullptr ;
561604 // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
562- void *pr;
563- pr = malloc (itemData.len );
564- if (pr == nullptr ) {
565- int ret = -1 ;
566- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
567- return ret;
568- }
569- ptr = PyBytes_AsString (itemData.data .ptr ());
570- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
571- memcpy (pr, ptr, itemData.len );
605+ // void *pr;
606+ // pr = malloc(itemData.len);
607+ // if (pr == nullptr) {
608+ // int ret = -1;
609+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
610+ // return ret;
611+ // }
612+ // ptr = PyBytes_AsString(itemData.data.ptr());
613+ // Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
614+ // memcpy(pr, ptr, itemData.len);
572615 // 还是有问题::memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
573616 // char *data_ = new char[itemData.data.length()+1];
574617 // strdup(.c_str());
575- tmpData->data = pr ;
618+ tmpData->data = itemData. data ;
576619 tmpData->status = DataStatus (itemData.status );
577620 if (idx == 0 ) {
578621 headPtr = tmpData;
@@ -581,8 +624,8 @@ int callBack(Response *resp, std::string sid) {
581624 curPtr->next = tmpData;
582625 curPtr = tmpData;
583626 }
584- spdlog::debug (" callback result,key:{},data:{},len:{},size,{}, type:{},status:{},sid:{}" ,
585- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
627+ spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
628+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
586629 tmpData->status , sid);
587630 }
588631
0 commit comments