AttributeRepository.cpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Tue Dec 21 22:43:08 CET 2004  AttributeRepository.cxx 
00003 
00004                         AttributeRepository.cxx -  description
00005                            -------------------
00006     begin                : Tue December 21 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009  
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037  
00038  
00039 
00040 #include "AttributeRepository.hpp"
00041 #include "mystd.hpp"
00042 #include <functional>
00043 #include <boost/bind.hpp>
00044 
00045 namespace RTT
00046 {
00047     using namespace std;
00048     using namespace boost;
00049 
00050   AttributeRepository::AttributeRepository()
00051       :bag(0)
00052   {
00053   }
00054 
00055   AttributeRepository::~AttributeRepository()
00056   {
00057       delete bag;
00058       // we do not claim automatically ownership
00059       // call clear() manually to delete all contents.
00060   }
00061 
00062     AttributeRepository* AttributeRepository::copy( std::map<const DataSourceBase*, DataSourceBase*>& repl, bool inst ) const
00063     {
00064         AttributeRepository* ar = new AttributeRepository();
00065         for ( map_t::const_iterator i = values.begin(); i != values.end(); ++i ) {
00066             ar->setValue((*i)->copy( repl, inst ) );
00067         }
00068         return ar;
00069     }
00070 
00071 
00072   void AttributeRepository::clear()
00073   {
00074     for ( map_t::iterator i = values.begin(); i != values.end(); ++i )
00075       delete *i;
00076     values.clear();
00077     delete bag;
00078     bag = 0;
00079   }
00080 
00081   bool AttributeRepository::setValue( AttributeBase* value )
00082   {
00083     map_t::iterator i = find( values.begin(), values.end(), value );
00084     if ( i != values.end() )
00085         return false;
00086     values.push_back( value );
00087     return true;
00088   }
00089 
00090     bool AttributeRepository::addProperty( PropertyBase* pb ) {
00091         if ( bag && bag->find( pb->getName() ) )
00092             return false;
00093         if ( bag == 0 )
00094             bag = new PropertyBag();
00095         bag->add( pb );
00096         return true;
00097     }
00098 
00099   bool AttributeRepository::removeValue( const std::string& name )
00100   {
00101     map_t::iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00102     if ( i != values.end() ) {
00103         delete (*i);
00104         values.erase( i );
00105         return true;
00106     }
00107     return false;
00108   }
00109 
00110   AttributeBase* AttributeRepository::getValue( const std::string& name ) const
00111   {
00112     map_t::const_iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00113     if ( i == values.end() ) return 0;
00114     else return *i;
00115   }
00116 
00117   bool AttributeRepository::hasAttribute( const std::string& name ) const
00118   {
00119     map_t::const_iterator i = find_if( values.begin(), values.end(), bind(equal_to<std::string>(),name, bind(&AttributeBase::getName, _1)) );
00120     return i != values.end();
00121   }
00122 
00123   bool AttributeRepository::hasProperty( const std::string& name ) const
00124   {
00125       return (bag && bag->find(name) != 0);
00126   }
00127 
00128     bool AttributeRepository::removeProperty( PropertyBase* p )
00129     {
00130         if ( bag && bag->find( p->getName() ) ) {
00131             bag->remove(p);
00132             removeValue( p->getName() );
00133             return true;
00134         }
00135         return false;
00136     }
00137 
00138     std::vector<std::string> AttributeRepository::names() const { return this->getAttributes(); }
00139 
00140     std::vector<std::string> AttributeRepository::getAttributes() const
00141     {
00142         std::vector<std::string> ret;
00143         std::transform( values.begin(), values.end(), ret.begin(),back_inserter(ret), bind(&AttributeBase::getName, _1) );
00144         return ret;
00145     }
00146 
00147     PropertyBag* AttributeRepository::properties() const
00148     {
00149         if ( bag == 0 )
00150             bag = new PropertyBag();
00151         return bag;
00152     }
00153 }

Generated on Tue Mar 25 17:41:40 2008 for OrocosReal-TimeToolkit by  doxygen 1.5.3