Boost logo

Boost Users :

From: damny_at_[hidden]
Date: 2008-04-17 15:13:20


hi,

i'm a newbie to boost an templates.
but for my interrest i experimentalise with that.
so i created an example like the following:

===============================================================================================

template<typename MAP, typename ELM, typename SC1, typename IT1>
class tCanMap {
  protected:
    mutable MAP _mpMap; // map
    mutable SC1 *_scNme; // sorting criteria name ... have to init from derived class
  public:
                                tCanMap ();
                 MAP &getmap () const;
                 UINT32 setmapelm (ELM val);
    const ELM *getmapelm (STRING stNme) const;
};

template<typename MAP, typename ELM, typename SC1, typename IT1>
tCanMap<MAP,ELM,SC1,IT1>::tCanMap() {
  this->_scNme = NULL;
}

template<typename MAP, typename ELM, typename SC1, typename IT1>
MAP &tCanMap<MAP,ELM,SC1,IT1>::getmap() const {
  return this->_mpMap;
}

template<typename MAP, typename ELM, typename SC1, typename IT1>
UINT32 tCanMap<MAP,ELM,SC1,IT1>::setmapelm(ELM val) {
  if (this!=NULL) { this->_mpMap.insert(val); return 0; }
  return 1;
}

template<typename MAP, typename ELM, typename SC1, typename IT1>
const ELM *tCanMap<MAP,ELM,SC1,IT1>::getmapelm(STRING stNme) const {
  try {
    if ( this!=NULL && this->_scNme!=NULL ) {
      IT1 itr = this->_scNme->find(stNme);
      if ( itr != this->_scNme->end() ) { return &(*itr); }
    }
  }
  catch(...) {}
  return NULL;
}

===============================================================================================

cClass00 {
  STRING nme;
             cClass00() {};
  STRING getnme() { return this->nme; }
}

typedef boost::multi_index_container <
  cClass00,
  boost::multi_index::indexed_by <
    boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(cClass00,STRING,getnme) >
>
> mClass00;

typedef mClass00::nth_index<0>::type mClass00ByNme;
typedef mClass00ByNme::iterator mClass00ByNmeItr;

===============================================================================================

cClass01 : public tCanMap<mClass00,cClass00,mClass00ByNme,mClass00ByNmeItr> {
  STRING nme;
             cClass01() { this->_scNme = &(this->_mpMap.get<0>()); }
  STRING getnme() { return this->nme; }
}

typedef boost::multi_index_container <
  cClass01,
  boost::multi_index::indexed_by <
    boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(cClass01,STRING,getnme) >
>
> mClass01;

typedef mClass01::nth_index<0>::type mClass01ByNme;
typedef mClass01ByNme::iterator mClass01ByNmeItr;

===============================================================================================

cClass02 : public tCanMap<mClass01,cClass01,mClass01ByNme,mClass01ByNmeItr> {
  cClass02() { this->_scNme = &(this->_mpMap.get<0>()); }
  ...
}

===============================================================================================

int main (void) {
  cClass02 *ptClass02 = new cClass02();
  ptClass02->fill_maps_with_elements();
  ptClass02->getmapelm("cClass01_Element")->dosomething(); // this works
  ptClass02->getmapelm("cClass01_Element")->getmapelm("cClass00_Element")->dosomething(); // seg vault !!!
  ...
}

===============================================================================================

could you explain why the second try of using "getmapelm" results in an error?
is there a better way to use a template class like above for handle a multi_index list as a member
in similarly classes, whose objects should describe a hierarchical structure?
(note: list shall have multiple indizes :o)

i use boost 1.34.1 and gcc 3.4.5 (mingw special).

many thanx
daniel
_____________________________________________________________________
Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
http://smartsurfer.web.de/?mc=100071&distributionid=000000000066


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net