boost-python-wrappec++类私有成员
boost python wrap c++ class private member
我们可以用boost python包装c++私有构造函数吗?我有一个单独的c++类,希望将它包装到python中。
我们可以用boost python包装c++私有成员函数吗?
非常感谢
使用类似的东西:
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
using std::cout;
class Singleton
{
private:
Singleton()
{
cout << "Creating instancen";
}
friend Singleton* create();
};
Singleton* pInstance_;
Singleton* create()
{
if(!pInstance_)
{
pInstance_ = new Singleton();
}
else
{
cout << "Using old instancen";
}
return pInstance_;
}
BOOST_PYTHON_MODULE(cppmodule)
{
def("create", create, return_value_policy<reference_existing_object>());
class_<Singleton>("Singleton", no_init);
}
/* using singleton later in code */
Singleton* otherInstance_ = create();
会话:
>>> import cppmodule
Creating instance
>>> s = cppmodule.Singleton()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python
>>> s = cppmodule.create()
Using old instance
singleton通常会用私有可见性声明其构造函数,并提供一个可以访问私有构造函数的工厂函数。
class singleton
{
public:
/// @brief Public factory function.
static singleton& instance()
{
static singleton instance_;
return instance_;
}
private:
// Private constructors and destructor.
singleton() {}
~singleton() {};
singleton(const singleton&);
singleton& operator=(const singleton&);
};
默认情况下,Boost.Python假定一个公共可用的构造函数,该构造函数与提供给boost::python::class_
构造函数的init表达式相匹配。为了抑制这种行为,可以向class_
构造函数提供特殊的boost::python::no_init
对象。此外,由于私有析构函数,class_
需要通过提供boost::noncopyable
作为模板参数来公开为不可复制。
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<singleton, boost::noncopyable>("Singleton", python::no_init);
}
在Python中,singleton很少通过其API将自己显示为singleton。因此,许多Python开发人员可能自然希望能够通过example.Singleton()
实例化Python类,而不是使用factor方法。虽然默认构造函数已通过python::no_init
公开,但可以为__init__
定义自定义构造函数。为了正确解释C++单例,通过Boost.Python公开的单例将使用带有无操作deleter的boost::shared_ptr
来保存单例的句柄。下面是一个完整的例子:
#include <boost/python.hpp>
/// @brief Mock up singleton class.
class singleton
{
public:
/// @brief Public factory function.
static singleton& instance()
{
static singleton instance_;
return instance_;
}
void set_x(unsigned int x) { x_ = x; }
unsigned int get_x() { return x_; }
private:
// Private constructors and destructor.
singleton() : x_(0) {}
~singleton() {};
singleton(const singleton&);
singleton& operator=(const singleton&);
private:
unsigned int x_;
};
/// @brief No operation deleter.
void noop_deleter(void*) {};
/// @brief Helper function used to get a shared_ptr that holds
/// a singleton.
boost::shared_ptr<singleton> py_get_singleton()
{
return boost::shared_ptr<singleton>(
&singleton::instance(), // the instance
&noop_deleter); // no-op deleter
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
// Expose the singleton class, supressing the default constructor
// via python::no_init, and providing a constructor that will return
// a handle to the singleton.
python::class_<singleton, boost::shared_ptr<singleton>,
boost::noncopyable>("Singleton", python::no_init)
.def("__init__", python::make_constructor(&py_get_singleton))
.add_property("x", &singleton::get_x, &singleton::set_x)
;
}
及其用途:
>>> import example
>>> s1 = example.Singleton()
>>> s2 = example.Singleton()
>>> s1.x
0
>>> s2.x
0
>>> s1.x = 5
>>> s2.x
5
请注意,从Python的角度来看,这更类似于borg或单状态模式,因为它们共享状态,而不是身份。(即s1.x == s2.x
,但id(s1) != id(s2)
。)如果Python类也需要是一个单例,而不仅仅是它的状态,那么可能需要用Python代码而不是C++来实现这种行为。
- 我应该包含什么来制作 boost.python 扩展?
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- Boost Python Numpy - 要初始化的未定义引用
- Boost.Python :C++模板类型匹配的嵌套命名空间
- Boost.Python 列出了所有公开的类和属性
- Boost.Python 在静态库方面失败
- 在cpp中使用boost-python的python代码是否进行动态内存分配
- Boost.Python 和导入 dll,"The specified module could not be found"
- 无法将__str__特殊方法与Boost::Python接口
- 编译时出现Boost.python链接错误
- 使用 boost python 从 c++ 为 python 中的类成员变量赋值
- Boost Python 不会自动转换 char* 数据成员
- Boost Python def 成员函数,它调用静态函数,其中 self 作为唯一的参数
- 将 int 指针成员变量包装在 boost.python/pyplusplus 中
- boost-python-wrappec++类私有成员
- 通过Boost.Python向Python公开C样式数组数据成员
- 使用boost::python公开具有std::函数作为参数的C++成员函数
- 查找Boost python::对象中是否存在成员函数
- Boost.Python:从成员函数中获取'self'