美文网首页
CPythonUtil

CPythonUtil

作者: 云谁之殇 | 来源:发表于2018-10-16 14:32 被阅读20次
/*******************************************************************
FileName: PyUtil.h
Author  : yszs
Date    : 2018-09-28 15:40:46
Desc    : C++ 调用Python模块相关函数
*******************************************************************/
#include "stdafx.h"
#include "python27/Python.h"

class CPythonUtil
{
public:
    CPythonUtil()
    {
        Py_Initialize();
    }

    CPythonUtil(std::string strModuleName)
        :CPythonUtil()
    {
        ImportModule(strModuleName);
    }

    ~CPythonUtil()
    {
        if (pModule_)
        {
            Py_DecRef(pModule_);
        }
        Py_Finalize();
    }

    //********************************************************************     
    // 函数说明:    获取Python模块     
    // 返回值:     bool                        是否成功导入Python模块
    // 参数:  std::string strModuleName   需要导入的Python模块名称(只有文件名,不包括文件格式)
    //******************************************************************** 
    bool ImportModule(std::string strModuleName)
    {
        strModuleName_ = strModuleName;
        if (Py_IsInitialized())
        {
            pModule_ = PyImport_ImportModule(strModuleName.c_str());
            return pModule_ != nullptr;
        }
        else
        {
            assert(0);
        }
        return false;
    }

    PyObject* ExcuteFunc(std::string strFuncName, PyObject* pArgs = nullptr, bool bAutoGC = true)
    {
        if (pModule_)
        {
            PyObject* pFunc = GetFunc(strFuncName);
            if (pFunc)
            {
                PyObject* pReturn = ExcuteFunc(pFunc, pArgs, bAutoGC);
                if (bAutoGC)
                {
                    if (pFunc) Py_DecRef(pFunc);
                    if (pArgs) Py_DecRef(pArgs);
                }
                return pReturn;
            }
        }
        else
        {
            assert(0);
        }
        return nullptr;
    }

    PyObject* ExcuteFunc(PyObject* pFunc, PyObject* pArgs = nullptr, bool bAutoGC = true)
    {
        if (pFunc)
        {
            PyObject* pReturn = NULL;
            pReturn = PyEval_CallObject(pFunc, pArgs);
            if (bAutoGC)
            {
                if (pArgs) Py_DecRef(pArgs);
            }
            return pReturn;
        }
        else
        {
            assert(0);
        }

        return nullptr;
    }

    PyObject* GetFunc(std::string strFuncName)
    {
        if (pModule_)
        {
            PyObject* pFunc = NULL;
            pFunc = PyObject_GetAttrString(pModule_, strFuncName.c_str());
            return pFunc;
        }
        else
        {
            assert(0);
        }
        return nullptr;
    }

    template<typename T>
    static T ParseArg(PyObject* pReturn, bool bAutoGC = true)
    {
        T obj = {0};
        if (pReturn)
        {
            if (std::is_same<T, char*>::value)
            {
                PyArg_Parse(pReturn, "s", &obj);
            }
            else if (std::is_same<T, int>::value)
            {
                PyArg_Parse(pReturn, "i", &obj);
            }
            else if (std::is_same<T, float>::value)
            {
                PyArg_Parse(pReturn, "d", &obj);
            }
            else if (std::is_same<T, bool>::value)
            {
                int t = 0;
                PyArg_Parse(pReturn, "i", &t);
                obj = t ? true : false;
            }
            else
            {
                //...
            }

            if (bAutoGC)
            {
                if (pReturn) Py_DecRef(pReturn);
            }
        }
        return obj;
    }


    //********************************************************************     
    // 函数说明:    string转化为PyUnicode   
    // 时间:     2018/10/09 16:32     
    // 返回值:     PyObject*     
    // 参数:  std::string str    
    //******************************************************************** 
    static PyObject* StringToPyUnicode(std::string str)
    {
        int wlen = ::MultiByteToWideChar(CP_ACP, NULL, str.c_str(), int(str.size()), NULL, 0);
        wchar_t* wszString = new wchar_t[wlen + 1];
        ::MultiByteToWideChar(CP_ACP, NULL, str.c_str(), int(str.size()), wszString, wlen);
        wszString[wlen] = '\0';
        PyObject* pObj = PyUnicode_FromUnicode((const Py_UNICODE*)wszString, wlen);
        delete[] wszString;
        wszString = nullptr;
        return pObj;
    }

private:
    PyObject* pModule_ = nullptr;
    std::string strModuleName_ = "";
};

相关文章

网友评论

      本文标题:CPythonUtil

      本文链接:https://www.haomeiwen.com/subject/hdrhzftx.html