[Fix] 去掉get...中的异常捕获

异常会被上层捕获,即pybind11.
pybind11会将异常转化为python异常然后回传到python之中。
This commit is contained in:
nanoric 2019-02-14 03:09:01 -04:00
parent 8c0d11d601
commit 92d488e640

View File

@ -61,81 +61,51 @@ public:
//从字典中获取某个建值对应的整数,并赋值到请求结构体对象的值上 //从字典中获取某个建值对应的整数,并赋值到请求结构体对象的值上
void getInt(dict d, const char *key, int *value) void getInt(const dict &d, const char *key, int *value)
{ {
if (d.contains(key)) //检查字典中是否存在该键值 if (d.contains(key)) //检查字典中是否存在该键值
{ {
object o = d[key]; //获取该键值 object o = d[key]; //获取该键值
try
{
*value = o.cast<int>(); *value = o.cast<int>();
} }
catch (const error_already_set &e)
{
cout << e.what() << endl;
}
}
}; };
//从字典中获取某个建值对应的浮点数,并赋值到请求结构体对象的值上 //从字典中获取某个建值对应的浮点数,并赋值到请求结构体对象的值上
void getDouble(dict d, const char *key, double *value) void getDouble(const dict &d, const char *key, double *value)
{ {
if (d.contains(key)) if (d.contains(key))
{ {
object o = d[key]; object o = d[key];
try
{
*value = o.cast<double>(); *value = o.cast<double>();
} }
catch (const error_already_set &e)
{
cout << e.what() << endl;
}
}
}; };
//从字典中获取某个建值对应的字符,并赋值到请求结构体对象的值上 //从字典中获取某个建值对应的字符,并赋值到请求结构体对象的值上
void getChar(dict d, const char *key, char *value) void getChar(const dict &d, const char *key, char *value)
{ {
if (d.contains(key)) if (d.contains(key))
{ {
object o = d[key]; object o = d[key];
try
{
*value = o.cast<char>(); *value = o.cast<char>();
} }
catch (const error_already_set &e)
{
cout << e.what() << endl;
}
}
}; };
template <size_t size>
using string_literal = char[size];
//从字典中获取某个建值对应的字符串,并赋值到请求结构体对象的值上 //从字典中获取某个建值对应的字符串,并赋值到请求结构体对象的值上
void getString(dict d, const char *key, char *value) template <size_t size>
void getString(const pybind11::dict &d, const char *key, string_literal<size> &value)
{ {
if (d.contains(key)) if (d.contains(key))
{ {
object o = d[key]; object o = d[key];
try std::string s = o.cast<std::string>();
{
string s = o.cast<string>();
const char *buf = s.c_str(); const char *buf = s.c_str();
strcpy(value, buf);
#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;
}
} }
}; };