From 92d488e640fc41132896b0f7930320726cdcab7e Mon Sep 17 00:00:00 2001 From: nanoric Date: Thu, 14 Feb 2019 03:09:01 -0400 Subject: [PATCH] =?UTF-8?q?[Fix]=20=E5=8E=BB=E6=8E=89get...=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 异常会被上层捕获,即pybind11. pybind11会将异常转化为python异常然后回传到python之中。 --- vnpy/api/ctp/vnctp/vnctp.h | 148 +++++++++++++++---------------------- 1 file changed, 59 insertions(+), 89 deletions(-) diff --git a/vnpy/api/ctp/vnctp/vnctp.h b/vnpy/api/ctp/vnctp/vnctp.h index 2858d559..c1d067ec 100644 --- a/vnpy/api/ctp/vnctp/vnctp.h +++ b/vnpy/api/ctp/vnctp/vnctp.h @@ -18,125 +18,95 @@ using namespace pybind11; //ṹ struct Task { - int task_name; //صƶӦij - void *task_data; //ָ - void *task_error; //ָ - int task_id; //id - bool task_last; //ǷΪ󷵻 + int task_name; //صƶӦij + void *task_data; //ָ + void *task_error; //ָ + int task_id; //id + bool task_last; //ǷΪ󷵻 }; class TaskQueue { private: - queue queue_; //׼ - mutex mutex_; // - condition_variable cond_; // + queue queue_; //׼ + mutex mutex_; // + condition_variable cond_; // public: - //µ - void push(const Task &task) - { - unique_lock mlock(mutex_); - queue_.push(task); //д - mlock.unlock(); //ͷ - cond_.notify_one(); //֪ͨȴ߳ - } + //µ + void push(const Task &task) + { + unique_lock mlock(mutex_); + queue_.push(task); //д + mlock.unlock(); //ͷ + cond_.notify_one(); //֪ͨȴ߳ + } - //ȡϵ - Task pop() - { - unique_lock mlock(mutex_); - while (queue_.empty()) //Ϊʱ - { - cond_.wait(mlock); //ȴ֪ͨ - } - Task task = queue_.front(); //ȡеһ - queue_.pop(); //ɾ - return task; //ظ - } + //ȡϵ + Task pop() + { + unique_lock mlock(mutex_); + while (queue_.empty()) //Ϊʱ + { + cond_.wait(mlock); //ȴ֪ͨ + } + Task task = queue_.front(); //ȡеһ + queue_.pop(); //ɾ + return task; //ظ + } }; //ֵлȡijֵӦֵṹֵ -void getInt(dict d, const char *key, int *value) +void getInt(const dict &d, const char *key, int *value) { - if (d.contains(key)) //ֵǷڸüֵ - { - object o = d[key]; //ȡüֵ - try - { - *value = o.cast(); - } - catch (const error_already_set &e) - { - cout << e.what() << endl; - } - } + if (d.contains(key)) //ֵǷڸüֵ + { + object o = d[key]; //ȡüֵ + *value = o.cast(); + } }; //ֵлȡijֵӦĸֵṹֵ -void getDouble(dict d, const char *key, double *value) +void getDouble(const dict &d, const char *key, double *value) { - if (d.contains(key)) - { - object o = d[key]; - try - { - *value = o.cast(); - } - catch (const error_already_set &e) - { - cout << e.what() << endl; - } - } + if (d.contains(key)) + { + object o = d[key]; + *value = o.cast(); + } }; //ֵлȡijֵӦֵַṹֵ -void getChar(dict d, const char *key, char *value) +void getChar(const dict &d, const char *key, char *value) { - if (d.contains(key)) - { - object o = d[key]; - - try - { - *value = o.cast(); - } - catch (const error_already_set &e) - { - cout << e.what() << endl; - } - } + if (d.contains(key)) + { + object o = d[key]; + *value = o.cast(); + } }; -//ֵлȡijֵӦֵַṹֵ -void getString(dict d, const char *key, char *value) -{ - if (d.contains(key)) - { - object o = d[key]; - try - { - string s = o.cast(); - const char *buf = s.c_str(); +template +using string_literal = char[size]; -#ifdef _MSC_VER //WIN32 - strcpy_s(value, strlen(buf) + 1, buf); -#elif __GNUC__ - strncpy(value, buffer, strlen(buffer) + 1); -#endif - } - catch (const error_already_set &e) - { - cout << e.what() << endl; - } - } +//ֵлȡijֵӦֵַṹֵ +template +void getString(const pybind11::dict &d, const char *key, string_literal &value) +{ + if (d.contains(key)) + { + object o = d[key]; + std::string s = o.cast(); + const char *buf = s.c_str(); + strcpy(value, buf); + } }; //GBKַתΪUTF8