无法理解multi_index
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。访问时不仅可以设置顺序,还可以设置结果吗?
另外,例如 tag_py
我想迭代所有 getPythonObject
,而不是 ObjectStorage
。这真的可以通过 multi_index
实现吗?
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;
If first index in multi_index
is right: sorting objects inside container refer to ZIndex
value, I'm not sure about another. I need such functionality:
Order by ZIndex but return getCppObject
when iterating. Is it possible not only to set ordering, but result when accessing?
Also, for example tag_py
I want to iterate through all getPythonObject
, not ObjectStorage
. Is this really possible with multi_index
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,
multi_index_container
包含ObjectStorage
对象的实例。因此,您可以按任何顺序迭代它并调用ObjectStorage
类的任何函数。例如使用
tag_py
标签进行迭代:使用
tag_zindex
标签:In your case
multi_index_container
contains instances ofObjectStorage
objects. So you can iterate in any order through it and call any function ofObjectStorage
class.For instance to iterate using
tag_py
tag:Using
tag_zindex
tag: