Orocos Real-Time Toolkit  2.5.0
ConfigurationInterface.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Tue Dec 21 22:43:08 CET 2004  ConfigurationInterface.cxx
00003 
00004                         ConfigurationInterface.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 "ConfigurationInterface.hpp"
00041 #include "internal/mystd.hpp"
00042 #include <functional>
00043 #include <boost/bind.hpp>
00044 
00045 namespace RTT {
00046     using namespace detail;
00047     using namespace std;
00048     using namespace boost;
00049 
00050   ConfigurationInterface::ConfigurationInterface()
00051   {
00052   }
00053 
00054   ConfigurationInterface::~ConfigurationInterface()
00055   {
00056       // we do not claim automatically ownership
00057       // call clear() manually to delete all contents.
00058   }
00059 
00060     ConfigurationInterface* ConfigurationInterface::copy( std::map<const DataSourceBase*, DataSourceBase*>& repl, bool inst ) const
00061     {
00062         ConfigurationInterface* ar = new ConfigurationInterface();
00063         for ( map_t::const_iterator i = values.begin(); i != values.end(); ++i ) {
00064             ar->setValue((*i)->copy( repl, inst ) );
00065         }
00066         return ar;
00067     }
00068 
00069 
00070   void ConfigurationInterface::clear()
00071   {
00072     for ( map_t::iterator i = values.begin(); i != values.end(); ++i )
00073       delete *i;
00074     values.clear();
00075     bag.clear();
00076   }
00077 
00078   bool ConfigurationInterface::setValue( AttributeBase* value )
00079   {
00080       if ( !value->getDataSource() || value->getName().empty() )
00081           return false;
00082       map_t::iterator i = find( values.begin(), values.end(), value );
00083       if ( i != values.end() ) {
00084           *i = value;
00085       } else
00086           values.push_back( value );
00087       return true;
00088   }
00089 
00090     bool ConfigurationInterface::addProperty( PropertyBase& pb ) {
00091         if ( bag.find( pb.getName() ) )
00092             return false;
00093         bag.add( &pb );
00094         return true;
00095     }
00096     void ConfigurationInterface::removeAttribute( const std::string& name ) {
00097         removeValue(name);
00098     }
00099 
00100   bool ConfigurationInterface::removeValue( const std::string& name )
00101   {
00102     map_t::iterator i = find_if( values.begin(), values.end(), boost::bind(equal_to<std::string>(),name, boost::bind(&AttributeBase::getName, _1)) );
00103     if ( i != values.end() ) {
00104         delete (*i);
00105         values.erase( i );
00106         return true;
00107     }
00108     return false;
00109   }
00110 
00111   AttributeBase* ConfigurationInterface::getValue( const std::string& name ) const
00112   {
00113     map_t::const_iterator i = find_if( values.begin(), values.end(), boost::bind(equal_to<std::string>(),name, boost::bind(&AttributeBase::getName, _1)) );
00114     if ( i == values.end() ) return 0;
00115     else return *i;
00116   }
00117 
00118   bool ConfigurationInterface::hasAttribute( const std::string& name ) const
00119   {
00120     map_t::const_iterator i = find_if( values.begin(), values.end(), boost::bind(equal_to<std::string>(),name, boost::bind(&AttributeBase::getName, _1)) );
00121     return i != values.end();
00122   }
00123 
00124   bool ConfigurationInterface::hasProperty( const std::string& name ) const
00125   {
00126       return (bag.find(name) != 0);
00127   }
00128 
00129     bool ConfigurationInterface::removeProperty( PropertyBase& p )
00130     {
00131         if ( bag.find( p.getName() ) ) {
00132             bag.remove(&p);
00133             return true;
00134         }
00135         return false;
00136     }
00137 
00138     std::vector<std::string> ConfigurationInterface::getAttributeNames() const
00139     {
00140         std::vector<std::string> ret;
00141         std::transform( values.begin(), values.end(), back_inserter(ret), boost::bind(&AttributeBase::getName, _1) );
00142         return ret;
00143     }
00144 
00145     void ConfigurationInterface::loadValues( AttributeObjects const& new_values) {
00146         values.insert(values.end(), new_values.begin(), new_values.end());
00147     }
00148 
00149 
00150     PropertyBag* ConfigurationInterface::properties()
00151     {
00152         return &bag;
00153     }
00154 }
00155 
00156 bool RTT::ConfigurationInterface::chkPtr(const std::string & where, const std::string & name, const void *ptr)
00157 {
00158     if ( ptr == 0) {
00159         log(Error) << "You tried to add a null pointer in '"<< where << "' for the object '" << name << "'. Fix your code !"<< endlog();
00160         return false;
00161     }
00162     return true;
00163 }
00164 
00165