0
class ObjectStorage
{
private:
std::string objName;
int zIndex;
// Reference for the Object interface
boost::shared_ptr<Object> mCppObject;
// Reference for the Python interface
boost::python::object mPythonObject;
public:
ObjectStorage(const std::string &name, int zIndex_, boost::shared_ptr<Object> cpp, boost::python::object python)
: objName(name), zIndex(zIndex_),
mCppObject(cpp), mPythonObject(python) {}
std::string getName() const { return objName; };
int getZIndex() const { return zIndex; }
boost::shared_ptr<Object> getCppObject() const { return mCppObject; }
boost::python::object getPythonObject() const { return mPythonObject; }
};
// Tagging for multi_index container
struct tag_zindex {};
struct tag_name {};
struct tag_cpp {};
struct tag_py {};
typedef boost::multi_index_container<ObjectStorage,
bmi::indexed_by<
// ZIndex
bmi::ordered_non_unique<
bmi::tag<tag_zindex>,
bmi::const_mem_fun<ObjectStorage, int, &ObjectStorage::getZIndex>
>,
// Name
bmi::ordered_unique<
bmi::tag<tag_name>,
bmi::const_mem_fun<ObjectStorage, std::string, &ObjectStorage::getName>
>,
// CPP reference
bmi::ordered_non_unique<
bmi::tag<tag_cpp>,
bmi::const_mem_fun<ObjectStorage, boost::shared_ptr<Object>, &ObjectStorage::getCppObject>
>,
// Python reference
bmi::ordered_unique<
bmi::tag<tag_py>,
bmi::const_mem_fun<ObjectStorage, boost::python::object, &ObjectStorage::getPythonObject>
>
>
> ObjectWrapperSet;
如果multi_index
第一指标是正确的:分类容器内的对象是指ZIndex
价值,我不知道其他。我需要这样的功能: 按ZIndex排序,但在迭代时返回getCppObject
。是否可能不仅设置顺序,而且访问时会导致结果?不能理解multi_index
另外,例如tag_py
我想遍历所有getPythonObject
而不是ObjectStorage
。这是真的可能与multi_index
?
你看,我说的是另一件事:有没有可能通过一些常见的数据迭代?我不想调用'getPythonObject()'(在你的例子中)。 'multi_index'是否提供这样的功能?我想遍历一些查看器(索引类型),并且在你的例子中不是'ObjectStorage',而是'boost :: python :: object'。那可能吗? – Ockonal 2011-04-10 18:01:38
'multi_index'无法做到这一点,但你可以使用['boost :: iterator_adaptor'](http://www.boost.org/doc/libs/release/libs/iterator/doc/facade-and-adaptor .html)来实现这一点。 – 2011-04-10 18:09:29
@ kirill-v-lyadvinsky感谢您的信息。请你能给我一小段代码来说明适配器的用法吗?我会很高兴看到它。 – Ockonal 2011-04-10 18:13:57