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;

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?

rcollyer
  • 10,475
  • 4
  • 48
  • 75
Max Frai
  • 61,946
  • 78
  • 197
  • 306

1 Answers1

1

In your case multi_index_container contains instances of ObjectStorage objects. So you can iterate in any order through it and call any function of ObjectStorage class.

For instance to iterate using tag_py tag:

ObjectWrapperSet ow_set;

ObjectWrapperSet::index_const_iterator<tag_py>::type it = ow_set.get<tag_py>().begin();
for ( ; it != ow_set.get<tag_py>().end(); ++it ) {
  const ObjectStorage& os = *it; // note `it` is the iterator for ObjectStorage
  // now you can do
  boost::python::object po = os.getPythonObject();
  // or
  boost::python::object po = it->getPythonObject();
}

Using tag_zindex tag:

ObjectWrapperSet::index_const_iterator<tag_zindex>::type it = ow_set.get<tag_zindex>().begin();
for ( ; it != ow_set.get<tag_zindex>().end(); ++it ) {
  boost::shared_ptr<Object> cpp_obj = it->getCppObject();
  // do something
}
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • Look, I'm talking about another thing: is it possible to iterate through some common data? I don't want to call `getPythonObject()` (in your example). Does `multi_index` gives such functionality? I want to iterate through some viewer(index type) and get not `ObjectStorage`, but `boost::python::object` in your example. Is that possible? – Max Frai Apr 10 '11 at 18:01
  • `multi_index` can't do that, but you can use [`boost::iterator_adaptor`](http://www.boost.org/doc/libs/release/libs/iterator/doc/facade-and-adaptor.html) to achieve that. – Kirill V. Lyadvinsky Apr 10 '11 at 18:09
  • @kirill-v-lyadvinsky Thanks for the info. Please, can you give me a little piece of code which shows usage of adaptor? I'll be very glad to see it. – Max Frai Apr 10 '11 at 18:13
  • [Here](http://live.boost.org/doc/libs/1_46_0/libs/iterator/doc/iterator_facade.html#tutorial-example) you can find tutorial example. – Kirill V. Lyadvinsky Apr 10 '11 at 18:20