2011-04-10 68 views
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

回答

1

在你的情況下,multi_index_container包含ObjectStorage對象的實例。所以你可以通過它迭代任何順序並調用ObjectStorage類的任何函數。

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(); 
} 

使用tag_zindex標籤:

例如使用tag_py標籤遍歷

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 
} 
+0

你看,我說的是另一件事:有沒有可能通過一些常見的數據迭代?我不想調用'getPythonObject()'(在你的例子中)。 'multi_index'是否提供這樣的功能?我想遍歷一些查看器(索引類型),並且在你的例子中不是'ObjectStorage',而是'boost :: python :: object'。那可能嗎? – Ockonal 2011-04-10 18:01:38

+0

'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

+0

@ kirill-v-lyadvinsky感謝您的信息。請你能給我一小段代碼來說明適配器的用法嗎?我會很高興看到它。 – Ockonal 2011-04-10 18:13:57