Ignore:
Timestamp:
10/30/15 16:33:48 (8 years ago)
Author:
rlacroix
Message:

Keep track of whether an object id was automatically generated or not.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/object_factory_impl.hpp

    r680 r769  
    5050      std::vector<boost::shared_ptr<U> > & vect = 
    5151                     U::AllVectObj[CObjectFactory::CurrContext]; 
    52                       
    53       typename std::vector<boost::shared_ptr<U> >::const_iterator  
     52 
     53      typename std::vector<boost::shared_ptr<U> >::const_iterator 
    5454         it = vect.begin(), end = vect.end(); 
    55        
     55 
    5656      for (; it != end; it++) 
    5757      { 
     
    6060            return (ptr); 
    6161      } 
    62        
     62 
    6363      ERROR("CObjectFactory::GetObject(const U * const object)", 
    6464               << "[type = " << U::GetName() << ", adress = " << object << "] " 
     
    9191 
    9292   template <typename U> 
    93       boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString & id) 
     93   boost::shared_ptr<U> CObjectFactory::CreateObject(const StdString& id) 
    9494   { 
    95       if (CurrContext.size() == 0) 
    96          ERROR("CObjectFactory::CreateObject(const StdString & id)", 
     95      if (CurrContext.empty()) 
     96         ERROR("CObjectFactory::CreateObject(const StdString& id)", 
    9797               << "[ id = " << id << " ] please define current context id !"); 
    98       if (id.size() == 0) 
     98 
     99      if (CObjectFactory::HasObject<U>(id)) 
    99100      { 
    100          boost::shared_ptr<U> value(new U(CObjectFactory::GenUId<U>())); 
    101          U::AllVectObj[CObjectFactory::CurrContext].insert 
    102             (U::AllVectObj[CObjectFactory::CurrContext].end(), value); 
    103          U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value)); 
    104          return (value); 
    105       } 
    106       else if (CObjectFactory::HasObject<U>(id)) 
    107       { 
    108          return (CObjectFactory::GetObject<U>(id)); 
     101         return CObjectFactory::GetObject<U>(id); 
    109102      } 
    110103      else 
    111104      { 
    112          boost::shared_ptr<U> value(new U(id)); 
    113          U::AllVectObj[CObjectFactory::CurrContext].insert 
    114             (U::AllVectObj[CObjectFactory::CurrContext].end(), value); 
    115          U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(id, value)); 
    116          return (value); 
     105         boost::shared_ptr<U> value(new U(id.empty() ? CObjectFactory::GenUId<U>() : id)); 
     106 
     107         U::AllVectObj[CObjectFactory::CurrContext].insert(U::AllVectObj[CObjectFactory::CurrContext].end(), value); 
     108         U::AllMapObj[CObjectFactory::CurrContext].insert(std::make_pair(value->getId(), value)); 
     109 
     110         return value; 
    117111      } 
    118112   } 
     
    124118      return (U::AllVectObj[context]); 
    125119   } 
    126     
    127    template <typename U>  
    128        StdString CObjectFactory::GenUId(void) 
    129        { 
    130           long int seed ; 
    131            
    132           xios_map<StdString,long int>::iterator it ; 
    133           it=U::GenId.find(CObjectFactory::CurrContext); 
    134           if (it==U::GenId.end()) 
    135           { 
    136             seed=0 ; 
    137             U::GenId[CObjectFactory::CurrContext]=seed ; 
    138           } 
    139           else 
    140           { 
    141             seed=it->second ; 
    142             seed++ ; 
    143             it->second=seed ; 
    144           } 
    145            
    146           StdOStringStream oss ; 
    147           oss<<"__"<<U::GetName()<<"_undef_id_"<<seed ; 
    148           return StdString(oss.str()) ; 
    149         } 
    150            
     120 
     121   template <typename U> 
     122   const StdString& CObjectFactory::GetUIdBase(void) 
     123   { 
     124      static const StdString base = "__" + U::GetName() + "_undef_id_"; 
     125      return base; 
     126   } 
     127 
     128   template <typename U> 
     129   StdString CObjectFactory::GenUId(void) 
     130   { 
     131      StdOStringStream oss; 
     132      oss << GetUIdBase<U>() << U::GenId[CObjectFactory::CurrContext]++; 
     133      return oss.str(); 
     134   } 
     135 
     136   template <typename U> 
     137   bool CObjectFactory::IsGenUId(const StdString& id) 
     138   { 
     139      const StdString& base = GetUIdBase<U>(); 
     140      return (id.size() > base.size() && id.compare(0, base.size(), base) == 0); 
     141   } 
     142 
    151143} // namespace xios 
    152144 
Note: See TracChangeset for help on using the changeset viewer.