Changeset 20009

Show
Ignore:
Timestamp:
04/27/07 14:13:55 (2 years ago)
Author:
aronb
Message:

Extend API to allow adding a ConfigElement? to a Configuration.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/jackal/config/jccl/Config/Configuration.cpp

    r19811 r20009  
    4545{ 
    4646 
     47 
     48/** 
     49 * This is a helper for use with std::find_if() for comparing element names. 
     50 */ 
     51struct ElementNamePred 
     52{ 
     53   ElementNamePred(const std::string& name) 
     54      : mName(name) 
     55   {} 
     56 
     57   bool operator()(jccl::ConfigElementPtr element) 
     58   { 
     59      return (element->getName() == mName); 
     60   } 
     61 
     62   std::string mName; 
     63}; 
     64 
    4765Configuration::Configuration() 
    4866{} 
     
    108126   } 
    109127   return false; 
     128} 
     129 
     130void Configuration::add(ConfigElementPtr newElement) 
     131{ 
     132   // Before we can add newElement to the database, we have to determine 
     133   // if there is already a element with the same name. 
     134   std::vector<ConfigElementPtr>::iterator iter = 
     135      std::find_if(mElements.begin(), mElements.end(), 
     136                   ElementNamePred(newElement->getName())); 
     137 
     138   // If no existing element has the same name as newElement, then we 
     139   // can just add it to the end. 
     140   if ( iter == mElements.end() ) 
     141   { 
     142      mElements.push_back(newElement); 
     143   } 
     144   // Otherwise, overwrite the old version. 
     145   else 
     146   { 
     147      *iter = newElement; 
     148   } 
    110149} 
    111150 
     
    323362} 
    324363 
    325 /** 
    326  * This is a helper for use with std::find_if() for comparing element names. 
    327  */ 
    328 struct ElementNamePred 
    329 { 
    330    ElementNamePred(const std::string& name) 
    331       : mName(name) 
    332    {} 
    333  
    334    bool operator()(jccl::ConfigElementPtr element) 
    335    { 
    336       return (element->getName() == mName); 
    337    } 
    338  
    339    std::string mName; 
    340 }; 
    341  
    342364/** Load the elements from a given 'elements' tree into this configuration. */ 
    343365bool Configuration::loadFromElementNode(cppdom::NodePtr elementsNode, 
     
    370392      else 
    371393      { 
    372          // Before we can add new_element to the database, we have to determine 
    373          // if there is already a element with the same name. 
    374          std::vector<ConfigElementPtr>::iterator iter = 
    375             std::find_if(mElements.begin(), mElements.end(), 
    376                          ElementNamePred(new_element->getName())); 
    377  
    378          // If no existing element has the same name as new_element, then we 
    379          // can just add it to the end. 
    380          if ( iter == mElements.end() ) 
    381          { 
    382             mElements.push_back(new_element); 
    383          } 
    384          // Otherwise, overwrite the old version. 
    385          else 
    386          { 
    387             *iter = new_element; 
    388          } 
     394         add(new_element); 
    389395      } 
    390396   } 
  • juggler/trunk/modules/jackal/config/jccl/Config/Configuration.h

    r19729 r20009  
    9696   bool remove(const std::string& name); 
    9797 
     98   /** 
     99    * Adds the given element to our list of elements. 
     100    * 
     101    * @param new_element ConfigElement to add 
     102    * @note If we already have a ConfigElement with the same name, replace it. 
     103    */ 
     104   void add(ConfigElementPtr newElement); 
     105 
    98106   /* IO functions: */ 
    99107